use*_*142 10 node.js npm express winston
我正在尝试使用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'
}
});
Run Code Online (Sandbox Code Playgroud)
这就是我将其包含在我的其他文件中的方式:
var winston = require('winston'),
accessLog = winston.loggers.get('accessLog'),
errorLog = winston.loggers.get('errorLog');
Run Code Online (Sandbox Code Playgroud)
在我看来,它似乎遵循文档(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"}
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激,我已经相当困难了几天了.
我尝试这样的事情,将所有与记录器相关的东西放入模块logger.js:
logger.js
var winston = require('winston');
var path = require('path');
// Set this to whatever, by default the path of the script.
var logPath = __dirname;
const tsFormat = () => (new Date().toISOString());
const errorLog = winston.createLogger({
transports: [
new winston.transports.File({
filename: path.join(logPath, 'errors.log'),
timestamp: tsFormat,
level: 'info'})
]
});
const accessLog = winston.createLogger({
transports: [
new winston.transports.File({
filename: path.join(logPath, 'access.log'),
timestamp: tsFormat,
level: 'info'})
]
});
module.exports = {
errorLog: errorLog,
accessLog: accessLog
};
Run Code Online (Sandbox Code Playgroud)
然后在index.js中测试:
index.js
var logger = require('./logger');
logger.errorLog.info('Test error log');
logger.accessLog.info('Test access log');
Run Code Online (Sandbox Code Playgroud)
您应该看到如下日志行:
errors.log:
{"level":"info","message":"Test access log","timestamp":"2018-03-14T07:51:11.185Z"}
Run Code Online (Sandbox Code Playgroud)
access.log的:
{"level":"info","message":"Test error log","timestamp":"2018-03-14T07:51:11.182Z"}
Run Code Online (Sandbox Code Playgroud)
编辑
在温斯顿的最后一个版本,
new (winston.Logger)
已被winston.createLogger
(https://github.com/bithavoc/express-winston/issues/175)取代
归档时间: |
|
查看次数: |
7208 次 |
最近记录: |