Correctly logging errors with nodejs bunyan?

I read Joyent's postmortem debug article and they recommended we use bunyan.

They talk about how the quality of the log information is very important and can help in resolving errors when they arise. Then there are external blog sites that show you how you can install and run bunyan in your Nodejs app, which is nice.

But I am new and in the dark about how to use logging effectively (both bunyan specific and logging techniques in general). I have three questions that are related to each other.

(1) Should I use bunyan to log every action that happens in my application?

t

router.post('/submit', function(req, res) {
    log.info({ req:req }, req.user.name + ' has called /submit');

    saveData(req)
    .then(function(data) {
        log.info({data: data}, req.user.name + ' has saved to DB successfully in /submit');
        res.json({'success': 'Saved!'});
    })
    .error(function(err) {
        log.error({ err: err }, req.user.name + ' has caused an error in /submit ');
        res.status(500).json("error": err});
    }); 
});

      

... And so on everywhere in my application?

(2) If so, can I get the log output in unrelated chunks? For example, if we have 3 concurrent users calling /submit

, then the messages could be:

Alice has called /submit
Bob has called /submit
Alice has caused an error in /submit
Carol has called /submit
Bob has saved to DB successfully in /submit
Carol has saved to DB successfully in /submit

      

It is right? If I have a stacktrace for Bob

, and I'm trying to get a prior knowledge of the state of the system before the program breaks, I'll have to somehow filter out everyone else and sort by timestamp to get a consistent order of events for Bob

?

(3) Should I use bunyan to register stacktraces? , that is, adding err.stack

to the bunyan log:

.error(function(err) {
    log.error({ err: err, stacktrace: err.stack }, req.user.name + ' has caused an error in /submit ');
    res.status(500).json("error": err});
});

      

... Or is there some other standard practice for logging and reporting errors (what does it basically mean to keep the stack stacks?)?

+3


source to share


1 answer


(1) Should I use bunyan to log every action that happens in my application?

Yes, basically. There are a few subtle points to this, but basically you want to make a good signal-to-noise trade-off while still maintaining a reasonable likelihood of successful debugging once started. Go too much about codes that never fail and you're drowning in noise. Don't write down enough and you may be literally clueless as to what caused the problem. Don't worry too much about it. It is a feeling that you only get with experience and it is easy enough to look through the analysis of the logs where you generate noise and can suppress the sound or filter it after the fact and where you have a large set of complex code executing without any details and you need add more protocols there.

(2) If so, can I get the log output in unrelated chunks?



Yes, but that's fine. You use post-processing tools to reconnect to a more consistent description. Common methods, including registering both a unique request ID for each request and a session ID. This way you can filter one user session or just one interesting query.

(3) Should I use bunyan to register stacktraces?

Yes, but bunyan includes standard serializers for Error instances, so just name the property err

and include the standard serializers and you will get an error message and register the stack trace correctly.

+5


source







All Articles