温斯顿:尝试在没有传输的情况下写入日志 - 使用默认记录器
winston: Attempt to write logs with no transports - using default logger
我按照教程在我的 Express 应用程序中设置了 winston (2.x) 默认记录器。更新到当前版本的 winston (3.0.0) 时,我在添加传输时遇到问题。我已经按照 latest docs 进行操作,但我仍然在控制台中收到通知,并且根本没有创建任何日志文件:
[winston] Attempt to write logs with no transports
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);
}
index.js
const winston = require('winston');
...
require('./logging');
winston.info("Give some info");
[winston] Attempt to write logs with no transports
{"message":"Give some info","level":"info"}
我做错了什么?
在 winston 3 中您需要创建一个 logger
对象,然后向其添加 transport
s。
Winston 3 有很多examples, but to adapt from the readme,做这样的事情:
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!!');
我也有类似的问题。如果我没记错的话,我不得不在 index.js.
中将需求作为函数调用
require('./logging')();
如果您想在 winston v3 中使用默认记录器,那么您只需在主文件中添加这段代码
const winston = require('winston')
winston.add(new winston.transports.File({ filename: 'logfile.log' }))
我按照教程在我的 Express 应用程序中设置了 winston (2.x) 默认记录器。更新到当前版本的 winston (3.0.0) 时,我在添加传输时遇到问题。我已经按照 latest docs 进行操作,但我仍然在控制台中收到通知,并且根本没有创建任何日志文件:
[winston] Attempt to write logs with no transports
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);
}
index.js
const winston = require('winston');
...
require('./logging');
winston.info("Give some info");
[winston] Attempt to write logs with no transports {"message":"Give some info","level":"info"}
我做错了什么?
在 winston 3 中您需要创建一个 logger
对象,然后向其添加 transport
s。
Winston 3 有很多examples, but to adapt from the readme,做这样的事情:
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!!');
我也有类似的问题。如果我没记错的话,我不得不在 index.js.
中将需求作为函数调用require('./logging')();
如果您想在 winston v3 中使用默认记录器,那么您只需在主文件中添加这段代码
const winston = require('winston')
winston.add(new winston.transports.File({ filename: 'logfile.log' }))