Search code examples
node.jsexpresswinston

Query winston log returns empty result


I have a very basic express app. I added winston without any configuration. I create log messages like this:

require('winston').info('App started');

and all log messages go into app.out.log. When I vi into the file I see

info: App started

so default logging using winston seems to work fine. Now I want to use the query api of winston:

require('winston').query({}, function(err, results) {
    if (err) {
        res.status(400).json(err);
    } else {
        res.json(results);
    }
});

But results is empty. The doc says:

Winston supports querying of logs with Loggly-like options. See Loggly Search API. Specifically: File, Couchdb, Redis, Loggly, Nssocket, and Http.

and since the default transport is File I would expect results to be not empty. Any ideas whats wrong?


Solution

  • By default, Winston uses only ConsoleTransport. So if you want to query a log file, you have to add proper Transport (in this example is used file transport).

    There is a little example:

    var winston = require('winston');
    
    winston.add(winston.transports.File, { filename: 'app.out.log' });
    
    winston.info('App started');
    
    winston.query({}, function(err, results) {
        if (err) {
          res.status(400).json(err);
      } else {
          res.json(results);
      }
    });