Node.js Logging with Winston
Ship logs from your Node.js application to Logit.io
Get started quickly with Node.js logging using our configuration guide on how to easily ship Node.js logs to your hosted Logstash and OpenSearch.
Install Integration
Introduction
The example code used in this guide has been setup using Node.js v20.18.0 (64-bit).
Node Package Manager (npm) can be used to install the required winston
(3.15.0)
and winston-logstash
(1.2.1) packages.
These will allow you to send log data from your Node.js application, to your Logit.io stack.
Installing
First, ensure that you have downloaded and installed Node.js. 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 (this will install the latest version of Winston and winston-logstash):
npm install winston
npm install winston-logstash
Sending Logs
To setup Winston, add the following code to your application before sending logs:
const winston = require('winston');
const LogstashTransport = require("winston-logstash/lib/winston-logstash-latest");
const logger = winston.createLogger({
transports: [
new LogstashTransport({
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:
logger.info('Hello, this is a log message!');
You can send different levels of log records by using different Winston log methods. Other log record levels and their corresponding winston methods include:
winston.log("This is a standard log message");
winston.error("This is an error log message");
winston.warn("This is a warning log message");
For additional information, the official Winston documentation can be found here (opens in a new tab).
Launch Logs to View Your Data
Launch LogsHow to diagnose no data in Stack
If you don't see data appearing in your stack after following this integration, take a look at the troubleshooting guide for steps to diagnose and resolve the problem or contact our support team and we'll be happy to assist.