Ready to get going? Start your 14 days free trial today

Start free trial

Have an account? Sign in

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

Winston Logstash Config For 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 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 TCP with SSL enabled.

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

Step 1 - InstallingCopy

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 LogsCopy

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-ssl-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");

Step 3 - Node.JS Logging OverviewCopy

Node.js is an open-source server-side platform built using JavaScript & was developed in 2009 by Ryan Dahl. As Node.js uses an event-driven, I/O model it is highly suited to powering real-time applications including live chat & chatbots, automatically refreshing feeds for social media, and additionally, it is increasingly used for sending web push notifications. It is most commonly run on Linux & Windows operating systems.

Winston is the most popular logging library for Node.js as it makes logging more flexible & can be configured to multiple levels.

Winston is well known for its robust feature set & easy installation which we have detailed in this configuration guide. Morgan is commonly used in association with Winston and operates as an HTTP request logger middleware for Node.js.

Parsing, transforming & analysing emergency, critical & error Node.js logs can be hard to perform at scale from the default logging format. Priority messages can easily get lost in the noise of other logging formats (such as Info & Notice) and are not alerted on to be resolved by your wider DevOps team.

Logit provides a complete Node.js log analyser by default as part of our log management platform, this includes alerting, collection & visualisations enhancing your text logs to provide data-driven insights & real-time log tailing.

If you need any more assistance with migrating your Node.js data to Logstash we're here to help. Just get in contact with our support team via live chat & we'll be happy to assist.

Toggle View

Expand View

Return to Search