For #1, I'm assuming you're using the bunyan-cli. You can change a few things, but I don't think you can change the order. It does have a formatter, and you could use node index.js | bunyan -o short
to change your output from this:
[2015-05-13T22:55:28.613Z] INFO: App/sampleObject/77405 on host.local: User logged in (reqId=1, user=frank)
[2015-05-13T22:55:28.615Z] INFO: App/sampleObject/77405 on host.local: User queried DB (reqId=1, user=frank)
to this:
22:55:15.830Z INFO App/sampleObject: User logged in (reqId=1, user=frank)
22:55:15.832Z INFO App/sampleObject: User queried DB (reqId=1, user=frank)
I find that more readable.
For #2, you'd want to set up a logging instance the start of your app, from a config file. Something like below:
var bunyan = require('bunyan');
var configOptions = require('../path/to/config.json');
var logger = bunyan.createLogger( configOptions );
bunyan.log = logger;
There are better strategies for loading a config file, but a single JSON file will work. You might need more options than pure JSON can provide if you want to set up process.stdout
streams, so a config.js
file would be better in that case.
In other files, you'd access the log like:
var log = require('bunyan').log;
log.info('This is another file.`);
Be sure to configure the logger before requiring the other files, or the logging object log
will not be initialized correctly.
NOTE: You can also add a stream to a Bunyan logger dynamically. This is not in their documentation (so maybe use at your own risk), but for a given logger
, you can make the call logger.addStream( streamConfigObj )
where streamConfigObj
is the same object you would use in stream
or stream:[]
to .createLogger