zoukankan      html  css  js  c++  java
  • 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);
      });
  • 相关阅读:
    指针常量与常量指针
    C语言创建并使用dll
    C语言创建并使用lib
    配置错误
    目前常用的自然语言处理开源项目/开发包大汇总
    pyhanlp 文本聚类详细介绍
    Spring Boot中对自然语言处理工具包hanlp的调用详解
    pyhanlp 停用词与用户自定义词典功能详解
    Hanlp分词实例:Java实现TFIDF算法
    Spark应用HanLP对中文语料进行文本挖掘--聚类详解教程
  • 原文地址:https://www.cnblogs.com/jay--zhang/p/5978190.html
Copyright © 2011-2022 走看看