Querying Winston Logs

I am using winston for logging in a node.js application.

I see there are options to query it.

The example shows how to use options from and to.

My question is

  1. What are the other options ?
  2. How do I specify what file should be queried ?

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

I ran into this same problem.

At least some of the options can be found in Winston’s transport.js in the function Transport.prototype.normalizeQuery.

Here’s a quick summary:

options.rows, options.limit = how many results to return. default is
10;

options.start = starting row offset. default is 0

options.from = date string or date object for starting limit.
default is now -24 hours

options.until = date string or date object for ending limit. default
is now

options.order = either ‘asc’ or ‘desc’ order. default is ‘desc’

options.fields = which fields to return. default is undefined (which
returns all)

Solution 2

Here’s an example of querying file logger + available options. The main drawback (IMO) is lack of filtering. The most useful feature would be filtering by level but alas…

"use strict";

var logFilename = __dirname + '/log/2014-02-24.log';

var winston = require('winston');
var logger = new (winston.Logger)({
    transports: [
        new (winston.transports.File)({
            filename:  logFilename,
            timestamp: true
        })
    ]
});

var options = {
    from:   new Date - 24 * 60 * 60 * 1000,
    until:  new Date,
    limit:  10,
    start:  0,
    order:  'asc',
    fields: ['message']
};
logger.query(options, function (err, result) {
    if (err) {
        throw err;
    }

    console.log(result);
});

P.S. BTW, here’s an open issue about querying logs.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply