【问题标题】:Why does Winstonjs ExceptionHandler silence my node errors?为什么 Winstonjs ExceptionHandler 使我的节点错误静音?
【发布时间】:2018-10-03 10:04:57
【问题描述】:

我有一个用 Node 编写的程序,我在其中使用 Winstonjs 进行日志记录。我还有一个异常处理程序,以便节点异常/错误也到达我的日志。我现在有一个问题。当我使用node index.js(而不是pm2)从命令行运行脚本时,脚本会在出现错误时静默结束。

看看我下面的示例代码。我添加了三个console.log()s,它们试图记录一个未定义的变量。当我使用node index.js 运行脚本时,它给了我一个ReferenceError 来表示第一个错误的console.log(undefinedVariable),正如预期的那样。当我现在删除第一个和/或第二个 console.log 时,脚本会静默结束。

"use strict";

let winston = require('winston');
const path = require('path');

const PRODUCTION = false;

// LOGGING
const myFormat = winston.format.printf(info => {
    return `${info.timestamp} ${info.level}: ${info.message}`;
});

console.log(undefinedVariable);  // THIS GIVES A REFERENCE ERROR

const logger = winston.createLogger({
    level: 'debug',
    format: winston.format.combine(winston.format.timestamp(), myFormat),
    transports: [
        new winston.transports.File({filename: 'logs/error.log', level: 'error'}),
        new winston.transports.File({filename: 'logs/combined.log'}),
    ],
    exceptionHandlers: [
        new winston.transports.File({ filename: 'logs/exceptions.log' }),
        new winston.transports.File({ filename: 'logs/combined.log' })
    ]
});

console.log(undefinedVariable);  // THIS DOES NOT GIVE A REFERENCE ERROR, BUT ENDS THE SCRIPT SILENTLY

if (!PRODUCTION) {
    // If we're not in production then also log to the `console`
    logger.add(new winston.transports.Console(
        {format: winston.format.combine(winston.format.timestamp(), myFormat), level: 'debug'}
    ));
}

console.log(undefinedVariable);  // THIS ALSO DOES NOT GIVE A REFERENCE ERROR, BUT ENDS THE SCRIPT SILENTLY

function log(message, level='debug'){
    // Levels: error, warn, info, verbose, debug, silly
    const e = new Error();
    const regex = /\((.*):(\d+):(\d+)\)$/
    const match = regex.exec(e.stack.split("\n")[2]);
    let log_source = path.basename(match[1]) + ':' + match[2];  // eg: index.js:285

    if (typeof message === 'object'){
        message = JSON.stringify(message);
    }
    logger[level](log_source + ' - ' + message);
}

我正在运行 Winstonjs 版本 3.0.0-rc5。我知道这还不是最终的 3.0 版本,但我想我只是在这里犯了一个错误。

有人知道我在这里做错了什么吗?欢迎所有提示!

【问题讨论】:

    标签: javascript node.js logging exception-handling winston


    【解决方案1】:

    如果你看到Handling Uncaught Exceptions with winston doc

    你可以设置exitOnError = false

    默认情况下,winston 将在记录 uncaughtException 后退出。如果这不是您想要的行为,请设置 exitOnError = false

    并且还添加到您的传输new winston.transports.Console({ handleExceptions: true }) 以在控制台中显示它。

    【讨论】:

    • 感谢您的提示。但是,问题不在于它在错误时退出。问题是它默默地失败了。它确实将错误记录到日志文件中,但不在控制台中。我也只是想在控制台中查看错误。
    • @kramer65 您是否尝试为控制台添加新的传输?喜欢:new winston.transports.Console({ handleExceptions: true })
    • 太棒了,这正是我想要的!如果您将其添加到您的答案中,我可以接受!
    • 和Kramer65有同样的问题,这个解决方案有效。
    猜你喜欢
    • 1970-01-01
    • 2010-11-26
    • 1970-01-01
    • 2017-08-07
    • 1970-01-01
    • 2020-03-26
    • 2022-01-21
    • 2019-09-24
    • 1970-01-01
    相关资源
    最近更新 更多