Node.js Morgan Guide

Any server or application needs some kind of logging system. It is common to skip logging in personal or practice project. But it is a must to have a fairly robust logging system in production ready servers and apps.

Benefits of a logging system:

L…


This content originally appeared on DEV Community and was authored by Paras ?‍♂️

Any server or application needs some kind of logging system. It is common to skip logging in personal or practice project. But it is a must to have a fairly robust logging system in production ready servers and apps.

Benefits of a logging system:

  • Logs are used by product managers and UX designers for planning & design.
  • It helps marketers track performance of various features related to advertising campaigns.
  • It gives us the idea of how much traffic is the application getting.
  • Track errors, attacks and unsuccessful requests in production.

Getting started with Morgan

Morgan is a logging package for node.js. It can generate log files for all the traffic that comes to your server. It has some cool tricks to help us in both development and production. So let's get started.

Install Morgan

$ npm install morgan --save

Basic Morgan Usage

Adding morgan to middlewares in express is enough to get you started.

const express = require('express');
const morgan = require('morgan'); // import morgan

const app = express();

// setup morgan
app.use(morgan("dev"));

app.listen(3000, () => {
    console.debug('App listening on :3000');
});

Above setup will start logging requests in console. You can see, I have specified dev in morgan setup. There are many presets available in morgan and one them is dev. Some other presets are:

  • tiny
  • short
  • dev
  • common : Follows Apache like log format

Logging requests to file

Now we will learn how to output logs to a file. Instead of creating simple logging to file, we will create rotating logs. Rotating logs are logs that covers a part of activity on the server in a file and then creates a new log file. This way we can keep track of past activities. We don't have to keep one single log file open for continuous logging. So, let's setup this.

For rotating logs, we will install a package named rotating-file-stream. It will help us automate log rotation.

$ npm install rotating-file-stream --save

Setup Morgan

const express = require('express');
const morgan = require('morgan'); // import morgan
const rfs = require("rotating-file-stream");

const app = express();

// MORGAN SETUP
// create a log stream
const rfsStream = rfs.createStream("log.txt", {
   size: '10M', // rotate every 10 MegaBytes written
   interval: '1d', // rotate daily
   compress: 'gzip' // compress rotated files
})

// add log stream to morgan to save logs in file
app.use(morgan("dev"), {
   stream: rfsStream
});

// another logger to show logs in console as well
app.use(morgan("dev"));

app.listen(3000, () => {
   console.debug('App listening on :3000');
});

That was enough to setup our logging system but we can add environment variables to make it more intuitive to enable and disable logging to file

Adding environment variables to our setup

You will need dotenv package to load environment variables in nodejs from .env file

$ npm install dotenv --save

Now create a .env file in the root directory of project and add the following variables.

LOG_FILE=log.txt
LOG_FORMAT=common
LOG_SIZE=10M
LOG_INTERVAL=1d

Let's change our configuration to use these variables

require('dotenv').config() // load variables from .env file
const express = require('express');
const morgan = require('morgan'); // import morgan
const rfs = require("rotating-file-stream");

const app = express();

// MORGAN SETUP
// create a log stream
const rfsStream = rfs.createStream(process.env.LOG_FILE || 'log.txt', {
   size: process.env.LOG_SIZE || '10M',
   interval: process.env.LOG_INTERVAL || '1d',
   compress: 'gzip' // compress rotated files
});

// if log file defined then use rfs stream else print to console
app.use(morgan(process.env.LOG_FORMAT || "dev"), {
   stream: process.env.LOG_FILE ? rfsStream : process.stdout
});

// if log file is defined then also show logs in console
// else it will use the previous process.stdout to print to console
if(process.env.LOG_FILE) {
   app.use(morgan(process.env.LOG_FORMAT || "dev"));    
}

app.listen(3000, () => {
   console.debug('App listening on :3000');
});

With above configuration we can relax and let morgan handle the job of logging. Whenever an error occurs, visit the log file and you can track where things went wrong.

I hope this article helped you understand why it important to have a good logging system and how to add one in express.


This content originally appeared on DEV Community and was authored by Paras ?‍♂️


Print Share Comment Cite Upload Translate Updates
APA

Paras ?‍♂️ | Sciencx (2021-06-05T16:26:54+00:00) Node.js Morgan Guide. Retrieved from https://www.scien.cx/2021/06/05/node-js-morgan-guide/

MLA
" » Node.js Morgan Guide." Paras ?‍♂️ | Sciencx - Saturday June 5, 2021, https://www.scien.cx/2021/06/05/node-js-morgan-guide/
HARVARD
Paras ?‍♂️ | Sciencx Saturday June 5, 2021 » Node.js Morgan Guide., viewed ,<https://www.scien.cx/2021/06/05/node-js-morgan-guide/>
VANCOUVER
Paras ?‍♂️ | Sciencx - » Node.js Morgan Guide. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/06/05/node-js-morgan-guide/
CHICAGO
" » Node.js Morgan Guide." Paras ?‍♂️ | Sciencx - Accessed . https://www.scien.cx/2021/06/05/node-js-morgan-guide/
IEEE
" » Node.js Morgan Guide." Paras ?‍♂️ | Sciencx [Online]. Available: https://www.scien.cx/2021/06/05/node-js-morgan-guide/. [Accessed: ]
rf:citation
» Node.js Morgan Guide | Paras ?‍♂️ | Sciencx | https://www.scien.cx/2021/06/05/node-js-morgan-guide/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.