node.js - winston 'handleExceptions: true' 记录两次
问题描述
只有一种传输方式,添加如下:
winston.add(winston.createLogger({
transports: [
new winston.transports.Console({
handleExceptions: true,
format: winston.format.combine(
winston.format.simple()
),
}),
]
}));
当从index.js
:抛出新的错误时,
throw new Error('++++ I will log twice ++++')
将产生两个单独的日志!
我也试过transport.File({ ... })
,但结果一样。
更新:正如@terry-lennox在他的回答中提到的,输出类似于:
error: uncaughtException: ++++ I will log twice ++++
Error: ++++ I will log twice ++++
at Object.
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
error: uncaughtException: ++++ I will log twice ++++
Error: ++++ I will log twice ++++
at Object.
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
系统设置版本:
OS: Windows 10 v1803
NodeJS: 10.14.1
express: 4.16.4
winston: 3.2.1
解决方案
根据我的经验,这对于 Javascript/节点异常日志记录来说实际上是很正常的。它实际上不是两个日志,而是一个嵌入了换行符的日志。
如果你分解它,它基本上是:
[LOGLEVEL]: [MESSAGE]
[FULL EXCEPTION DUMP]
我假设“MESSAGE”是winston打印“uncaughtException”+ e.message。
当您记录异常时,您会得到:
[EXCEPTIONAME]: [MESSAGE]
[STACK TRACE]
您可以通过切换到 winston.format.json() 来确认这一点 - 在这种情况下,它将整个日志转换为 JSON,因此您可以看到它只是一个日志。您还可以通过添加 winstson.format.timestamp() 使用简单的日志格式进行确认 - 您会看到它仅将时间戳放在整个文本正文的开头,而不是每一行。
推荐阅读
- python - 在 Azure Functions 中输入多个 blob
- if-statement - 将 if/else 的主体留空的约定
- symfony4 - 如何在 api-platform 的 dto 中返回输出类的数据实例
- python - Sklearn 中 Isomap 的示例扩展
- go - 如何在 Go 中解析 RFC3339?
- javascript - jsVectorMap 渲染太小并破坏布局
- oracle - Oracle字符串操作根据分隔符排除特定字符
- c# - 如何获取应用于 ToObject() 的 JSValue 中的属性?
- wireshark - 在一个wireshark中显示多个tcpdump outout
- flutter - Dynamic List View with Text and Radio Button In Flutter