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 日志格式化

To specify custom log format you should set formatter function for transport. Currently supported transports are: Console, File, Memory. Options object will be passed to the format function. It's general properties are: timestamp, level, message, meta. Depending on the transport type may be additional properties.
 
要指定自定义日志格式,您应该为传输设置格式化函数。 当前支持的传输有:Console,File,Memory。 Options对象将被传递给format函数。 它的一般属性是:timestamp,level,message,meta。 根据传输类型可能有其他属性。
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.');
 

winston日志管理3的更多相关文章

  1. winston日志管理1

    Usage There are two different ways to use winston: directly via the default logger, or by instantiat ...

  2. winston日志管理2

    上次讲到 Exceptions  例外 Handling Uncaught Exceptions with winston 使用winston处理未捕获的异常(这个如果对于异步,我不是很喜欢用) 使用 ...

  3. winston 日志管理4

    配置File Transport winston.add(winston.transports.File, options) The File transport should really be t ...

  4. Nodejs日志管理包

    Nodejs日志管理工具包:log4js 和 winston 1.log4js的使用 1)package.json中加入依赖 "log4js":"~0.6.21" ...

  5. 第13章 Linux日志管理

    1. 日志管理 (1)简介 在CentOS 6.x中日志服务己经由rsyslogd取代了原先的syslogd服务.rsyslogd日志服务更加先进,功能更多.但是不论该服务的使用,还是日志文件的格式其 ...

  6. ABP(现代ASP.NET样板开发框架)系列之8、ABP日志管理

    点这里进入ABP系列文章总目录 基于DDD的现代ASP.NET开发框架--ABP系列之8.ABP日志管理 ABP是“ASP.NET Boilerplate Project (ASP.NET样板项目)” ...

  7. 【Java EE 学习 76 下】【数据采集系统第八天】【通过AOP实现日志管理】【日志管理功能分析和初步实现】

    一.日志管理相关分析 1.日志管理是一种典型的系统级别的应用,非常适合使用spring AOP实现. 2.使用日志管理的目的:对系统修改的动作进行记录,比如对权限.角色.用户的写操作.修改操作.删除操 ...

  8. ElasticSearch+NLog+Elmah实现Asp.Net分布式日志管理

    本文将介绍使用NLOG.Elmah结合ElasticSearch实现分布式日志管理. 一.ElasticSearch简介 ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布 ...

  9. Apache 日志管理,获取客户端端口号

    日志管理分类 日志文件是用户管理和监控 Apache 安全的非常好的第一手资料,它清晰地记录了客户端访问 Apache 服务器资源的每一条记录,以及在访问中出现的错误信息,可以这样说,Apache 可 ...

随机推荐

  1. 紧挨导航栏下的链接a标签失效

    在我编完网页测试的的时候,发现导航下的链接不能用,不出现小手的图表,而下面的相同的链接都能用.如下图所示 遇到的相同问题的另一个未完成的页面如图 先不谈论其原因,说一下我对第一个页面的解决办法,我在导 ...

  2. 程序中使用gc_enable() 和 gc_disable()开启和关闭

    在理解PHP垃圾回收机制(GC)之前,先了解一下变量的存储. php中变量存在于一个zval的变量容器中.结构如下: 类型 值 is_ref refcount zval中,除了存储变量的类型和值之外, ...

  3. RAID 容量计算器

    https://www.synology.com/zh-cn/support/RAID_calculator   磁盘阵列比较表   n/2 n/2 n n/2 安全性高 综合RAID 0/1优点,理 ...

  4. 2. Basic environment configuration

    网卡设置: Controller Node # The loopback network interface auto lo iface lo inet loopback   # The primar ...

  5. iOS 键盘的隐藏

     在 iOS开发中 最常用的 一些控件,如TextFiled 和 TextView,点击时会自动弹出键盘,但是隐藏操作需要我们自己来编码完成. 最常用的一种方法是,让TextFiled 和 TextV ...

  6. yii2.0安装创建应用shiyong 归档文件安装

    环境是wamp在本机开发 http://www.yiiframework.com/download/ Install from an Archive File Download one of the ...

  7. [学点经济]什么是SDR [the IMF's Special Drawing Rights (SDR) basket of currencies]

    思考题: 1.什么是SDR?能否用通俗的语言说明. 2.加入SDR对中国有什么好处?能否举1-3个实例说明. 3.加入SDR有没有坏处?能否举例说明. 4.近期关于SDR的新闻有哪些?中国外国的例子都 ...

  8. ecshop运行超过30秒超时的限制解决办法

    ecshop运行超过30秒超时的限制解决办法 ECSHOP模板/ecshop开发中心(www.68ecshop.com) / 2014-06-04 ecshop运行超过服务器默认的设置30秒的限制时会 ...

  9. android 设计工具栏

    设计工具栏Action Bar(订制工具栏类型) 工具栏给用户提供了一种熟悉和可预测的方式来执行某种动作和操纵应用程序,但是这并不意味着它就需要和其他的应用程序看起来一样的.如果想设计工具栏以使得它能 ...

  10. 防止sql注入,过滤敏感关键字

    //sql过滤关键字 public static bool CheckKeyWord(string sWord) { //过滤关键字 string StrKeyWord = @"select ...