2

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

victorkt
  • 13,992
  • 9
  • 52
  • 51
user3007385
  • 153
  • 4
  • 15

1 Answers1

3

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

Ankur Soni
  • 5,725
  • 5
  • 50
  • 81
clay
  • 5,917
  • 2
  • 23
  • 21
  • Hi Clay, I am ok with the first answer. I have implemented it in windoes machine and I am getting the output as expected. But when I am trying to run the same node server in raspberry pi I am getting an error stating bunyan not found. sorry to mention that I am not able to understand second answer. can you please explain with an example. – user3007385 May 21 '15 at 05:15
  • The second answer, for how to configure bunyan from a config file, basically boils down to "Use data from a config file to create the bunyan log instance". Bunyan must be configured, either the `stream` object, or `streams : [ ]` that is part of the object passed into `bunyan.createLogger`. With that, you can create any file path you want, but *you* must connect your config data to that bunyan call, bunyan won't do it for you. I'll also edit my answer with another approach. – clay May 21 '15 at 12:35
  • Can i achieve #1 by passing some param in createLogger function rather than cli? – Johnson Cherian Oct 13 '21 at 01:58