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?
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 functionsquery
,stream
nor is anEventEmitter
so that you can usewinston.on('logging', function() {...})
.The solution you need to use is to instantiate your own logger like so:
and then you do some logging:
then you can stream the already logged entries by using
stream
: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: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
orstream
functions with the default logger.