我正在尝试使用Winston为我的快速服务器设置访问日志和错误日志,但我似乎做错了什么 .
这是我对配置文件的尝试:
const winston = require('winston'),
fs = require('fs');
const tsFormat = () => (new Date()).toLocaleTimeString();
winston.loggers.add('errorLog', {
file: {
filename: '<path>/errors.log', //<path> is replaced by the
timestamp: tsFormat, //absolute path of the log
level: 'info'
}
});
winston.loggers.add('accessLog', {
file: {
filename: '<path>/access.log', //same as before
timestamp: tsFormat,
level: 'info'
}
});
这就是我将其包含在我的其他文件中的方式:
var winston = require('winston'),
accessLog = winston.loggers.get('accessLog'),
errorLog = winston.loggers.get('errorLog');
这似乎在我看来它遵循文档(https://github.com/winstonjs/winston/tree/2.4.0#working-with-multiple-loggers-in-winston)但我在尝试登录时遇到此错误:
[winston] Attempt to write logs with no transports {"message":"pls","level":"info"}
[winston] Attempt to write logs with no transports {"message":"Bad request: undefined","level":"warn"}
任何帮助将不胜感激,我已经相当困难了几天了 .
2 回答
我尝试这样的事情,将所有与 Logger 相关的东西放入模块logger.js:
logger.js
然后在index.js中测试:
index.js
您应该看到如下日志行:
errors.log:
access.log的:
EDIT
1用于开发目的的Logger控制台日志记录:
logger.js
app.js