Winston Logstash Config for Node.js
Ship logs from your Node.js application to Logstash
Get started quickly with NodeJS logging using our configuration guide on how to easily ship NodeJS logs to your hosted Logstash and OpenSearch.
Install Integration
Introduction
The example code used in this guide has been setup using Node.js v14.17.0 (64-bit).
Node Package Manager (npm) can be used to install the required winston
(3.3.3)
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 documentation can be found here (opens in a new tab).
Installing
First, ensure that you have downloaded and installed Node.js (32 or 64 bit). You can get it from the official website, here (opens in a new tab). This includes various options to install including using an installer file (for Windows or Mac).
If you are just starting out with sending logs via Node.js there is a useful tutorial for creating an 'express' application to help get you up-and-running here (opens in a new tab).
Inside your project directory, open a terminal of your choice and type the following:
npm install winston@3.3.3
npm install winston-logstash@1.2.1
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: '@logstash.sslPort:strip_quotes',
host: '@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");
Check Logit.io for your logs
Data should now have been sent to your Stack.
View My DataIf you don't see take a look at How to diagnose no data in Stack below for how to diagnose common issues.
How to diagnose no data in Stack
If you don't see data appearing in your Stack after following the steps, visit the Help Centre guide for steps to diagnose no data appearing in your Stack or Chat to support now.
Node.JS Logging Overview
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.io 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 and we'll be happy to assist.