winston + PM2记录uncaughtException两次

时间:2017-06-08 16:25:08

标签: node.js logging pm2 winston

我正在使用winston作为我的NodeJS应用程序,使用结构化日志记录格式(JSON),稍后我将处理并通过Filebeat发送到Logstash。

根据PM2和12factor.net的建议,我正在使用winston.transports.Console传输进行记录,并让PM2处理stdout中的stderrecosystem.json

在我的logger.js模块中,我有以下内容:

"use strict";
const winston = require("winston");
// Remove logging on console
winston.remove(winston.transports.Console);
// env
const env = process.env.NODE_ENV || 'development';
const isDev = env === "development";
// The default Console transport
const defaultLogLevel = isDev ? "debug" : "info";

const consoleTransport = new winston.transports.Console({
    level: defaultLogLevel,
    colorize: true,
    json: true,
    timestamp: true,
    exceptionsLevel: "error",
    prettyPrint: false,
    handleExceptions: true,
    humanReadableUnhandledException: false,
    exitOnError: true
});

const defaultOptions = {
    transports: [
    consoleTransport
    ]
};


module.exports = function(options)
{
    let initOpts = defaultOptions;
    if(options)
    {
        Object.assign(initOpts, options);
    }
    const logger = new winston.Logger(initOpts);
    // suppress any logger errors
    logger.emitErrs = false;
    winston.handleExceptions(consoleTransport);

    return logger;
};

在我的主应用程序文件server.js中,我运行此代码来模拟一个未处理的异常被抛出,需要一个假依赖,但未找到:

"use strict";
var log         = require('./logger')({name: "api-messages"});
log.info("some info message");
log.error("some error message");
var notFound = require("someInexistentPackage");

最后,在我的PM2 ecosystem.json中,我有以下内容:

{
  "apps":
  [
    {
      "name"            : "my-app",
      "script"          : "server.js",
      "max_restarts"    : 1,
      "error_file"      : "~/.pm2/logs/my-app.log",
      "out_file"        : "~/.pm2/logs/my-app.log",
      "merge_logs"      : true

    }
  ]
}

当我运行pm2 start ecosystem.json时,日志文件的内容如下:

{
  "level": "info",
  "message": "some info message",
  "timestamp": "2017-06-08T16:10:05.967Z"
}
{
  "level": "error",
  "message": "some error message",
  "timestamp": "2017-06-08T16:10:05.968Z"
}
Error: Cannot find module 'someInexistentPackage'
    at Function.Module._resolveFilename (module.js:470:15)
    at Function.Module._load (module.js:418:25)
    at Module.require (module.js:498:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/Users/mps/Sites/my-app/server.js:10:20)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
{
  "date": "Thu Jun 08 2017 13:10:05 GMT-0300 (-03)",
  "process": {
    "pid": 50862,
    "uid": 501,
    "gid": 20,
    "cwd": "/Users/mps/Sites/my-app",
    "execPath": "/Users/mps/.nvm/versions/node/v7.10.0/bin/node",
    "version": "v7.10.0",
    "argv": [
      "/Users/mps/.nvm/versions/node/v7.10.0/bin/node",
      "/Users/mps/.nvm/versions/node/v7.10.0/lib/node_modules/pm2/lib/ProcessContainerFork.js"
    ],
    "memoryUsage": {
      "rss": 36724736,
      "heapTotal": 9879552,
      "heapUsed": 6309680,
      "external": 63983
    }
  },
  "os": {
    "loadavg": [
      2.49560546875,
      2.228515625,
      2.0205078125
    ],
    "uptime": 329559
  },
  "trace": [
    {
      "column": 15,
      "file": "module.js",
      "function": "Module._resolveFilename",
      "line": 470,
      "method": "_resolveFilename",
      "native": false
    },
    {
      "column": 25,
      "file": "module.js",
      "function": "Module._load",
      "line": 418,
      "method": "_load",
      "native": false
    },
    {
      "column": 17,
      "file": "module.js",
      "function": "Module.require",
      "line": 498,
      "method": "require",
      "native": false
    },
    {
      "column": 19,
      "file": "internal/module.js",
      "function": "require",
      "line": 20,
      "method": null,
      "native": false
    },
    {
      "column": 20,
      "file": "/Users/mps/Sites/my-app/server.js",
      "function": null,
      "line": 10,
      "method": null,
      "native": false
    },
    {
      "column": 32,
      "file": "module.js",
      "function": "Module._compile",
      "line": 571,
      "method": "_compile",
      "native": false
    },
    {
      "column": 10,
      "file": "module.js",
      "function": "Module._extensions..js",
      "line": 580,
      "method": ".js",
      "native": false
    },
    {
      "column": 32,
      "file": "module.js",
      "function": "Module.load",
      "line": 488,
      "method": "load",
      "native": false
    },
    {
      "column": 12,
      "file": "module.js",
      "function": "tryModuleLoad",
      "line": 447,
      "method": null,
      "native": false
    },
    {
      "column": 3,
      "file": "module.js",
      "function": "Module._load",
      "line": 439,
      "method": "_load",
      "native": false
    }
  ],
  "stack": [
    "Error: Cannot find module 'someInexistentPackage'",
    "    at Function.Module._resolveFilename (module.js:470:15)",
    "    at Function.Module._load (module.js:418:25)",
    "    at Module.require (module.js:498:17)",
    "    at require (internal/module.js:20:19)",
    "    at Object.<anonymous> (/Users/mps/Sites/my-app/server.js:10:20)",
    "    at Module._compile (module.js:571:32)",
    "    at Object.Module._extensions..js (module.js:580:10)",
    "    at Module.load (module.js:488:32)",
    "    at tryModuleLoad (module.js:447:12)",
    "    at Function.Module._load (module.js:439:3)"
  ],
  "level": "error",
  "message": "uncaughtException: Cannot find module 'someInexistentPackage'",
  "timestamp": "2017-06-08T16:10:05.972Z"
}

如您所见,PM2正在将未捕获的Exception添加到日志文件中,即此代码:

Error: Cannot find module 'someInexistentPackage'
    at Function.Module._resolveFilename (module.js:470:15)
    at Function.Module._load (module.js:418:25)
    at Module.require (module.js:498:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/Users/mps/Sites/my-app/server.js:10:20)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)

这会破坏我的日志的格式/结构,因为它是纯文本而不是我从winston收到的格式化JSON。

我确信这是PM2问题/配置错误,因为如果我运行node server.js,控制台输出只是JSON对象。

我是否必须在PM2 ecosystem.json文件中设置特定属性?

如何告诉PM2忽略Node的uncaughtException

我提出的唯一替代方案(我尚未尝试过)是将我的winston传输更改为winston.transports.File,将所有日志重定向到stdout并告诉PM2将stderr保存在另一个文件中,但我宁愿坚持使用控制台方法。

1 个答案:

答案 0 :(得分:2)

阻止PM2记录uncaughtException的一种方法是将--no-pmx作为标记传递,即pm2 start --no-pmx myapp.js或将pmx: false添加到ecosystem.json文件

这会禁止PMX注入PM2,PM2负责记录此uncaughtException