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); });

 

 

 

 

 

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM