[英]Change bunyan log color for different levels
When using Bunyan, all my log levels use the same cyan color, like so:使用 Bunyan 时,我所有的日志级别都使用相同的青色,如下所示:
Here is the Bunyan configuration we are using:这是我们正在使用的 Bunyan 配置:
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?我的问题是 - 如何让 Bunyan 使用红色或洋红色来记录错误信息/堆栈跟踪? 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这是班扬自述文件: 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'
}
]
});
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.使用 bunyan 登录时更改控制台输出颜色的一种方法是提供自定义流并将其与颜色模块结合使用。
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.
*/
您可以检查bunyan 格式的颜色如何记录并在您的代码中执行相同的操作,或者将其作为模块导入并直接使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.