如何在 NodeJs 中使用 winston?

How to use winston in NodeJs?

我正在尝试创建一个示例 nodeJs 程序来使用和理解 nodeJs 的 winston 库,
我有一个现有项目的示例代码,我正在尝试创建一个示例代码示例,
该代码包含 winston 配置和使用不同日志级别打印日志的简单测试功能。
我正在使用节点版本 4.6.2,我知道它很旧但是,现有的应用程序使用的是相同的,因此我想了解相同版本的实现,
该示例由以下代码组成,一个日志文件夹,我希望在其中打印日志。

以下是我正在尝试的示例代码 运行 -

var winston = require('winston');
var envData = require('dotenv').config({path: 'server.env'})
require('winston-daily-rotate-file');

var levelLog = 'debug';
var winstonTransports = [];

const logDTFormat = () => (new Date().toFormat('DD MMM YYYY HH24:MI:SS'));

// Winston Rotate File Logs
var transportDailyRotate = new (winston.transports.DailyRotateFile)({
    filename: './logs/hrfid_app_',
    datePattern: 'yyyy-MM-dd.log',
    prepend: false,
    level: levelLog,
    timestamp: function() {
    var dateTime = new Date(Date.now());
        return dateTime.toFormat('DD/MM/YYYY HH24:MI:SS');
    }
});

winstonTransports.push(transportDailyRotate);

// Winston Console Log 
var trasportConsole = new (winston.transports.Console)({
    timestamp: logDTFormat,
    colorize: true,
    level: levelLog
});

winstonTransports.push(trasportConsole);

// Winston Config
winston.configure({
    level: levelLog,
    transports: winstonTransports
});

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()
    }));
  }

// Validate Server Configuration
if (envData.error) {
    winston.error('[APP-CONFIG]:', JSON.stringify(envData.error))
    winston.error('[APP-CONFIG]:', 'Error on Server Configuration')
    return
}

var test = function(){
  winston.error("ERROR log");
  winston.info("INFO log");
  winston.debug("DEBUG log");
}
test();

我收到以下错误,
有人可以帮助修复我的示例代码吗?

升级到节点 6 或 8。

最新的 winston 至少需要 node 6.4

https://github.com/winstonjs/winston/blob/c42ab7fdc51b88db180a7dd90c52ce04ddd4e054/package.json#L69

或者通过删除现有版本并执行 npm install winston@2.4.4

来使用旧版本的 winston