Multiple log files with Winston?

后端 未结 5 1482
故里飘歌
故里飘歌 2020-12-14 09:36

We\'d like to use Winston for our logging in Node.js. But, we can\'t figure out how to have two log files: one for just errors, and one for everything else.

Doing th

5条回答
  •  情深已故
    2020-12-14 10:15

    This feature is now officially supported in Winston and is addressed in the README here

    Code example:

    const logger = winston.createLogger({
      level: 'info',
      format: winston.format.json(),
      defaultMeta: { service: 'user-service' },
      transports: [
        //
        // - Write to all logs with level `info` and below to `combined.log` 
        // - Write all logs error (and below) to `error.log`.
        //
        new winston.transports.File({ filename: 'error.log', level: 'error' }),
        new winston.transports.File({ filename: 'combined.log' })
      ]
    });
    
    //
    // If we're not in production then log to the `console` with the format:
    // `${info.level}: ${info.message} JSON.stringify({ ...rest }) `
    // 
    if (process.env.NODE_ENV !== 'production') {
      logger.add(new winston.transports.Console({
        format: winston.format.simple()
      }));
    }
    

提交回复
热议问题