Appending Syslog Over TLS Protocol in NodeJS

What is Syslog?

Syslog refers to the central logging of the softwares or applications. Syslog can log the kernal messages, system messages, as well as user defined application messages.

When to use Syslog?

Syslog is used when there is a large number of devices/servers and we need a central logging system for the whole architecture. Syslog servers support a large number of I/O operations on the network. Syslogs can be generated through TCP/UDP/TLS protocols.

How to implement syslogs in NodeJS applications?

I had a requirement to append syslogs over TLS protocol. I searched over the internet for the same but unfortunately I couldn’t find any. I have created an appender for syslogs. You can find the appender here.

Creating a demo app for syslog

Create a directory and navigate to the newly created directory.

mkdir syslog-demo && cd syslog-demo

Now initialize the directory as node project.

npm init -y

Let’s install syslog-appender-pro.

npm i syslog-appender-pro

Create a file index.js and paste the content in the file. The content in the file should be

const { Appender, FACILITIES, SEVERITIES, PROTOCOLS } = require('syslog-appender-pro');const logger = new Appender({
defaultAppName: 'syslog-demo',
caPath: '/etc/ssl/CA.crt',
certificatePath: '/etc/ssl/client.crt',
keyPath: '/etc/ssl/client.key',
host: 'localhost',
port: 514,
protocol: PROTOCOLS.TLS,
facility: FACILITIES.LOCAL0,
severity: SEVERITIES.DEBUG
})
var i = 0;
setInterval(() => {
logger.debug({ message: 'Message from syslog-appender-pro!' + i++ })
.then((result) => {
console.log('Sent message on tls');
})
.catch(console.error);
}, 2000);

Run the app using the command

node index.js

You can see the output for the logging of the app. Also check the syslog into your syslog logs

tail -f /var/log/syslog

You can see your demo app logs being generateed in syslog

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store