winston日志管理1
Usage
There are two different ways to use winston: directly via the default logger, or by instantiating your own Logger. The former is merely intended to be a convenient shared logger to use throughout your application if you so choose.
有两种不同的方式使用winston:直接通过默认的logger,或者通过实例化你自己的Logger。
Logging
Logging levels in winston conform to the severity ordering specified by RFC5424: severity of all levels is assumed to be numerically ascending from most important to least important.
winston中的日志记录级别符合RFC 5424规定的严重性顺序:所有级别的严重性被假定为从最重要到最不重要的数字上升。(数字越小,级别越高)
1.使用默认的用法:
The default logger is accessible through the winston module directly. Any method that you could call on an instance of a logger is available on the default logger:
默认日志记录器可通过winston模块直接访问。 您可以在logger实例上调用的任何方法都可在默认记录器上使用:
var winston = require('winston') 与 var winston = require('winston'); var logger = new winston.Logger(); winston的方法在logger上都可以使用.
var winston = require('winston');
winston.log('info', 'Hello distributed log files!');
winston.info('Hello again distributed logs');
winston.level = 'debug';
winston.log('debug', 'Now my debug messages are written to console!');
上述代码和下述代码效果一样
var winston = require('winston');
var logger = new winston.Logger();
logger.log('info', 'Hello distributed log files!');
logger.info('Hello again distributed logs');
By default, only the Console transport is set on the default logger. You can add or remove transports via the add() and remove() methods:
默认情况下,仅在默认logger设置控制台传输。传输使用console和文件. 您可以通过add()和remove()方法添加或删除传输:
补充:有一个比較值得一提的是winston-irc,你能够用来把日志输出到你的团队的IRC渠道。
https://github.com/winstonjs/winston/blob/master/docs/transports.md 是transports的文档地址.
winston.add(winston.transports.File, { filename: 'somefile.log' }); //这里是将日志信息放到somefile.log文件中
winston.remove(winston.transports.Console); //这个只是将日志信息打印出来
2.实例化自己的logger
If you would prefer to manage the object lifetime of loggers you are free to instantiate them yourself:
如果你想管理记录器的对象生命周期,你可以自由实例化它们:
1.第一种实例化的方法
var logger = new (winston.Logger)({
transports: [
new (winston.transports.Console)(), //console.log
new (winston.transports.File)({ filename: 'somefile.log' }) //写日志文件
]
});
You can work with this logger in the same way that you work with the default logger:
//
// Logging
//
logger.log('info', 'Hello distributed log files!');
logger.info('Hello again distributed logs'); //
// Adding / Removing Transports
// (Yes It's chainable)
//
2.第二种实例化的方法
logger
.add(winston.transports.File) //自由实例化他们的第二个方法,使用logger对象
.remove(winston.transports.Console);
3.第三种实例化的方法
You can also wholesale reconfigure awinston.Loggerinstance using theconfiguremethod:
var logger = new winston.Logger({
level: 'info',
transports: [
new (winston.transports.Console)(),
new (winston.transports.File)({ filename: 'somefile.log' })
]
});
//
// Replaces the previous transports with those in the
// new configuration wholesale.
//这是用configure重新实例化的一个
logger.configure({
level: 'verbose',
transports: [
new (require('winston-daily-rotate-file'))(opts)
]
});
Logging with Metadata 元数据
In addition to logging string messages, winston will also optionally log additional JSON metadata objects. Adding metadata is simple:
除了记录字符串消息之外,winston还将可选地记录附加的JSON元数据对象。 添加元数据很简单:还能记录对象的形式
winston.log('info', 'Test Log Message', { anything: 'This is metadata' });
这些对象的存储方式因传输而异(以最好地支持所提供的存储机制)。 以下是每个传输处理元数据的快速摘要:
- Console: Logged via util.inspect(meta)
- File: Logged via util.inspect(meta)
Multiple transports of the same type 同一类型的多个transports
可以使用相同类型的多个传输,例如 winston.transports.File通过在构建传输时传递自定义名称。
定义了两个文件,一个是info,一个是error文件
var logger = new (winston.Logger)({
transports: [
new (winston.transports.File)({
name: 'info-file',
filename: 'filelog-info.log',
level: 'info'
}),
new (winston.transports.File)({
name: 'error-file',
filename: 'filelog-error.log',
level: 'error'
})
]
});
If you later want to remove one of these transports you can do so by using the string name. e.g.:
logger.remove('info-file');
In this example one could also remove by passing in the instance of the Transport itself. e.g. this is equivalent to the string example above;这只是另外一种remove file的方法
var infoFile = logger.transports[0];
logger.remove(infoFile);
Profiling 分析
In addition to logging messages and metadata, winston also has a simple profiling mechanism implemented for any logger:
除了记录消息和元数据之外,winston还具有为任何记录器实现的简单分析机制:
//
// Start profile of 'test'
// Remark: Consider using Date.now() with async operations
//
winston.profile('test'); setTimeout(function () {
//
// Stop profile of 'test'. Logging will now take place:
// "17 Jan 21:00:00 - info: test duration=1000ms"
//
winston.profile('test');
}, 1000);
String interpolation 字符串插值
The log method provides the same string interpolation methods like util.format.字符串的拼接
logger.log('info', 'test message %s', 'my string');
// info: test message my string
logger.log('info', 'test message %d', 123);
// info: test message 123
logger.log('info', 'test message %j', {number: 123}, {});
// info: test message {"number":123}
// meta = {}
logger.log('info', 'test message %s, %s', 'first', 'second', {number: 123});
// info: test message first, second
// meta = {number: 123}
logger.log('info', 'test message', 'first', 'second', {number: 123});
// info: test message first second
// meta = {number: 123}
logger.log('info', 'test message %s, %s', 'first', 'second', {number: 123}, function(){});
// info: test message first, second
// meta = {number: 123}
// callback = function(){}
logger.log('info', 'test message', 'first', 'second', {number: 123}, function(){});
// info: test message first second
// meta = {number: 123}
// callback = function(){}
Querying Logs 日志查询
winston支持winston自己查询日志,这个类似于有点数据库的感觉.
Winston supports querying of logs with Loggly-like options. See Loggly Search API. Specifically: File, Couchdb,Redis, Loggly, Nssocket, and Http.
var options = {
from: new Date - 24 * 60 * 60 * 1000,
until: new Date,
limit: 10,
start: 0,
order: 'desc',
fields: ['message']
};
//
// Find items logged between today and yesterday.
//
winston.query(options, function (err, results) {
if (err) {
throw err;
}
console.log(results);
});
Streaming Logs 创建数据流日志
Streaming allows you to stream your logs back from your chosen transport.
//
// Start at the end.
//
winston.stream({ start: -1 }).on('log', function(log) {
console.log(log);
});
winston日志管理1的更多相关文章
- winston日志管理3
Further Reading 延伸阅读 Events and Callbacks in Winston winston的事件和回调 Each instance of winston.Logger i ...
- winston日志管理2
上次讲到 Exceptions 例外 Handling Uncaught Exceptions with winston 使用winston处理未捕获的异常(这个如果对于异步,我不是很喜欢用) 使用 ...
- winston 日志管理4
配置File Transport winston.add(winston.transports.File, options) The File transport should really be t ...
- Nodejs日志管理包
Nodejs日志管理工具包:log4js 和 winston 1.log4js的使用 1)package.json中加入依赖 "log4js":"~0.6.21" ...
- 第13章 Linux日志管理
1. 日志管理 (1)简介 在CentOS 6.x中日志服务己经由rsyslogd取代了原先的syslogd服务.rsyslogd日志服务更加先进,功能更多.但是不论该服务的使用,还是日志文件的格式其 ...
- ABP(现代ASP.NET样板开发框架)系列之8、ABP日志管理
点这里进入ABP系列文章总目录 基于DDD的现代ASP.NET开发框架--ABP系列之8.ABP日志管理 ABP是“ASP.NET Boilerplate Project (ASP.NET样板项目)” ...
- 【Java EE 学习 76 下】【数据采集系统第八天】【通过AOP实现日志管理】【日志管理功能分析和初步实现】
一.日志管理相关分析 1.日志管理是一种典型的系统级别的应用,非常适合使用spring AOP实现. 2.使用日志管理的目的:对系统修改的动作进行记录,比如对权限.角色.用户的写操作.修改操作.删除操 ...
- ElasticSearch+NLog+Elmah实现Asp.Net分布式日志管理
本文将介绍使用NLOG.Elmah结合ElasticSearch实现分布式日志管理. 一.ElasticSearch简介 ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布 ...
- Apache 日志管理,获取客户端端口号
日志管理分类 日志文件是用户管理和监控 Apache 安全的非常好的第一手资料,它清晰地记录了客户端访问 Apache 服务器资源的每一条记录,以及在访问中出现的错误信息,可以这样说,Apache 可 ...
随机推荐
- https资料
1.HTTPS的七个误解 http://blog.httpwatch.com/2011/01/28/top-7-myths-about-https/ 中文 http://www.cnblogs.c ...
- pdf转能编辑的word的方法
方法一:用汉王ocr文字识别软件,扫描文字,一页一页扫描,复制粘贴 方法二:将pdf文件拷贝到没有pdf阅读器的电脑上,同时你的office是2013,用word打开你的pdf文档,根据他的提示操作, ...
- HDU 1114 完全背包+判断能否装满
题意 给出一个存钱罐里的钱币重量 给出可能的n种钱币重量以及价值 求存钱罐中钱币的最小价值 若不可能另有输出 在裸的完全背包上加了一点东西 即判断这个背包能否被装满 初始化 dp[0]=0 其余的都使 ...
- Memcached 笔记与总结(4)memcache 扩展的使用
在 wamp 环境下进行测试:WAMPSERVER 2.2(Windows 7 + Apache 2.2.21 + PHP 5.3.10 + memcache 3.0.8 + Memcached 1. ...
- PHP 设计模式 笔记与总结(10)数据对象映射模式 2
[例2]数据对象映射模式结合[工厂模式]和[注册模式]的使用. 入口文件 index.php: <?php define('BASEDIR',__DIR__); //定义根目录常量 includ ...
- PHP fwrite() 函数与 file_put_contents() 函数的比较
两个 PHP 函数都可以把字符串保存到文件中,fwrite() 函数的格式是: int fwrite ( resource handle , string string [ , int length] ...
- python 不得不知的第三方库以及常用安装包
mysql 驱动$ sudo pip install MySQL-python redis 数据库$ sudo pip install redis django 全文搜索$ sudo pip inst ...
- journalctl --help
journalctl [OPTIONS...] [MATCHES...] Query the journal. Flags: --system Show the sy ...
- 送给使用phpstorm+thinkphp开发者的福利
送给使用phpstorm+thinkphp开发者的福利 记得两年前的这个时候,我开始学习php.我选择了thinkphp入门,写了我的第一个简单的cms.当时我什么都不懂,但是这里的技术氛围好的, ...
- 关于APP接口设计
最近一段时间一直在做APP接口,总结一下APP接口开发过程中的注意事项: 1.效率:接口访问速度 APP有别于WEB服务,对服务器端要求是比较严格的,在移动端有限的带宽条件下,要求接口响应速度要快,所 ...