我在第 3 版,但由于我已经升级了记录对象的唯一方法是首先对对象进行字符串化。
但是错误更糟,因为我根本无法记录错误,我需要使用 error.message 或直接使用控制台。
这是我的开发环境配置:
`` const { createLogger,格式,传输} = require('winston');
const { 组合、时间戳、标签、printf } = 格式;
const environment = process.env.NODE_ENV;
const timestampFormat = 'YYYY-MM-DD HH:mm:SS';
const loggerFormat = printf((info) => {
return `[${info.label}] ${info.message}`;
});
logger = createLogger({
format: combine(
label({ label: 'immoliste' }),
format.colorize({ all: true }),
format.splat(),
format.simple(),
loggerFormat
),
transports: [
new transports.Console({
level: 'debug',
colorized: true
})
]
});
这样做:
logger.error('Error sending contact message:', JSON.stringify(err));
我只得到:
[mylabel] Error sending contact message:
但是,必须对所有内容进行字符串化,并为错误执行此操作是一件很痛苦的事情。 我在做什么错?,因为在以前的版本中它很简单。
+1
@eyp你可以试试这个吗?
logger.error('Error sending contact message:%o', {foo: "bar"});
底层来源在这里:
https://github.com/winstonjs/logform/blob/master/splat.js
你可以看到它使用来自节点的util.format
https://nodejs.org/api/util.html#util_util_format_format_args
所以%o
, %O
和%j
完成了这项工作
%o
就像一个魅力,我不知道温斯顿使用了与 util.format 相同的格式参数。
谢谢!
温斯顿 3.1.0
我试试这个写在这里:
const debugConsole = new winston.transports.Console({
级别:'调试',
格式:winston.format.combine(winston.format.simple()),
处理异常:真
})
logger.add(调试控制台)
在我使用的代码中: logger.debug('%o', req.query)
我看到了 json 对象,但我之前也看到了“%o”,控制台日志如下:
调试:%o {"offset":"73000","count":"3","timestamp":"2018-10-29 15:02:08"}
我在做什么?
通常 %o 由 util.format() 调用,您可以在其中指定所需的对象。 从输出看起来你甚至不需要 %o 因为正在显示对象。
只需使用它, logger.debug(req.query) 它应该可以工作。 或者你可以像这样使用 util.format 将它存储在一个变量中
const queryFormatted = util.format('%o', req.query)
logger.debug(queryFormatted)
@frenzymind或者尝试解决@mingchuno 发布到原始问题的工作。
只需使用它, logger.debug(req.query) 它应该可以工作
@eponymz在这种情况下有:调试:[object Object] {"timestamp":"2018-10-30 11:14:04"}
const queryFormatted = util.format('%o', req.query)
这是有效的,但很吵。
第二个的输出是你想要的吗? 我的意思是,如果你想在日志中查询,它是否显示。 如果不尝试在 mingchuno 发布之前提到的解决方法。
是的,第二种方式按我的意愿工作。 我看到了 json 对象数据。
如果我理解正确,mingchuno 建议在字符串中使用 %o,因为 winston 在“幕后”使用 util。 我尝试了它并且有效,但是在我的情况下也打印了 %o 。 我还将 splat() 格式添加到我的调试级别,但没有任何变化。 我错过了什么吗?
是的,我看到%o
预置到对象检查中。 我想这是某种错误?
这是一个深思熟虑的设计选择吗? 它与标准有很大的偏差(我花了一个小时才找到)而不是
console.log('Log me plz: ', {'ok': 'logged'});
必须(winston 3.x)将所有日志调用更改为
logger.info('Log me plz: %o', {'ok':'logged'});
对不起,我说的对吗? 我很迷惑...
说真的,这似乎是降级。 使用 Winston 2,我可以将所有console.X
替换winston.X
。 为什么突然出现丑陋的格式标签?
因此,解决方法是将%o
附加到我要打印出对象的每个日志记录语句中?
是的!
无法接受我必须始终使用 %o 并提出了这个简单的解决方案:
const prettyJson = format.printf(info => {
if (info.message.constructor === Object) {
info.message = JSON.stringify(info.message, null, 4)
}
return `${info.level}: ${info.message}`
})
const logger = createLogger({
level: 'info',
format: format.combine(
format.colorize(),
format.prettyPrint(),
format.splat(),
format.simple(),
prettyJson,
),
transports: [
new transports.Console({})
],
})
所以这个记录器....
logger.info({ hi: 123 })
...在控制台中转换为这个
info: {
"hi": 123
}
希望我能帮助你们:D 如果我们总是有两个使用“%o”,那就太糟糕了
无法接受我必须始终使用 %o 并提出了这个简单的解决方案:
const prettyJson = format.printf(info => { if (info.message.constructor === Object) { info.message = JSON.stringify(info.message, null, 4) } return `${info.level}: ${info.message}` }) const logger = createLogger({ level: 'info', format: format.combine( format.colorize(), format.prettyPrint(), format.splat(), format.simple(), prettyJson, ), transports: [ new transports.Console({}) ], })
所以这个记录器....
logger.info({ hi: 123 })
...在控制台中转换为这个
info: { "hi": 123 }
希望我能帮助你们:D 如果我们总是有两个使用“%o”,那就太糟糕了
不错,但很奇怪,我需要为我所有的记录器级别做这件事吗?
仅供参考,最好使用
```javascript
typeof info.message === 'object'
````
代替
```javascript
info.message.constructor === 对象
````
当消息为空或未定义时避免错误,并且数组将正确显示
(例如:[{hi:“456”}]
最有用的评论
说真的,这似乎是降级。 使用 Winston 2,我可以将所有
console.X
替换winston.X
。 为什么突然出现丑陋的格式标签?