NodeJS: Quick and dirty logging 📈
Tags: node, express, logging, beginners
Published: 2020-06-11 11:21:47


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](https://logz.io/blog/what-is-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.](https://expressjs.com/en/starter/hello-world.html)

Also make sure you have NodeJS, npm and express dependencies [installed](https://expressjs.com/en/starter/installing.html).

    //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}`));
    
Run the server on your terminal$ node index.js and you should be able to access it through your browser at [http://localhost:3000](http://localhost:3000)

![It looks simple but it works](https://cdn-images-1.medium.com/max/2400/1*Y9_e7NPpCAyqta-lBYhBzw.png)

**Step 2: Lets add some simple logging to the console**
Lets modify the code and add a library called [Morgan](https://github.com/expressjs/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}`));
    
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/](http://localhost:3000/)
[http://localhost:3000/forbidden](http://localhost:3000/forbidden)
[http://localhost:3000/sdfasdfasdf](http://localhost:3000/sdfasdfasdf)

👏 Great, you now have logging! 👏
Your terminal should look something like this:
![](https://cdn-images-1.medium.com/max/2000/1*mUrr-DrTVzyaPp4ppx7T4Q.png)

**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}`));
    
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](https://cdn-images-1.medium.com/max/2000/1*XCPLE7UtgtY_I-JAg6aQnQ.png)

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}`));

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](https://cdn-images-1.medium.com/max/3628/1*NSxHNj56Rf4LFmmlWK279A.png)

🎉 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](https://www.elastic.co/beats/filebeat), [Elasticsearch](https://www.elastic.co/) and [Kibana](https://www.elastic.co/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).