Further Reading 延伸阅读
Events and Callbacks in Winston winston的事件和回调
Each instance of winston.Logger is also an instance of an EventEmitter. A log event will be raised each time a transport successfully logs a message:
logger.on('logging', function (transport, level, msg, meta) {
// [msg] and [meta] have now been logged at [level] to [transport]
}); logger.info('CHILL WINSTON!', { seriously: true });
Working with multiple Loggers in winston 在winston中使用多个logger
Often in larger, more complex applications it is necessary to have multiple logger instances with different settings. Each logger is responsible for a different feature area (or category). This is exposed in winston
in two ways: through winston.loggers
and instances of winston.Container
. In fact, winston.loggers
is just a predefined instance of winston.Container
:
定义多个日志文件
1.利用 add方法来增加文件
var winston = require('winston'); //
// Configure the logger for `category1`
//
winston.loggers.add('category1', {
console: {
level: 'silly',
colorize: true,
label: 'category one'
},
file: {
filename: '/path/to/some/file'
}
}); //
// Configure the logger for `category2`
//
winston.loggers.add('category2', {
couchdb: {
host: '127.0.0.1',
port: 5984
}
});
Filters and Rewriters 过滤日志和重写
Filters allow modifying the contents of log messages, and Rewriters allow modifying the contents of log meta e.g. to mask data that should not appear in logs.
Filters允许修改日志消息的内容,并且Rewriter允许修改日志元数据的内容。 以屏蔽不应出现在日志中的数据。
Both filters and rewriters are simple Arrays of functions which can be provided when creating a new winston.Logger(options)
. e.g.:
var logger = new winston.Logger({
rewriters: [function (level, msg, meta) { /* etc etc */ }],
filters: [function (level, msg, meta) { /* etc etc */ }] //具体怎么定义?????
})
Like any Array they can also be modified at runtime with no adverse side-effects to the winston
internals.
logger.filters.push(function(level, msg, meta) {
return meta.production
? maskCardNumbers(msg)
: msg;
}); logger.info('transaction with card number 123456789012345 successful.');
This may result in this output:
info: transaction with card number 123456****2345 successful.
这个方法是岁信息处理后,返回的的值,然后在存入日志文件中
Adding Custom Transports 添加自定义Transports
Adding a custom transport is actually pretty easy. All you need to do is accept a couple of options, set a name, implement a log() method, and add it to the set of transports exposed by winston.
添加自定义传输实际上很容易。 所有你需要做的是接受几个选项,设置name,实现log()方法,并将其添加到winston公开的transports。
var util = require('util'),
winston = require('winston'); var CustomLogger = winston.transports.CustomLogger = function (options) {
//
// Name this logger
//
this.name = 'customLogger'; //
// Set the level from your options
//
this.level = options.level || 'info'; //
// Configure your storage backing as you see fit
//
}; //
// Inherit from `winston.Transport` so you can take advantage
// of the base functionality and `.handleExceptions()`.
//
util.inherits(CustomLogger, winston.Transport); CustomLogger.prototype.log = function (level, msg, meta, callback) {
//
// Store this message and metadata, maybe use some custom logic
// then callback indicating success.
//
callback(null, true);
};
Custom Log Format 日志格式化
var logger = new (winston.Logger)({
transports: [
new (winston.transports.Console)({
timestamp: function() {
return Date.now();
},
formatter: function(options) {
// Return string will be passed to logger.
return options.timestamp() +' '+ options.level.toUpperCase() +' '+ (undefined !== options.message ? options.message : '') +
(options.meta && Object.keys(options.meta).length ? '\n\t'+ JSON.stringify(options.meta) : '' );
}
})
]
});
logger.info('Data to log.');