Send data via Node.js to your Logstash instance provided by Logit.io

Node.js

Ship logs from your Node.js application to logstash

The example code used in this guide has been setup using Node.js v8.11.3 (64-bit). Node Package Manager (npm) can be used to install the required winston (2.4.1) and winston-logstash (0.4.0) packages. These will allow us to send log data from our Node.js application, to logstash using TCP with SSL enabled.

The official winston 2.x documentation can be found here.

Step 1 - Installing

First, ensure that you have downloaded and installed Node.js (32 or 64 bit). You can get it from the official website, here.

Inside your project directory, open a terminal of your choice and type the following:

npm install winston@2.4.1
npm install winston-logstash@0.4.0

Step 2 - Sending Logs

To setup winston, add the following code to your application before sending logs:


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

winston.add(winston.transports.Logstash,
{
    port: your-port,
    host: 'your-logstash-host',
    ssl_enable: true,
    max_connect_retries: -1,
});

You can then send logs using a winston log method:


winston.error('This is a test error log message', { custom: 'my custom field', Environment: 'local' });
    

The first parameter is the log message field. The second parameter allows you to specify additional fields, or modify default fields.

You can send different levels of log records by using different winston log methods. This example uses the error method to send error log records. Other log record levels and their corresponding winston methods include:


winston.log("This is a standard log message");
winston.info("This is an info log message");
winston.warn("This is a warning log message");
     
expand view

Expand View

compact view

Compact View

Return to Search
Sign Up