【问题标题】:winston 'handleExceptions: true' logs twicewinston 'handleExceptions: true' 记录两次
【发布时间】:2021-12-31 11:42:46
【问题描述】:

只有一种传输方式,添加如下:

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 在他的answer 中提到的那样,输出类似于:

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

【问题讨论】:

    标签: node.js logging winston


    【解决方案1】:

    这是你看到的那种行为吗:

    错误:uncaughtException:++++ 我将记录两次 ++++ 错误:++++ 我将记录两次 ++++ 在对象。 在 Module._compile (internal/modules/cjs/loader.js:689:30) 在 Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10) 在 Module.load (internal/modules/cjs/loader.js:599:32)

    或者你看到这重复了吗?

    【讨论】:

    • 是的,这很奇怪。我很难复制这个..我想知道你能否给我你的操作系统和 Node.js 版本?我对不同平台上文件 I/O 等不同行为的表现感到惊讶。
    • 确实,我已经更新了问题以涵盖设置版本
    【解决方案2】:

    根据我的经验,这对于 Javascript/节点异常日志记录来说实际上是很正常的。它实际上不是两个日志,而是一个嵌入了换行符的日志。

    如果你把它分解,它基本上是:

    [LOGLEVEL]: [MESSAGE]
    [FULL EXCEPTION DUMP]
    

    我假设“MESSAGE”是 winston 打印“uncaughtException”+ e.message。

    当您记录异常时,您会得到:

    [EXCEPTIONAME]: [MESSAGE]
    [STACK TRACE]
    

    您可以通过切换到 winston.format.json() 来确认这一点 - 在这种情况下,它将整个日志转换为 JSON,因此您可以看到它只是一个日志。您还可以通过添加 winstson.format.timestamp() 来使用简单的日志格式进行确认 - 您会看到它仅将时间戳放在整个文本正文的开头,而不是每一行。

    【讨论】:

      猜你喜欢
      • 2017-11-10
      • 1970-01-01
      • 1970-01-01
      • 2017-07-21
      • 1970-01-01
      • 2014-08-02
      • 2019-09-29
      • 2015-04-15
      • 1970-01-01
      相关资源
      最近更新 更多