Search code examples
node.jslogginglogstashwinstonelk

Winston 3.x support for logstash missing?


I'm trying to integrate a dummy nodejs app to an ELK stack. I'm using winston as logging library since it's my preferred choice when I develop on a JS stack. I've tried to write this sinppet of code, using winston 3.x version

var winston = require('winston');
require('winston-logstash');


var logger =  winston.createLogger({
    level: 'info',
    transports: [
        new winston.transports.Console(),
        new winston.transports.Logstash({
            port: 5000,
            node_name: 'my node name',
            host: '127.0.0.1'
        })
    ]
});

logger.info('ciao mondo')

The result was

> node index.js

logstash is a legacy winston transport. Consider upgrading: 
- Upgrade docs: https://github.com/winstonjs/winston/blob/master/UPGRADE-3.0.md

So I've read the official instructions which state that the logstash support is now a format

var winston = require('winston');
require('winston-logstash');


var logger =  winston.createLogger({
    level: 'info',
    format : winston.format.logstash(),
    transports: [
         new winston.transports.Console()/*,
         new winston.transports.Logstash({
           port: 5000,
           node_name: 'my node name',
           host: '127.0.0.1'
         })*/
      ]
  });

  logger.info('ciao mondo')

Now the output is

> node index.js

{"@message":"ciao mondo","@fields":{"level":"info"}}

Which is ok, but since I've not told how to connect with logstash socket, the centralized logging system didn't receive this event.

I've tried downograding winston to 2.4.1 and it works fine, excpet for the fact that I'm loosing some good feature introduced in the laters versions. I've not found any documentation that informs about logstash support on 3.x version. Am I missing something or I have to write something custom?


Solution

  • There are two concepts at play here, in Winston 3.

    A format just does some transformations on the object you're logging.

    A transport is a "pipe" that defines a way of taking your log object and sending it somewhere.

    In the Winston 2 world, these two concepts were often combined; for instance, the winston-logstash module was a transport but also implicitly formatted objects in a way that's acceptable for Logstash. There's nothing in Winston 2 or 3 that restricts what is possible in terms of transports or formats, so Winston 3 is not "missing support" for Logstash.

    However, Winston 3 did make some changes to the transport API (and also separated out formats, as mentioned - though a transport can still do formatting). That is why you see warnings about winston-logstash being a legacy transport: the authors of winston-logstash have not updated their module to use the new Winston 3 style interfaces. There is a thread on the Winston project with some helpful info for transport authors on updating their transports.

    So, you could bug the authors to upgrade their transport, upgrade/wrap it yourself (see linked thread), or - I think the legacy warning is just a warning, so it should still work ok with Winston 3.

    In fact, I think there is already a PR open to make winston-logstash 3.x compatible, though the repo maintainer seems to have ghosted; I suppose you could try using the PR branch/fork. You might also try winston-logstash-transport which appears to achieve the same goals but appears to be designed for 3.x. Feel free to comment on what works best.