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.Logger
instance using theconfigure
method:
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 可 ...
随机推荐
- 【液晶模块系列基础视频】1.3.iM_TFT30模块简介
[液晶模块系列基础视频]1.3.iM_TFT30模块介绍 ============================== 技术论坛:http://www.eeschool.org 博客地址:http:/ ...
- PHP 错误与异常 笔记与总结(8)自定义错误处理函数 set_error_handler()
通过 Set_error_handler() 函数设置用户自定义的错误处理函数. 步骤: ① 创建错误处理函数 ② 设置不同级别调用函数 ③ Set_error_handler() 函数制定接管错误处 ...
- cURL 学习笔记与总结(5)用 cURL 访问 HTTPS 资源
<?php $curlobj = curl_init(); // 初始化 curl_setopt($curlobj, CURLOPT_URL, "https://ajax.aspnet ...
- 用diss 实现 push动画
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { AAAViewController * aaa = [[AAAVie ...
- PHPStorm下XDebug配置
PHPStorm下XDebug配置 分类: PHP2013-08-11 22:15 19697人阅读 评论(0) 收藏 举报 目录(?)[+] 1安装Xdebug 用yum安装可能会失败,用p ...
- MySQL 性能优化的最佳20多条经验分享[转]
今天,数据库的操作越来越成为整个应用的性能瓶颈了,这点对于Web应用尤其明显.关于数据库的性能,这并不只是DBA才需要担心的事,而这更是我们程序员需要去关注的事情. 当我们去设计数据库表结构, ...
- linux下利用curl监控网页shell脚本
#!/bin/bash smail() {mail -s "$1" gjw_apparitor@gmail.com <<EOF$1$2====report time: ...
- LENGTH() CHAR_LENGTH()
http://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_length LENGTH(str) Returns the ...
- 每个配置xml的含义作用
参考链接 web.xml xxx-servlet.xml(例如:dispatcher-servlet.xml)是Spring MVC里的,控制器,拦截url,转发view,对应的是controller ...
- delphi 高版本可执行程序减小的办法
选菜单里的 Project -> Options.. (Shift+Ctrl+F11)出现Project Options for Project1.exe窗口,在左边选 Packages出现如下 ...