无法使用 winston 记录未处理的拒绝

时间:2021-04-26 21:19:18

标签: winston

我正在处理日志文件,因此我使用的是 winston 版本 3.3.3 。 我有一个用于调用记录器的函数。出于某种原因,我无法登录拒绝承诺。有人能指出我正确的方向吗?

function DevLogger() {
  const logFormat = printf(({ level, message, timestamp, stack }) => {
    return `${timestamp} ${level}: ${stack || message}`;
  });

  let fileProperty = {
    maxsize: 5242880,
    maxFiles: 5,
    colorize: false,
    level: "error",
  };

  let options = {
    uncaughtException: {
      filename: config.get("uncaughtExceptionHandler"),
      handleExceptions: true,
    },
    uncaughtRejection: {
      filename: config.get("rejectionHandler"),
      handleRejections: true,
    },
    console: {
      format: logFormat,
      colorize: true,
      prettyPrint: true,
    },
    database: {
      level: "info",
      collection: "exceptionLog",
      db: config.get("connectionstring"),
      options: { useUnifiedTopology: true },
      metaKey: "metadata",
    },
  };

  return createLogger({
    format: combine(
      timestamp(),
      errors({ stack: true }),
      logFormat,
      json(),
      metadata()
    ),
    transports: [
      new transports.File(
        Object.assign(options.uncaughtException, fileProperty)
      ),
      new transports.MongoDB(options.database),
    ],
    rejectionHandlers: [
    new transports.File(
       Object.assign(options.uncaughtRejection, fileProperty)
      ),
    ],
    exitOnError: false,
  });
}

module.exports = { buildDevLogger, error};

在索引文件中,我只是抛出了一个承诺拒绝错误。代码如下

process.on("unhandledRejection", (ex) => {
  console.log("UNHANDLED REJECTION DETECTED!!!!");
  logger.error(ex.message, ex);
  process.exit(1);
});

const p = Promise.reject(new Error("Something failed!!!"));
p.then(() => console.log("Done"));

0 个答案:

没有答案
相关问题