Winston: Attempt to write logs with no transports

后端 未结 3 1832
不思量自难忘°
不思量自难忘° 2021-02-20 10:24

I\'m trying to set up an access log and an error log for my express server using Winston, but I seem to be doing something wrong.

Here is my attempt at a config file:

3条回答
  •  悲&欢浪女
    2021-02-20 10:45

    1 Logger + console logging for development purpose:

    logger.js

    var logPath = '';
    var log_level = '';
    
    const log = winston.createLogger({
      level: log_level,
      format: winston.format.json(),
      transports: [
        new winston.transports.File({
          filename: path.join(logPath, 'access.log'),
          timestamp: tsFormat,
          level: log_level
        }),
        new winston.transports.File({
          filename: path.join(logPath, 'error.log'),
          timestamp: tsFormat,
          level: 'error'
        }),
      ]
    });
    
    if (process.env.NODE_ENV !== 'production') {
      log.add(new winston.transports.Console({
        format: winston.format.simple()
      }));
    }
    
    module.exports = {
      log: log
    };
    

    app.js

    const logger = require('./logger');
    
    logger.log.info("starting application..");
    

提交回复
热议问题