javascript - 为什么 Winstonjs ExceptionHandler 会消除我的 Node 错误?

标签 javascript node.js logging exception-handling winston

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

请看下面我的示例代码。我添加了三个 console.log() 来尝试记录一个 undefined variable 。当我使用 node index.js 运行脚本时,它会按预期为第一个错误的 console.log(undefinedVariable) 提供 ReferenceError。当我现在删除第一个和/或第二个 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 版本,但我想我只是在这里犯了一个错误。

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

最佳答案

如果您看到 Handling Uncaught Exceptions with winston doc

你可以设置exitOnError = false

By default, winston will exit after logging an uncaughtException. If this is not the behavior you want, set exitOnError = false

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

关于javascript - 为什么 Winstonjs ExceptionHandler 会消除我的 Node 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49979743/

相关文章:

javascript - 表格计算JS(单选)

node.js - 如何在 grunt.js 上绑定(bind)服务器和客户端?

postgresql - 在 postgres 9.1 中记录触发器的触发

javascript - JavaScript 中如何为字符串加数组定义加号运算符的串联

javascript - jQuery:在状态和类之间切换

javascript - 是否可以使用 jQuery 更改伪选择器,例如悬停?

javascript - 使用 Tensorflow.js 计算损失梯度

node.js - 我可以从自己的服务器安装 Node.js 模块吗?

c# - 如何使用NLog登录到Amazon ElasticSearch?

java - log4j.xml 日志记录在 spring Controller 中不起作用?