winston:尝试写入没有传输的日志 - 使用默认记录器

ekn*_*tmz 8 javascript node.js winston

我按照教程在我的快递应用程序中设置了winston(2.x)默认记录器.当更新到当前版本的winston(3.0.0)时,我遇到了添加传输的问题.我已经关注了最新的文档,但仍然在控制台中收到通知,并且根本没有创建日志文件:

[winston]尝试写没有传输的日志

logging.js

const winston = require('winston');

module.exports = function () {

  const files = new winston.transports.File({ filename: 'logfile.log' });
  const myconsole = new winston.transports.Console();

  winston.add(myconsole);
  winston.add(files);

}
Run Code Online (Sandbox Code Playgroud)

index.js

const winston = require('winston');
...

require('./logging');
winston.info("Give some info");
Run Code Online (Sandbox Code Playgroud)

[winston]尝试写没有传输的日志{"message":"提供一些信息","级别":"info"}

我究竟做错了什么?

dav*_*ewy 8

在winston 3中,您需要创建一个logger对象,然后添加transports.

Winston 3有很多例子,但为了适应自述文件,可以这样做:

const logger = winston.createLogger({
  level: 'info',
  format: winston.format.json(),
  transports: [
    new winston.transports.Console(),
    new winston.transports.File({ filename: 'logfile.log' })
  ]
});

logger.info('it works!!');
Run Code Online (Sandbox Code Playgroud)


小智 8

如果您想在 winston v3 中使用默认记录器,那么您只需在主文件中添加这段代码

const winston = require('winston')

winston.add(new winston.transports.File({ filename: 'logfile.log' }))
Run Code Online (Sandbox Code Playgroud)


小智 5

我也有类似的问题。如果我没记错的话,我必须在我的 index.js 中将需求作为函数调用。

require('./logging')();
Run Code Online (Sandbox Code Playgroud)