Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/403.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么Winstonjs ExceptionHandler使我的节点错误静音?_Javascript_Node.js_Logging_Exception Handling_Winston - Fatal编程技术网

Javascript 为什么Winstonjs ExceptionHandler使我的节点错误静音?

Javascript 为什么Winstonjs ExceptionHandler使我的节点错误静音?,javascript,node.js,logging,exception-handling,winston,Javascript,Node.js,Logging,Exception Handling,Winston,我在Node中编写了一个程序,我在其中使用Winstonjs进行日志记录。我还有一个exceptionHandler,这样节点异常/错误也会到达我的日志。不过我现在有个问题。当我从命令行使用node index.js(而不是)运行脚本时,当脚本出现错误时,脚本将以静默方式结束 看看下面我的示例代码。我添加了三个console.log()s,它们试图记录一个未定义的变量。当我使用node index.js运行脚本时,对于第一个错误的console.log(未定义变量),它会给我一个Referenc

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

看看下面我的示例代码。我添加了三个
console.log()
s,它们试图记录一个未定义的变量。当我使用
node index.js
运行脚本时,对于第一个错误的
console.log(未定义变量)
,它会给我一个
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版本,但我想我只是犯了一个错误

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

如果你看到

您可以设置
exitOnError=false

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


还可以添加到您的transports
新的winston.transports.Console({handleExceptions:true})
以在Console中显示它。

谢谢您的提示。但是,问题不在于它在错误中存在。问题是它会默默地失败。它会将错误记录到日志文件中,但不会记录在控制台中。我只是想看看控制台中的错误。@kramer65您是否尝试过为控制台添加新的传输?比如:
new winston.transports.Console({handleExceptions:true})
some,这正是我想要的!如果你把这个加在你的答案上,我可以接受!与Kramer65有相同的问题,此解决方案有效。