DEV Community

Cover image for NodeJS: Quick and dirty logging 📈
Victor Chan
Victor Chan

Posted on

NodeJS: Quick and dirty logging 📈

Here is a quick and dirty guide on how to add logging to your ExpressJS app. I’ll assume that you already know about Javascript, NodeJS and ExpressJS.

Why logging 📈?
Logging is one of the three main pillars of a systems Observability. Adding logging allows your express app to be monitored, for debugging errors as well as infer usage of the app.

Im sold 🙋🏻‍♂️, how do I add logging?
Just follow the four examples below to get an understanding of it.
Or just skip to Step 4 👇 if you want the answer to just add to your Express app.

Step 1: Have an ExpressJS app prepared
For this tutorial we will go through the hello world example found here.

Also make sure you have NodeJS, npm and express dependencies installed.

//index.js
const express = require("express");
const app = express();
const port = 3000;

app.get("/", (req, res) => res.send("Hello World!\n"));

app.get("/forbidden", (req, res) => res.status(403).send("Forbidden!\n"));

app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`));
Enter fullscreen mode Exit fullscreen mode

Run the server on your terminal$ node index.js and you should be able to access it through your browser at http://localhost:3000

It looks simple but it works

Step 2: Lets add some simple logging to the console
Lets modify the code and add a library called Morgan. npm install morgan

//index.js
const express = require("express");
const morgan = require("morgan"); //Import it here
const app = express();
const port = 3000;

app.use(morgan("tiny")); //Use it here
app.get("/", (req, res) => res.send("Hello World!\n"));

app.get("/forbidden", (req, res) => res.status(403).send("Forbidden!"));

app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`));
Enter fullscreen mode Exit fullscreen mode

Run the server again on your terminal $ node index.js and on your browser test it with the following links:
http://localhost:3000/
http://localhost:3000/
http://localhost:3000/forbidden
http://localhost:3000/sdfasdfasdf

👏 Great, you now have logging! 👏
Your terminal should look something like this:

Step 3: Store the logs on the machine

const express = require("express");
const fs = require("fs");
const app = express();
const port = 3000;

app.use(morgan("tiny")); //We will keep this for demo purposes
app.use(morgan("tiny", { stream: fs.createWriteStream("./access.log", { flags: "a" }),})); //Write to a file here

app.get("/", (req, res) => res.send("Hello World!\n"));

app.get("/forbidden", (req, res) => res.status(403).send("Forbidden!"));

app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`));
Enter fullscreen mode Exit fullscreen mode

Once again run the server in your terminal $ node index.js and access the links, the server will now store them into a file called access.log
Your precious logs, now stored in a file

But what if the server keeps running, and keeps generating logs? Over time the file size could become way too large. This can be solved by rotating the log files.

Step 4: Rotate the logs daily, and remove older logs

Lets add another library called file-stream-rotator $ npm install file-stream-rotator which will allow us to clean up logs past a certain amount of time. Lets also change the type of logging from tiny to combined , this will show more details in the logs.

const express = require("express");
const morgan = require("morgan");
const fs = require("fs");
const fileStreamRotator = require("file-stream-rotator");
const app = express();
const port = 3000;

//This will ensure log directory exists for acccess logs
const logsFolder = __dirname + "/accessLog";
fs.existsSync(logsFolder) || fs.mkdirSync(logsFolder);
//Create a log stream here
const rotatingLogStream = fileStreamRotator.getStream({
    filename: `${logsFolder}/access-%DATE%.log`,
    frequency: "daily",
    verbose: false,
    date_format: "YYYY-MM-DD",
    max_logs: 45, //Keep for 45 days
});

app.use(morgan("tiny")); //We will keep this for demo purposes
app.use(morgan("combined", {stream: rotatingLogStream}));

app.get("/", (req, res) => res.send("Hello World!\n"));

app.get("/forbidden", (req, res) => res.status(403).send("Forbidden!"));

app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`));
Enter fullscreen mode Exit fullscreen mode

Again run the server in your terminal $ node index.js and access the links, the server will now store them into a folder with log files sorted daily, and cleaning them up after 45 days.

Now kept daily and rotated, nice and neat

🎉 Congratulations! 🎊 You now have a rotating log setup for your Express app with just a few lines of code.

For a production setup you can easily use Filebeats, Elasticsearch and Kibana to collect and display these logs onto a dashboard (Out of this posts scope, but maybe I’ll cover it in a future writeup).

Top comments (1)

Collapse
 
jabo profile image
Jabo

Will definitely try this out!