console-request
HTTP request logger middleware for node.js
Table of Contents
Why?
A simple logging library for multi-level log for console and file.
Installation
$ npm i console-request -s
Functions
Take a look into the usage section for a detailed example.
console-request
Note: Use this Middleware below body-parser Middleware.
This Middleware Takes an object as an parameter and logs the request body, request headers and response.
Usage
Example 1
Note: it will logs all API
;;; const app = ; // must parse body before console-request as body will be loggedapp; // consoleRequest middleware logs the request app;
Example 2
Note: it will logs Particular router
;;;; const app = ; // must parse body before console-request as body will be loggedapp; // consoleRequest middleware logs only user Router app;
Options
console-request accepts these properties in the options object.
console
log the header, request and response in the console (default: true), it accepts boolean
app;
stream
write the header, request and response in file. it accepts writable stream
var logStream = fs app;
header
write the header of the request. it accepts boolean or object (defalut: true)
- pretty: To formate header object (default: true)
- excludeKeys: it accepts array of key that can be excluded
app;
OR
app;
request
write the request body of the request. it accepts boolean or object (defalut: true)
- pretty: To formate request body object (default: true)
- excludeKeys: it accepts array of key that can be excluded
app;
OR
app;
response
write the response of the api. it accepts boolean or object (defalut: true)
- pretty: To formate response object (default: true)
- excludeKeys: it accepts array of key that can be excluded
app;
OR
app;
message
write the custom message of the api. it accepts string
app;
excludeURLs
exclude some urls to write
app;
write logs to a file
single file
Sample app that will log all requests in the to the file access.log
.
var express = var fs = var consoleRequest = var path = var app = // create a write stream (in append mode)var logStream = fs app; app
log file rotation
Sample app that will log all requests in the log file per day in the log/
directory using the
rotating-file-stream module.
var express = var consoleRequest = var path = var rfs = // version 2.x var app = // create a rotating write streamvar accessLogStream = rfs // setup the loggerapp;app