Skip to content

Express middleware to log the http request data to a csv file and output to the console.

License

Notifications You must be signed in to change notification settings

rabikr/Serverlogger

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Serverlogger

Express middleware to log the http request data to a csv file and output to the console.

Install

$ npm install --save serverlogger

Usage

By default, Serverlogger outputs the log file "serverlog.csv" in the root folder, enables log to console, enables write to file, and set console message color to green.

const serverLogger = require("serverlogger");

app.use(serverLogger());    // Express middleware

You can also override the default settings by passing an object.

const serverLogger = require("serverlogger");

// Express middleware
app.use(serverLogger({
    fileName: "log.csv",    // By default, file output location is the root folder
    toConsole: false,       // By default, toConsole is set to true
    toFile: true,           // By default, toFile is set to true
    color: 4                // By default, color set to 0 (green)
})); 

Color codes are as follows:

  • 00. Green
  • 11. Black
  • 22. Gray
  • 33. White
  • 44. Red
  • 55. Yellow
  • 66. Blue
  • 77. Magenta
  • 88. Cyan

Example

Example of a simple Express server with default serverlogger settings

const express = require("express");             // Get express framework
const serverLogger = require("serverlogger");   // Get serverlogger
const app = express();                          // Initialize a new express app

app.use(serverLogger());                        // Use serverlogger as express middleware

app.get("/", (req, res) => {                    // Express handle get request on '/' route
    res.send("Homepage");
});

const HOST = process.env.IP || "127.0.0.1";     // Only listen to IPv4
app.listen(3000, HOST);                         // Start the server

Example of the console when a user requested the "/" route

$ node app.js
GET     127.0.0.1                       /

If a user is authenticated using passport.js module, the username will also be displayed

$ node app.js
GET     127.0.0.1                       /
GET     127.0.0.1                       /auth/login
POST    127.0.0.1                       /auth/login
GET     127.0.0.1       rabik           /dashboard

File structure example

By default, serverlogger keeps the log file in the root folder of your project.

|--node_modules
        |--(other modules)
        |--serverlogger
|--package.json
|--index.js
|--serverlog.csv

If you wish to change the default file output path, change the fileName settings with the correct path.

app.use(serverLogger({
    fileName: "/logs/log.csv"
})); 
|--node_modules
        |--(other modules)
        |--serverlogger
|--logs
    |--log.csv
|--package.json
|--index.js

License

MIT

About

Express middleware to log the http request data to a csv file and output to the console.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published