为什么 Winstonjs ExceptionHandler 会消除我的节点错误?
Why does Winstonjs ExceptionHandler silence my node errors?
我有一个用 Node 编写的程序,我在其中使用 Winstonjs 进行日志记录。我还有一个 exceptionHandler,这样节点 exceptions/errors 也可以到达我的日志。我现在有一个问题。当我使用 node index.js
(而不是 pm2)从命令行 运行 脚本时,脚本在出现错误时静默结束。
看看下面我的示例代码。我添加了三个 console.log()
s 来尝试记录一个未定义的变量。当我 运行 使用 node index.js
的脚本时,它会按预期为第一个错误 console.log(undefinedVariable)
提供 ReferenceError
。当我现在删除第一个 and/or 第二个 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 })
以便在控制台中显示。
我有一个用 Node 编写的程序,我在其中使用 Winstonjs 进行日志记录。我还有一个 exceptionHandler,这样节点 exceptions/errors 也可以到达我的日志。我现在有一个问题。当我使用 node index.js
(而不是 pm2)从命令行 运行 脚本时,脚本在出现错误时静默结束。
看看下面我的示例代码。我添加了三个 console.log()
s 来尝试记录一个未定义的变量。当我 运行 使用 node index.js
的脚本时,它会按预期为第一个错误 console.log(undefinedVariable)
提供 ReferenceError
。当我现在删除第一个 and/or 第二个 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 })
以便在控制台中显示。