nodejs日志winston

//npm install winston
//npm install uuid
const uuid = require('uuid');
const winston = require('winston');

const { createLogger, format, transports } = require('winston');
const { combine, timestamp, label, printf } = format;

const myFormat = printf(info => {
  return `${info.timestamp} [${info.label}] ${info.level}: ${info.message}`;
});

function newLogger(loglabel = uuid.v4()){
    const logger = createLogger({
      format: combine(
        label({ label: loglabel}),
        timestamp(),
        myFormat
      ),
      transports: [new transports.Console()]
    });
    return logger;
}
/*
const logger = winston.createLogger({
  level: 'info',
  format: winston.format.json(),
  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()
  }));
}*/

module.exports = newLogger;

猜你喜欢

转载自blog.csdn.net/zhoujiaping123/article/details/80285703
今日推荐