Logs, Monitoring, and NodeJs in Harmony — 1

Rafael Costa
3 min readJun 25, 2021

--

When we build our applications, in our development environment, we have a lot of tools to help us in this process, tools for monitoring, debugging, testing, etc. But when our application is running in a production environment sometimes we forget that we also need to monitoring, debugging constantly to check the application's health and to guarantee the maximum of disponibility.

In this series of articles, we discuss a simple way to do configure the monitoring and logging on a NodeJs Web Application, we’ll have the following articles:

1 — Build a simple NodeJs Web Application

2 — Configure docker environment for ELK Stack and Elastic APM Agente in our Application

3 — Configure Logstash and Filebeat

1 — Build a simple NodeJs Web Application

First, let’s start with our application, I’ve decided to create a REST API with Express. In the picture below we have the application structure, to create this demo. To build this application I’ve used some Clean Architecture concepts.

Picture 1

A little overview of the structure. In the Infra folder, there are our modules that depend somehow on external libraries. Into the App folder, there are all of our business logic. Into the Controller folder, there are the modules that handle HTTP requests and into the Shared folder, there are some modules shared in other layers. As we can see the main.js file in our application entry point and the routes.js in on all of the application HTTP router is configured.

Our server configuration was like this:

Here we have a classic express configuration but have one thing here I want to infantize, our server class receives as dependency injection the logger object, using this object we can log everything if is necessary, but why didn’t I use the console.log for this job? Well, when we talk about the log, it is not only to show some messages on the screen, we need to show a lot of things like the severity of the log (log level), the time when this log happens, the PID that logged the message, and the log format need to follow the log pattern to enable sending our logs to a logging tool like ELK Stack.

In NodeJs there are a lot of packages with implementing a Logger Interface to handle the log pattern for us, in this article I’ve decided to use the pino.js. In the code below we can see how the log class was done.

Here I create a class using the singleton pattern to guarantee that the logger was instantiated only once. To be easier to return the HTTP Status Code I create a BaseController to provide some methods to handle it for us. Next, we can see the BaseController and the HelloWorldController.

The HelloWorldController receives the helloWorldUsecase in the constructor, so let’s see what do we have in HelloWorldUsecase.

Another thing I did in this project was to use the Either pattern instead throws exceptions everywhere. To understand more about this pattern and the pros and cons consider red this article. Now let’s see our routes.

Wait a minute, we have something a little different where, the routesAdapt function. What does the function do? This function does two things. First, it isolated our controllers from express, if someday we need to change express for fastify.js or other frameworks, we need to change only this function and the router file. Secondly, it concentrates on our default request logs. Let’s see how is it implemented.

I guess with this explanation we can continue in our other chapter. If you want to see all the application, you can find here, and if you have some questions it was my pleasure to answer if I can.

In the next article, we going to configure our docker environment with ELK Stack and create the Dockerfile to containerize our application.

So, see you in the next article.

My Linkedin. My Github.

--

--

Rafael Costa

Backend Engineer— [GoLang] [Rust] [IoT] [IIoT] [NodeJs]