简体   繁体   English

使用 winston 和 morgan 登录中间件

[英]Loggin middleware with winston and morgan

I create a logger middleware with using winston and morgan like below:我使用winston和morgan创建了一个记录器中间件,如下所示:

  const format = winston.format.combine(
    winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss:ms' }),
    winston.format.printf(
      (info) => `${info.timestamp} ${info.level}: ${info.message}`,
    ),
  )

  const transports = [
    new winston.transports.Console(),
    new winston.transports.File({
      filename: 'logs/error.log',
      level: 'error',
    }),
    new winston.transports.File({ filename: 'logs/all.log' }),
  ]

  const logger = winston.createLogger({
    format,
    level: "info",
    transports
  })

  const stream = {
    write: (message) => logger.log(message),
  };

  const morganMiddleware = morgan(
    ":remote-addr :method :url :status :res[content-length] - :response-time ms",
    { stream }
  );

  app.use(morganMiddleware);

But when my start is running and receive first log, winston throw me this error:但是当我开始运行并接收到第一个日志时,winston 向我抛出了这个错误:

level[LEVEL] = level.level;
                   ^

TypeError: Cannot create property 'Symbol(level)' on string '::1 GET /api/amr/applications?order=ASC&sortBy=application.lastIncidentAt 304 - - 380.459 ms

can someone tell me what is wrong with my winston/morgan configuration?有人可以告诉我我的 winston/morgan 配置有什么问题吗?

thanks for any help!谢谢你的帮助!

Winston logger expects two arguments. Winston 记录器需要两个 arguments。 So instead of this:所以代替这个:

const stream = {
  write: (message) => logger.log(message),
};

Do this:做这个:

const stream = {
  write: (message) => logger.log('info', message),
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM