javascript - 如何在 Winstonjs 记录器中记录未处理的超时错误?

标签 javascript node.js logging error-handling winston

我有一个 Node 应用程序,我在其中使用 Winstonjs作为记录器。在我的记录器中,我有一个特定的部分用于记录这样的异常:

const myFormat = winston.format.printf(info => {
    return `${info.timestamp} ${info.level}: ${info.message}`;
});
const logger = winston.createLogger({
    level: "debug",
    format: winston.format.combine(winston.format.timestamp(), myFormat),  // winston.format.json(),
    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' })
    ],
    exitOnError: false
});

所以任何异常都应该记录在 logs/combined.loglogs/exceptions.log 中。

当我运行我的程序时,我现在有时会在我的 STDOUT 中收到以下错误,该错误来自 Sequelizejs我用它来写入我的 MySQL 数据库。

Unhandled rejection TimeoutError: ResourceRequest timed out
    at ResourceRequest._fireTimeout (/root/mmjs/node_modules/generic-pool/lib/ResourceRequest.js:62:17)
    at Timeout.bound (/root/mmjs/node_modules/generic-pool/lib/ResourceRequest.js:8:15)
    at ontimeout (timers.js:424:11)
    at tryOnTimeout (timers.js:288:5)
    at listOnTimeout (timers.js:251:5)
    at Timer.processTimers (timers.js:211:10)

此错误仅出现在标准输出中(当手动运行我的程序时),它不会出现在 combined.logexceptions.log 中.可能是因为这是错误而不是异常。不过,我不确定如何正确记录。

(我想我可以 wrap my whole code in a try/catch ,但我想还有更好的方法.. :-) )

有人可以帮我记录这个错误和类似的未捕获错误吗?

最佳答案

进程指的是 Node 。尝试在您的 index.js 中添加一个 unhandledRejection 检查或使用 proper winston exception handling :

"use strict";

const winston = require('winston');

const myFormat = winston.format.printf(info => {
    return `${info.timestamp} ${info.level}: ${info.message}`;
});
const logger = winston.createLogger({
    level: "debug",
    format: winston.format.combine(winston.format.timestamp(), myFormat),  // winston.format.json(),
    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' })
    ],
    exitOnError: false,
    // handleExceptions: true // Otherwise, you should use this in options.
});

process.on('unhandledRejection', (reason, promise) => {
    logger.debug(reason);
});

process.on('uncaughtException', (err) => {
    logger.debug(err);
});

但是,请在与 index.js 文件相同的目录中查看 package.json 文件。在 dependencies 部分查找 node-pool 并将其版本更改为 "node-pool": "^3.4.2"因为你描述的问题很可能在 3.1.8 版本中得到修复。

关于javascript - 如何在 Winstonjs 记录器中记录未处理的超时错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51916061/

相关文章:

混合移动应用程序中的 JavaScript 安全风险

python - 良好的性能

记录到 Amazon S3

java - 如何访问 Azure 的 Tomcat 日志以进行 Java 应用服务部署

javascript - 如何处理html5的canvas图片加载异步?

javascript - 查找距复杂多边形中的点设定距离内的区域

node.js - mongodb 无法连接到服务器 [127.0.0.1 :27017] on first connect

javascript - 尝试在 mysql 中存储特定的 json 键时出错

node.js - 无法 `gulp install` ,导致 `Cannot read property ' apply' of undefined`

javascript - 如何在事件总线上存储 'global var'