[英]NodeJS - Winston file transport with pretty JSON
I'm using Winston for logging to a file. 我正在使用Winston记录到文件。 I would like to log to a file in a human readable way and have JSON objects formatted with line breaks and tabs.
我想以人类可读的方式登录文件,并使用换行符和制表符格式化JSON对象。
I'm trying to use the formatter
function. 我正在尝试使用
formatter
功能。
var winston = require('winston');
var moment = require('moment');
function formatter(args) {
var date = moment().format("D/MM/YYYY hh:mm:ss");
var msg = date + ' - ' + args.level + ' - ' + args.message + ' - ' + JSON.stringify(args.meta);
return msg;
}
var logger = new (winston.Logger)({
transports: [
new (winston.transports.File)({
level: 'silly',
filename: __dirname + '/logs/test.log',
json: false,
formatter: formatter
})
]
});
logger.log('info', 'info 123', { some: 'json' });
Output: 输出:
16/12/2015 12:23:44 - info - info 123 - {"some":"json"}
If I don't use JSON.stringify
I just get [object Object]
. 如果我不使用
JSON.stringify
我只需要[object Object]
。
What I'd like to get to is: 我想要的是:
16/12/2015 12:23:44 - info - info 123 -
{
"some":"json"
}
Can Winston do this out the box in some way..? 温斯顿可以用某种方式解决这个问题吗?
...or is there a function someone has written that find {}
in a string and adds the line breaks and tabs..? ...或者是否有人编写的函数在字符串中找到
{}
并添加换行符和制表符...?
Like this? 像这样?
var msg = date + ' - ' +
args.level + ' - ' +
args.message + ' - \n' + JSON.stringify(args.meta, null, 2);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.