Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do Winston's log streams work?

The Winston documentation has a section on streaming logs which says:

Streaming allows you to stream your logs back 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 each new log message added would be output to the console. The {start: -1} config tells the stream to start at the end of the file, so only new log entries are output. I expect the following Node script would result in each existing line of the test.log file being output to the console, and then a new object to be output every 500ms thereafter.

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 occurs is that the logging works as expected with the File transport (the file gets a new log entry every 500ms) but there is no output to the console. The console.log(log) line is never called.

Have I missed something obvious, or misunderstood the purpose of Winston's log streams?

like image 240
drzax Avatar asked Dec 10 '13 06:12

drzax


People also ask

What is a Winston logger?

winston is designed to be a simple and universal logging library with support for multiple transports. A transport is essentially a storage device for your logs. Each winston logger can have multiple transports (see: Transports) configured at different levels (see: Logging levels).

How do I view Winston logs?

Go to the example. log file in the logs folder to view the log. Winston allows you to implement multiple logging transports, i.e., a log can be recorded to a file, console, or database.

Is Winston logging asynchronous?

It does use the async module, but that is not asynchronous on all accounts either, i.e.: function cb(err) { if (callback) { if (err) return callback(err); callback(null, level, msg, meta); } callback = null; if (!

How do I change the log level in Winston?

You can change the logging level in runtime by modifying the level property of the appropriate transport: var log = new (winston. Logger)({ transports: [ new (winston.


1 Answers

I have experienced the same kind of bugg that you are describing. My conclusion is that the default logger var winston = require('winston') do not work with the functions query, stream nor is an EventEmitter so that you can use winston.on('logging', function() {...}).

The solution you need to use is to instantiate your own logger like so:

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

and then you do some logging:

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

then you can stream the already logged entries by using stream:

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

I think you have misunderstood the stream function because it do not stream log-entries on the fly, it just streams all the entries you have already logged. To hook up an event to your logger you can use the 'logging' event on the logger 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 do not know why this is not supported by the default logger and as I said in the beginning of this post I get errors inside of the async-library (dependency to winston) when using the query or stream functions with the default logger.

like image 63
user579089 Avatar answered Sep 17 '22 20:09

user579089