我们想使用Winston来登录Node.js.但是,我们无法弄清楚如何有两个日志文件:一个用于错误,一个用于其他所有文件。
这样做的天真方式不起作用,但是:添加多个winston.transports.File
传输会产生错误。
Others have run into this problem, with vague hints of a solution, but no real answer
有什么想法吗?
答案 0 :(得分:18)
不幸的是,pesho提到的补丁似乎仍未包含在官方版本中(请参阅pull request #149中的stephenbeeson的评论)。
所以,我使用了一种解决方法。当winston比较名称属性时,您可以通过自己定义名称来欺骗它:
winston = require 'winston'
logger = new winston.Logger
transports: [
new winston.transports.File
name: 'file#debug'
level: 'debug'
filename: '/tmp/debug.log'
new winston.transports.File
name: 'file#error'
level: 'error'
filename: '/tmp/error.log'
]
logger.error 'error' # both logs
logger.debug 'debug' # on debug log
也许不优雅,但至少它有效。
答案 1 :(得分:10)
与此同时,你可以使用相同的界面实现一个基本的包装器
var winston = require('winston');
var configs = require('./env.js');
var debug = new winston.Logger({
levels: {
debug: 0
},
transports: [
new (winston.transports.File)({ filename: configs.PATH_TO_LOG, level: 'debug'}),
new (winston.transports.Console)({level: 'debug'})
]
});
var info = new winston.Logger({
levels: {
info: 1
},
transports: [
new (winston.transports.File)({ filename: configs.PATH_TO_LOG, level: 'info'}),
new (winston.transports.Console)({level: 'info'})
]
});
var warn = new winston.Logger({
levels: {
warn: 2
},
transports: [
new (winston.transports.File)({ filename: configs.PATH_TO_LOG, level: 'warn'}),
new (winston.transports.Console)({level: 'warn'})
]
});
var error = new winston.Logger({
levels: {
error: 3
},
transports: [
new (winston.transports.File)({ filename: configs.PATH_TO_LOG, level: 'error'}),
new (winston.transports.Console)({level: 'error'})
]
});
var exports = {
debug: function(msg){
debug.debug(msg);
},
info: function(msg){
info.info(msg);
},
warn: function(msg){
warn.warn(msg);
},
error: function(msg){
error.error(msg);
},
log: function(level,msg){
var lvl = exports[level];
lvl(msg);
}
};
module.exports = exports;
这将涵盖基本的winston API。可以扩展元数据等等......
答案 2 :(得分:7)
我刚刚发送了一个允许在一个记录器中使用多个文件传输的pull请求。 https://github.com/flatiron/winston/pull/149
它已经合并到flatiron / winston。
你也可以使用我的分叉回购: https://github.com/pdobrev/winston
答案 3 :(得分:5)
您只需要为传输提供自定义name
属性,这样就不会发生冲突:
const logger = new (winston.Logger)({
transports: [
new (winston.transports.Console)(),
new (winston.transports.File)({ name: 'text', filename: logFile, json: false }),
new (winston.transports.File)({ name: 'json', filename: logFileJson })
]
});
您可以在文档中详细了解多个传输:https://github.com/winstonjs/winston#multiple-transports-of-the-same-type
答案 4 :(得分:2)
Winston现在正式支持此功能,并且自述文件here
中已解决代码示例:
const logger = winston.createLogger({
level: 'info',
format: winston.format.json(),
defaultMeta: { service: 'user-service' },
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()
}));
}