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 RFC5424severity 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 a winston.Logger instance using the configure 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‘ });

这些对象的存储方式因传输而异(以最好地支持所提供的存储机制)。 以下是每个传输处理元数据的快速摘要:
  1. Console: Logged via util.inspect(meta)
  2. 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: FileCouchdb,RedisLogglyNssocket, 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);
  });
时间: 2024-08-06 20:06:00

winston日志管理1的相关文章

winston日志管理3

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 (tr

Nodejs日志管理包

Nodejs日志管理工具包:log4js 和 winston 1.log4js的使用 1)package.json中加入依赖 "log4js":"~0.6.21" 2)写一个log4js的配置文件log4js_conf.json { "appenders": [ { "type": "console" }, { "type": "file", "filena

slf4j+logback搭建超实用的日志管理模块

文章转自http://www.2cto.com/kf/201702/536097.html slf4j+logback搭建超实用的日志管理模块(对日志有编号管理):日志功能在服务器端再常见不过了,我们非常有必要记录下发生在服务器上的活动,这些日志将用于debug.统计等各种用途. slf4j+logback这种实现方式是很常见的,好处自然是方便!.在这篇文章中,你将看到如何使用logback搭建你自己的日志组件并将日志输出到文件.如何查看这些文件.如何为每个线程上的访问分配独有的一个日志id.

linux 学习 14 日志管理

第十四讲 日志管理 14.1 日志管理-简介 1.日志服务 ?在CentOS 6.x中日志服务已经由rsyslogd取代了原先的syslogd服务.rsyslogd日志服务更加先进,功能更多.但是不论该服务的使用,还是日志文件的格式其实都是和syslogd服务相兼容的,所以学习起来基本和syslogd服务一致. ?rsyslogd的新特点: ?基于TCP网络协议传输日志信息; ?更安全的网络传输方式: ?有日志消息的及时分析框架: ?后台数据库: ?配置文件中可以写简单的逻辑判断: ?与sysl

MySQl Study学习之--MySQl二进制日志管理

MySQl Study学习之--MySQl二进制日志管理 MySQL二进制日志(Binary Log):   a.它包含的内容及作用如下:    包含了所有更新了数据或者已经潜在更新了数据(比如没有匹配任何行的一个DELETE)    包含关于每个更新数据库(DML)的语句的执行时间信息    不包含没有修改任何数据的语句,如果需要启用该选项,需要开启通用日志功能    主要目的是尽可能的将数据库恢复到数据库故障点,因为二进制日志包含备份后进行的所有更新    用于在主复制服务器上记录所有将发送

ABP日志管理

ABP日志管理 基于DDD的现代ASP.NET开发框架--ABP系列之8.ABP日志管理 ABP是“ASP.NET Boilerplate Project (ASP.NET样板项目)”的简称. ABP的官方网站:http://www.aspnetboilerplate.com ABP在Github上的开源项目:https://github.com/aspnetboilerplate 本文由东莞-天道提供翻译 Server side(服务器端) ASP.NET Boilerplate使用Castl

SpringAOP拦截Controller,Service实现日志管理(自定义注解的方式)

首先我们为什么需要做日志管理,在现实的上线中我们经常会遇到系统出现异常或者问题.这个时候就马上打开CRT或者SSH连上服务器拿日子来分析.受网络的各种限制.于是我们就想为什么不能直接在管理后台查看报错的信息呢.于是日志管理就出现了. 其次个人觉得做日志管理最好的是Aop,有的人也喜欢用拦截器.都可以,在此我重点介绍我的实现方式. Aop有的人说拦截不到Controller.有的人说想拦AnnotationMethodHandlerAdapter截到Controller必须得拦截org.sprin

java程序日志管理

初入软件开发这一行的人,可能对日志管理的概念并不是很明确,大概是由于经验所限,以至于根本还考虑不到这个问题. 而从某种意义上来说,日志管理实际上也不需要初入这一行的人来管,他们只需要负责实现自己的主要业务逻辑和功能就好了. 我当初刚入行的时候就有很长一段时间完全不用去关心日志,到后来偶尔涉及到的时候,也都是从其他地方采用cv大法直接搬用. 不过,随着工作时间的变化,随着手头上任务重要程度的变化,也随着接触到的项目数量的变化,让我越来越意识到日志的重要性,它在整个系统中发挥着至关重要的作用! 尤其

Linux的日志管理

Linux日志的管理 日志:记录了你几乎所有的操作记录,用于系统的审核,故障的排除.日志文件永久存放在日志目录中,系统日志保存在/var/log中 rsyslog 按照日志类型分类,把所有日志记录到/var/log目录下. /var/log/messages是许多进程日志文件的汇总,从该文件可以看出任何入侵企图或成功的入侵. /var/log/secure 与安全相关的日志. /var/log/cron 与计划任务相关的日志. /var/log/boot.log与系统启动的相关日志,只保留本次系