node.js 温斯顿没有漂亮地打印到控制台
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17963406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Winston doesn't pretty-print to console
提问by JoBu1324
I'm trying to get Winston to pretty print to the console, so I stuck this in a file and ran it with node:
我试图让 Winston 漂亮地打印到控制台,所以我把它放在一个文件中并用 node 运行它:
var winston = require('winston');
winston.cli();
winston.data({
a: "test",
of: "many",
properties: {
like: "this"
}
});
winston.data('data', {
a: "test",
of: "many",
properties: {
like: "this"
}
});
The terminal spit back the following (not exactly pretty) messages:
终端回吐以下(不完全是漂亮的)消息:
data: a=test, of=many, like=this
data: data a=test, of=many, like=this
I'm following the instructions on the Winston Readme("Using winston in a CLI tool"). Am I misreading something? Missing a setting somewhere?
我正在按照Winston 自述文件(“在 CLI 工具中使用 winston”)上的说明进行操作。我是否误读了什么?某处缺少设置?
回答by JoBu1324
I figured out the answer (the documentation is incorrect). If you use the constructor, and manually add transports, you can set options, both for winston, and for individual transports. Certain options need to be added to winston directly, while others need to be added to the transport.
我想出了答案(文档不正确)。如果使用构造函数并手动添加传输,则可以为 winston 和单个传输设置选项。某些选项需要直接添加到 winston 中,而其他选项需要添加到传输中。
E.g.:
例如:
var winston = require('winston');
var logger = new (winston.Logger)({
levels: {
trace: 0,
input: 1,
verbose: 2,
prompt: 3,
debug: 4,
info: 5,
data: 6,
help: 7,
warn: 8,
error: 9
},
colors: {
trace: 'magenta',
input: 'grey',
verbose: 'cyan',
prompt: 'grey',
debug: 'blue',
info: 'green',
data: 'grey',
help: 'cyan',
warn: 'yellow',
error: 'red'
}
});
logger.add(winston.transports.Console, {
level: 'trace',
prettyPrint: true,
colorize: true,
silent: false,
timestamp: false
});
logger.add(winston.transports.File, {
prettyPrint: false,
level: 'info',
silent: false,
colorize: true,
timestamp: true,
filename: './nKindler.log',
maxsize: 40000,
maxFiles: 10,
json: false
});
回答by Sam Gh
If you're using [email protected] then the accepted answer won't work. Try the following:
如果您使用的是 [email protected],那么接受的答案将不起作用。请尝试以下操作:
const winston = require("winston");
let date = new Date().toISOString();
const logFormat = winston.format.printf(function(info) {
return `${date}-${info.level}: ${JSON.stringify(info.message, null, 4)}\n`;
});
const logger = winston.createLogger({
transports: [
new winston.transports.Console({
level: level,
format: winston.format.combine(winston.format.colorize(), logFormat)
})
]
});
The logs will have the following format:
日志将采用以下格式:
It's colored BTW
它是彩色的 BTW
2018-03-01T19:49:54.042Z-info: "----- Customer Details ------"
2018-03-01T19:49:54.042Z-info: [
{
"A": 1,
"B": 2
}
]
回答by chrishiestand
Here's a solution for Winston v3+
这是 Winston v3+ 的解决方案
let winstonFormat = winston.format.json();
if (NODE_ENV == "development") {
winstonFormat = winston.format.combine(winston.format.json(), winston.format.prettyPrint());
}
const log = winston.createLogger({
level: "info",
format: winstonFormat,
defaultMeta: {app: "myapp"},
transports: [
new winston.transports.File({filename: "/dev/stderr", level: "warn"}),
new winston.transports.File({filename: "/dev/stdout"}),
],
});
回答by timetofly
I took @partycoder's answer and trimmed it down to use only the default logging levels that come with winston. It also doesn't reverse the order of the errors. 0 = highest priority.
我接受了@partycoder 的回答并将其缩减为仅使用 winston 附带的默认日志记录级别。它也不会颠倒错误的顺序。0 = 最高优先级。
winston.addColors({
silly: 'magenta',
debug: 'blue',
verbose: 'cyan',
info: 'green',
warn: 'yellow',
error: 'red'
});
winston.remove(winston.transports.Console);
winston.add(winston.transports.Console, {
level: process.env.LOG_LEVEL,
prettyPrint: true,
colorize: true,
silent: false,
timestamp: false
});
回答by Nehemie Niyomahoro
I faced the same issue too but I came to meet it on git, and found some interesting trick that might be helpful about it. issue #1217
我也遇到了同样的问题,但我在 git 上遇到了它,并发现了一些可能对它有帮助的有趣技巧。 问题 #1217
First of all, the trick is in using
首先,诀窍在于使用
JSON.stringify(jsonData, null, 4) // This helps indent lines and make them readable
You can see it in this visual studio code screenshoot below for clear documentation how to use JSON.stringify
您可以在下面的这个 Visual Studio 代码截图中看到它,以获得如何使用 JSON.stringify 的清晰文档
For example if you were using a console transporter you would create it like this
例如,如果您使用的是控制台传输器,您将像这样创建它
transports: [
new winston.transports.Console({
format: printf((info) => {
return `${info.timestamp} [${info.label}] [${info.level}] : ${info.message}\n
${JSON.stringify(info.meta, null, 4)}`; // Meta as additional data you provide while logging
;
})
})
]
And use it like
并使用它
Logger.info("Message text", { meta: metaDataHere } )

