【问题标题】:How to use winston in NodeJs?如何在 NodeJs 中使用winston?
【发布时间】:2019-04-14 01:31:07
【问题描述】:

我正在尝试创建一个示例 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();

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

【问题讨论】:

  • 如果您能描述问题,那就太好了。您是否收到任何错误消息?你的预期输出是什么?你的实际输出是多少?
  • @yeya,用我面临的错误更新了问题。
  • 你的节点版本是多少?运行node -v
  • 我使用的是 v4.6.2

标签: node.js winston


【解决方案1】:

升级到节点 6 或 8。

最新的 winston 至少需要节点 6.4

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

或者通过删除现有的winston来使用旧版本的winston并执行npm install winston@2.4.4

【讨论】:

  • 实际上现有的应用程序在 v4.6.2 上运行,这就是为什么我尝试做同样的事情:/
  • 试一试没问题,但是不行。您将需要为此重写 winston 库。尝试升级,也许您现有的应用程序也可以在 8 上运行良好。无论如何,您可以使用 nvm 在同一台机器上使用多个节点版本。
  • 是的,我使用的是 nvm,代码在 v10+ 上运行,但项目依赖于 v4.6.2,并且日志在项目中工作,为什么不使用这个简单的代码:/
  • 您不能在节点 4 中使用此 winston 库。它使用节点 4 不支持的 es6 语法。您可以降级到 winston 2 而不是 3,这将为您提供对节点 4 的支持。
猜你喜欢
  • 1970-01-01
  • 2020-06-27
  • 1970-01-01
  • 2014-08-30
  • 2013-01-28
  • 2013-01-09
  • 1970-01-01
  • 2016-10-16
  • 2019-10-21
相关资源
最近更新 更多