且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

日志库 winston 的学习笔记 - 创建一个使用 winston 的 Node.js 应用

更新时间:2022-06-05 17:59:28

const winston = require('winston');
 
const logger = winston.createLogger({
  level: 'info',
  format: winston.format.json(),
  defaultMeta: { service: 'user-service' },
  transports: [
    //
    // - Write all logs with level `error` and below to `error.log`
    // - Write all logs with level `info` and below to `combined.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(),
  }));
}

日志库 winston 的学习笔记 - 创建一个使用 winston 的 Node.js 应用

日志库 winston 的学习笔记 - 创建一个使用 winston 的 Node.js 应用

日志库 winston 的学习笔记 - 创建一个使用 winston 的 Node.js 应用