Logstash Client
logstash-client is a general-purpose Node.js library for sending logs to Logstash instances. It provides support for UDP, TCP, and in-memory transports, designed to be independent of specific logging frameworks like Winston or Bunyan. The package's current stable version is 1.1.1, which was published in 2016. Due to its age and lack of updates, the release cadence is inactive, and the library is no longer under active development. Key differentiators at the time of its development included its transport flexibility and independence from specific logger implementations, allowing it to integrate with various Node.js applications. However, this also means it predates modern JavaScript features and best practices.
Common errors
-
ReferenceError: require is not defined in ES module scope
cause Attempting to use `require()` in an ECMAScript module (.mjs file or `type: module` in package.json).fixThis package is CommonJS-only. Convert your project to CommonJS, or use a modern Logstash client that supports ES modules. If you must use this package in an ESM context, consider a wrapper function or a build step like Webpack/Rollup that can handle CJS modules. -
Error: connect ECONNREFUSED <host>:<port>
cause The client could not establish a connection to the Logstash server. This usually means Logstash is not running, is not listening on the specified host/port, or a firewall is blocking the connection.fixVerify that your Logstash instance is running and has an input configured for the specified type (UDP/TCP) on the correct host and port. Check firewall rules between your Node.js application and the Logstash server. -
Messages not appearing in Logstash when using TCP, or appearing concatenated/malformed.
cause Before version 1.0.2, the TCP transport did not automatically add a newline, which is crucial for Logstash's `json_lines` codec to correctly parse individual JSON events. Custom formatters might also omit the newline.fixEnsure you are using `logstash-client` version 1.0.2 or newer. If you provide a custom `format` function, ensure it appends a newline character (`\n`) after stringifying your JSON message (e.g., `return JSON.stringify(message) + '\n';`). Confirm your Logstash input configuration uses `codec => json_lines {}`.
Warnings
- breaking Version 1.0.2 introduced a fix to add a newline character to TCP transport's `socket.write()` call. If you were relying on concatenated messages or custom handling of JSON streams without explicit newlines in previous versions, this change will alter the message framing.
- gotcha This package is abandoned and has not been updated since 2016. It does not support modern JavaScript features like ES modules and may have compatibility issues with newer Node.js versions or recent Logstash releases. It also lacks security updates.
- gotcha Messages sent via UDP transport have a maximum size limit, typically around 64KB, which is dependent on the underlying network and OS. Exceeding this limit can result in silent message truncation or loss without an error being reported by the client.
- gotcha If the Logstash server is unreachable or misconfigured, the client's `send` method, when called without a callback, will not report delivery failures. This can lead to silent message loss, especially with UDP where delivery is inherently unreliable.
Install
-
npm install logstash-client -
yarn add logstash-client -
pnpm add logstash-client
Imports
- Logstash
import Logstash from 'logstash-client'; // This package is CommonJS-only import { Logstash } from 'logstash-client';const Logstash = require('logstash-client');
Quickstart
const Logstash = require('logstash-client');
// Configure a UDP client to send messages to Logstash
const logstashUdp = new Logstash({
type: 'udp',
host: 'localhost', // Replace with your Logstash host
port: 13333 // Replace with your Logstash UDP input port
});
// Send a basic log message
logstashUdp.send({
'@timestamp': new Date().toISOString(),
'message': 'Hello from logstash-client UDP!',
'level': 'info',
'service': 'my-node-app'
}, (err) => {
if (err) {
console.error('Error sending UDP message:', err);
} else {
console.log('UDP message sent successfully.');
}
});
// Configure a TCP client (requires a Logstash TCP input, e.g., on port 8099)
const logstashTcp = new Logstash({
type: 'tcp',
host: 'localhost', // Replace with your Logstash host
port: 8099, // Replace with your Logstash TCP input port
format: (message) => {
// Custom formatter: add a timestamp and filter sensitive data
message.formattedAt = new Date().toISOString();
message.apiKey = '[FILTERED]';
return JSON.stringify(message) + '\n'; // Ensure newline for TCP streams
}
});
logstashTcp.send({
'event': 'user_login',
'userId': 123,
'apiKey': process.env.API_KEY ?? 'secret_key_if_not_set',
'status': 'success'
}, (err) => {
if (err) {
console.error('Error sending TCP message:', err);
} else {
console.log('TCP message sent successfully.');
}
});