Fastify writes logs to a Syslog file.

Edison Devadoss
3 min readAug 15, 2021

Hi friends, In this article, I will explain how to write node.js logs in Syslog.

https://www.photopea.com/

Before we dive into writes the node.js logs to the Syslog file, we should know why we need the logs.

Why do we need logs:

Log messages are imported in the development life-cycle. Log messages are very helpful for developers. Logs help developers know what their code actually doing.

Logs help developers to resolve bugs quickly, get clear insights, and detect problems early and as they happen through analysis log messages.

For example, If there is a bug in the database related code, our logs will point out where the bug is placed, helping us to resolve the problem quickly.

Node.js lets you write log messages to standard output (stdout) and standard error (stderr)using the log and error functions, respectively.

Fastify with logger.

In this example, I use Fastify for my framework and Pino for the logger.

Create a project

$ mdkir fastify-pino-logs
$ cd fastify-pino-logs
$ npm init -y
$ npm install fastify pino@next

Follow the above commands for installation steps.

Create logger.js file.

In this logger.js file, I have set two types of logs. pino.transport can be used for both transmitting and transforming log output.

The first one I transfer all logs to the Syslog file. The second one shows the logs in the terminal.

https://carbon.now.sh

destination: ‘/var/log/syslog’ transport the logs to the Syslog file.

We can set various options for that click here.

Create index.js file

https://carbon.now.sh

The above image shows the project setup. Now we can run our project in 3000 port.

Run the project

$ node index.js

When I run my project I got the below error.

Screenshot

Error is permission denied for Syslog.

To fix this we can run our application as a root user.

Change to root user

$ sudo su
Screenshot

After changed to root user our project runs successfully.

Now our log messages are transported to the Syslog file.

$ tail /var/log/syslog -f

Using the tail command we can logs messages.

--

--

Edison Devadoss

Software developer / JavaScript / React / React Native / Firebase / Node.js / C Programming / Book Reader