[英]Multiple log files with Winston?
We'd like to use Winston for our logging in Node.js. But, we can't figure out how to have two log files: one for just errors, and one for everything else.我们想在 Node.js 中使用 Winston 进行日志记录。但是,我们不知道如何拥有两个日志文件:一个仅用于错误,一个用于所有其他内容。
Doing this the naive way doesn't work, however: adding multiple winston.transports.File
transports gives an error.但是,以简单的方式执行此操作是行不通的:添加多个winston.transports.File
传输会出错。
Others have run into this problem, with vague hints of a solution, but no real answer .其他人遇到过这个问题,有解决方案的模糊提示,但没有真正的答案。
Any ideas?有任何想法吗?
Unfortunately, the patch that pesho mentioned seems to be still not included in the official version (see stephenbeeson's comment in the pull request #149 ).不幸的是,pesho 提到的补丁似乎仍未包含在正式版本中(请参阅pull request #149中 stephenbeeson 的评论)。
So, I used a workaround instead.所以,我改用了一种解决方法。 As winston compares the name attributes, you can fool it by defining the name yourself:由于 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
Maybe not elegant, but at least it works.也许不优雅,但至少它有效。
In the meantime, you can implement a rudimentary wrapper using the same interface like so同时,您可以像这样使用相同的接口实现一个基本的包装器
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;
This will cover the basic winston API. could be extended for metadata and so on...这将涵盖基本的 winston API。可以扩展元数据等等......
I just sent a pull request that allows using multiple File transports in one logger.我刚刚发送了一个拉取请求,允许在一个记录器中使用多个文件传输。 https://github.com/flatiron/winston/pull/149 https://github.com/flatiron/winston/pull/149
It is already merged into flatiron/winston.它已经合并到 flatiron/winston 中。
You can also use my forked repo: https://github.com/pdobrev/winston你也可以使用我的分叉仓库:https://github.com/pdobrev/winston
You just need to give the transport a custom name
property so you don't have a collision:您只需要为传输提供自定义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 })
]
});
You can read more about multiple transports in the docs: https://github.com/winstonjs/winston#multiple-transports-of-the-same-type您可以在文档中阅读有关多种传输的更多信息: https://github.com/winstonjs/winston#multiple-transports-of-the-same-type
This feature is now officially supported in Winston and is addressed in the README here此功能现已在 Winston 中得到正式支持,并在此处的自述文件中进行了说明
Code example:代码示例:
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()
}));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.