【问题标题】:How to log Unhandled TimeoutError in Winstonjs logger?如何在 Winstonjs 记录器中记录 Unhandled TimeoutError?
【发布时间】:2019-01-25 17:11:59
【问题描述】:

我有一个节点应用程序,我在其中使用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 中收到以下错误,该错误来自我用来写入我的 MySQL 数据库的Sequelizejs

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,但我想还有更好的方法.. :-))

谁能帮我记录这个和类似的未捕获的错误?

【问题讨论】:

  • 所以你的nodejs程序被那个错误杀死了?什么在运行你的 nodejs 程序?下午2点?
  • @Cétia - 目前我只是手动运行它:node index.js

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


【解决方案1】:

进程指的是节点。尝试在 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 文件。在依赖项部分查看 node-pool 并将其版本更改为 "node-pool": "^3.4.2" 因为您描述的问题很可能在 3.1.8 版本中得到修复。

【讨论】:

  • 感谢您的建议。但是我应该把它放在什么过程中呢?我不是在运行服务器,而是在运行 setInterval() 调用的集合和一个 websocket,其中包含由收到的消息产生的许多操作。知道如何使用它吗?
  • 整件事只有一个文件:index.js。但我没有任何具体的过程。如果我只有一个setInterval(),里面有一些代码怎么办。那我会把它放在哪里呢?
  • 我知道你猜不到我的代码是什么样子的。但是,如果它只是一堆带有某些功能的setInterval() 怎么办。在这种情况下,没有将其挂钩的过程。在这种情况下你会怎么做?
  • 进程是node,我猜你是用node index.js开始你的代码?
  • 啊啊啊啊,刚才我意识到process 是一个全局对象,而不是我需要自己创建的某个类的实例化!那应该可以解决了,谢谢!
猜你喜欢
  • 2018-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-06
  • 2021-10-27
  • 1970-01-01
相关资源
最近更新 更多