1
child.log.info('info');
child.log.debug('debug');

I use the following command:

node app.js | bunyan -o short -l debug

However it only shows INFO. Does not show DEBUG. I'd like ot display debug messages in the console, what am i doing wrong?

basickarl
  • 37,187
  • 64
  • 214
  • 335

2 Answers2

2

When you create a logger, use the level option to set the level at which bunyan will emit log messages:

var log = bunyan.createLogger({ level: "debug" });

Similarly, use the level option to set the level at which child loggers will emit log messages:

var child = log.child({ level: "debug" });

This level option set at the time of instantiation determines the minimum level of log message emitted by bunyan.

The command line switch -l ( or --level ) is simply a filter; it allows through messages emitted at a given level or above. But, it does not set the minimum level of log message emitted by your instance of bunyan.

For example, suppose I want an instance of bunyan that emits log messages at the "debug" level or higher. I can put the following in main.js:

var bunyan = require("bunyan");
var log = bunyan.createLogger({ level: "debug" });

log.trace("Nitty gritty info here.");
log.debug("Eek ork op.");
log.info("The Red Zone is for loading and unloading only.");

If I run the following from the command line:

node main.js | bunyan

I will see this in the output:

Eek ork op.
The Red Zone is for loading and unloading only.

Notice I didn't filter anything from the command line and the call to log.trace() didn't print anything, either.

Now, type this into the command line:

node main.js | bunyan -l "debug"

You'll get:

Eek ork op.
The Red Zone is for loading and unloading only.

The same as before.

Now, try:

node main.js | bunyan -l "trace"

You'll still get the same output as before.

Why? Because your instance of bunyan is set to emit messages at the "debug" level or higher. The command-line switch -l is just a filter.

Finally, try:

node main.js | bunyan -l "info"

This time, you'll only see:

The Red Zone is for loading and unloading only.

The call to log.debug() was filtered out.

StudentsTea
  • 329
  • 4
  • 16
  • For reference on setting level in constructor: https://www.npmjs.com/package/bunyan#constructor-api – arcseldon Jul 04 '19 at 04:57
  • what if you dont have access to bunyan.createLogger? What if you only have the 'log' variable? – john k Aug 17 '23 at 15:38
  • @johnk You'll have to figure out where your instance of `log` is getting created and adjust settings there. I highly recommend learning how to use `grep` on a code base to find where stuff is created and used in a code base. – StudentsTea Aug 19 '23 at 08:00
0

While creating streams for different log levels, creating a stream for debug like

 bunyan.createLogger({
    name: <name>,
    streams: [{
        level : 'debug',
        stream : process.stdout
    }]
});

Using this all your debugging logs would be streamed to the console.

Jayant Jaiswal
  • 171
  • 1
  • 8