简体   繁体   中英

Formatting output in bunyan logging

I am experimenting on node bunyan module. As a part of it I would like to know some facts regarding bunyan..

  1. Does bunyan facilitate a way to change the order of the contents printed on bunyan logs. For example by default timestamp will be printed at the end...Is there a way to print it at the start? If yes please share with me..

  2. Bunyan logs will be logged to a file by specifying the path in the application. Instead of specifying in the application,can we specify somewhere else in the properties file.If so please share how to do that...

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 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM