Change bunyan magazine color for different levels

When using Bunyan, all of my log levels use the same cyan color, for example:

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 to log information about errors / stack traces? The problem is that "ERROR" in red characters is not enough to get my attention - I would like the whole stack to be red or purple.

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

I see only the "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' } ] }); 
+5
source share
2 answers

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

The following example displays the information log in blue and 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. */ 
+4
source

You can check how bunyan-format color magazines are or just use this module.

+3
source

Source: https://habr.com/ru/post/1270557/


All Articles