Node.js Syslog Server

1.0.1 · active · verified Tue Apr 21

syslog-server is a lightweight Node.js library for creating a server to receive syslog messages over UDP. It provides a simple, event-driven API to start, stop, and manage the syslog listener. The library, currently at version 1.0.1, allows configuration of the listening port and address, and emits 'message' events for each incoming syslog packet. Each message event provides parsed details such as the reception date, originating host IP, IP protocol version (IPv4/IPv6), and the raw syslog message content. While it does not specify adherence to particular syslog RFCs for deep message parsing, it offers a foundational layer for applications needing to collect basic syslog data. Its primary differentiator is its minimal footprint and direct use of Node.js's built-in `dgram` module, making it suitable for simple syslog collection setups.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart initializes a syslog server, logs incoming messages to the console, and includes basic error handling and graceful shutdown.

const SyslogServer = require('syslog-server');
const server = new SyslogServer();

server.on('message', (value) => {
    console.log(`Received message from ${value.host} (${value.protocol}) at ${value.date.toISOString()}:`);
    console.log(value.message);
});

server.on('start', () => {
    console.log('Syslog server started on port 514');
});

server.on('error', (err) => {
    console.error('Syslog server error:', err.message);
});

// Start the server, potentially on a different port if 514 is privileged or in use
server.start({
    port: process.env.SYSLOG_PORT ? parseInt(process.env.SYSLOG_PORT, 10) : 514,
    address: '0.0.0.0'
}).catch(err => {
    console.error('Failed to start syslog server:', err.message);
    process.exit(1);
});

// To stop the server gracefully on process exit
process.on('SIGINT', async () => {
    console.log('Shutting down syslog server...');
    await server.stop();
    console.log('Syslog server stopped.');
    process.exit(0);
});

view raw JSON →