How do Winston log threads work?

The Winston documentation contains a section for streaming logs that reads:

Streaming allows you to broadcast your logs from your chosen transport.

and gives the following code example:

// // Start at the end. // winston.stream({ start: -1 }).on('log', function(log) { console.log(log); }); 

My reading of this is that every new log message added will be printed to the console. The configuration {start: -1} tells the thread to start at the end of the file, so only new log entries are output. I expect that the following Node script will cause each existing line of the test.log file test.log be displayed on the console, and then a new object that will be displayed every 500 meters. After that.

 var winston = require('winston'); winston.add(winston.transports.File, { filename: 'test.log' }); winston.remove(winston.transports.Console); winston.stream().on('log', function(log) { console.log(log); }); setInterval(function(){ winston.log('info', 'help'); }, 500); 

I would expect to see something like the following output:

 {"level":"info","message":"help","timestamp":"2013-12-10T05:55:15.806Z"} {"level":"info","message":"help","timestamp":"2013-12-10T05:55:16.307Z"} {"level":"info","message":"help","timestamp":"2013-12-10T05:55:16.809Z"} {"level":"info","message":"help","timestamp":"2013-12-10T05:55:17.309Z"} {"level":"info","message":"help","timestamp":"2013-12-10T05:56:48.316Z"} 

What actually happens is that logging works as expected with the File transport (the file receives a new log entry every 500 ms), but there is no way out on the console. The string console.log(log) never called.

Am I missing something obvious or misunderstood the purpose of Winston log threads?

+7
stream logging winston
source share
1 answer

I experienced the same type of bugg that you describe. I came to the conclusion that the default logger var winston = require('winston') does not work with query , stream functions and is not an EventEmitter , so you can use winston.on('logging', function() {...})

The solution you need to use is to create an instance of your own registrar as follows:

  var logger = new (winston.Logger)({ transports: [ new (winston.transports.Console)({ level: 'info' }), new (winston.transports.File)({ filename: 'app.log' }) ] }); 

and then do some logging:

  setInterval(function(){ logger.log('info', 'logging some stuff..'); }, 500); 

then you can transfer already registered recordings using stream :

  logger.stream().on('log', function(log) { console.log('>>> ', log); }); 

I think you misunderstood the stream function, because it does not record journal entries on the fly, it simply transfers all the records that you have already registered. To connect an event to your registrar, you can use the "logging" event on the log object:

  logger.on('logging', function (transport, level, msg, meta) { // [msg] and [meta] have now been logged at [level] to [transport] console.log("[%s] and [%s] have now been logged at [%s] to [%s]", msg, JSON.stringify(meta), level, transport.name); }); 

I don’t know why this is not supported by the default registrar, and, as I said at the beginning of this post, I get errors inside the asynchronous library (winston dependency) when using the query or stream function with the default registrar.

+9
source share

All Articles