Express middleware to log the http request data to a csv file and output to the console.
$ npm install --save serverlogger
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:
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
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
MIT