代码之家  ›  专栏  ›  技术社区  ›  kramer65

如何在Winstonjs logger中记录未处理的TimeoutError?

  •  1
  • kramer65  · 技术社区  · 6 年前

    我有一个节点应用程序 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.log logs/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)
    

    这个错误只出现在stdout中(当手动运行我的程序时),而在 combined.log 或者 exceptions.log

    (我想我可以 wrap my whole code in a try/catch

    有谁能帮我记录下这些和类似的意外错误吗?

    1 回复  |  直到 6 年前
        1
  •  1
  •   Gillsoft AB    6 年前

    这个 过程 是指 节点 . 尝试在index.js中添加未处理的弹出检查,或使用 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);
    });
    

    但是,在package.json文件中查找index.js文件所在的目录。在“依赖关系”部分中查找节点池,并将其版本更改为“节点池”:“^3.4.2”,因为您描述的问题很可能已在3.1.8版本中修复。