7

When using Bunyan, all my log levels use the same cyan color, like so:

enter image description here

Here is the Bunyan configuration we are using:

const bunyan = require('bunyan');
module.exports = bunyan.createLogger({name: 'cdt-api-server'});

My question is - how can I get Bunyan to use red or magenta for logging error information / stack traces? The problem is that "ERROR" in red characters is not enough to capture my attention - I'd like to have the whole stack in red or magenta.

Here is the Bunyan readme: https://github.com/trentm/node-bunyan

I only see "color" mentioned once.

Can we do something like this?

const bunyan = require('bunyan');

module.exports = bunyan.createLogger({
  name: 'cdt-api-server',
  streams: [
    {
      level: 'trace',
      stream: process.stdout,
      color: 'black',
    },
    {
      level: 'debug',
      stream: process.stdout,
      color: 'blue',
    },
    {
      level: 'info',
      stream: process.stdout,
      color: 'cyan',
    },
    {
      level: 'error',
      path: process.stderr,
      color: 'red'
    },
    {
      level: 'warn',
      path: process.stderr,
      color: 'magenta'
    }
  ]
});
Alexander Mills
  • 90,741
  • 139
  • 482
  • 817

2 Answers2

6

One way to change the color of the console output when logging with bunyan is to provide a custom stream and use it in combination with the colors module.

The below example will output the info log in blue while the error log in red:

var colors = require('colors');
var bunyan = require('bunyan');

function MyRawStream() {}
MyRawStream.prototype.write = function (rec) {
    console.log('[%s] %s: %s',
        rec.time.toISOString(),
        bunyan.nameFromLevel[rec.level],
        rec.msg);
}

var log = bunyan.createLogger({
    name: 'play',
    streams: [
        {
            level: 'info',
            stream: new MyRawStream(),
            type: 'raw'
        }
    ]
});

log.info(colors.blue('This is an info message displayed in blue.'));
log.error(colors.red('This is an error message displayed in red.'));

/* Output:
[2017-08-14T20:32:33.550Z] info: This is an info message displayed in blue.
[2017-08-14T20:32:33.558Z] error: This is an error message displayed in red.
*/
M3talM0nk3y
  • 1,382
  • 14
  • 22
  • the write method can probably be overwriten without using console.log - it seems like it would be incorrect if we use console.log there, no? – Alexander Mills Aug 17 '17 at 00:09
  • Under the hood, `console.log()` calls `process.stdout.write()` with formatted output; the latter function being what bunyan defaults to for the ***info*** log level - see [Streams Introduction](https://github.com/trentm/node-bunyan#streams-introduction). Writing to said function was how I got this to work. If you look at the implementation for [bunyan-format](https://github.com/thlorenz/bunyan-format) (which is the other module suggested), they actually invoke `formatRecord()` internally before sending output to ***process.stdout***. – M3talM0nk3y Aug 17 '17 at 00:32
5

You can check how bunyan-format colors logs and do the same in your code, or just import it as a module and use it directly.

waldyrious
  • 3,683
  • 4
  • 33
  • 41
Jehy
  • 4,729
  • 1
  • 38
  • 55
  • What is "this module"? – Alexander Mills Aug 11 '17 at 17:43
  • 1
    Links to external resources are encouraged, but please add context around the link so your fellow users will have some idea what it is and why it’s there. Always quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline. – Graham Aug 14 '17 at 20:35