simple-express-logs
Simple express logging middleware
Are you prototyping an Express app and want a simple way to log all requests? Good news, you're looking at it.
Install
$ npm install --save simple-express-logs
Usage
const express = require('express');
const logger = require('simple-express-logs');
const app = express();
app.use(logger());
Or, in one line:
app.use(require('simple-express-logs')());
Output
By default, simple-express-logs
outputs a timestamp, whether the request was made over HTTP or HTTPS, the HTTP verb, the path, the source IP, and the reverse DNS host (if available.)
20:06 1/15/2019 UTC: https POST /api/items/ from 24.63.26.119
5:17 1/8/2019 UTC: https GET /favicon.ico from 66.249.66.152 (crawl-66-249-66-152.googlebot.com)
20:06 1/15/2019 UTC: http GET / from 24.63.26.119 (c-24-63-26-119.hsd1.ma.comcast.net) referred by https://www.google.com
20:06 1/15/2019 UTC: http GET /style.css from 24.63.26.119 (c-24-63-26-119.hsd1.ma.comcast.net)
Options
Both the the header values and request body are not logged by default, but may be enabled when adding the middleware.
app.use(logger({ headers: true }));
23:15 1/25/2019 UTC: http POST /api/test/?type=sample Headers: { host: "localhost:8080", user-agent: "HTTPie/0.9.9", accept-encoding: "gzip, deflate", accept: "application/json, */*", connection: "keep-alive", content-type: "application/json", content-length: "24" }
Logging the body relies on the body
property of the request to be populated, so make sure to do that before using the logger.
app.use(bodyParser.json());
app.use(logger({ body: true }));
23:15 1/25/2019 UTC: http POST /api/test/?type=sample Body: {"VehicleColor":"Blue"}
By default, reverse DNS is used to log the request's host.
app.use(logger());
5:17 1/8/2019 UTC: https GET /favicon.ico from 66.249.66.152 (crawl-66-249-66-152.googlebot.com)
This can be disabled with the reverseDNS
option.
app.use(logger({ reverseDNS: false }));
5:17 1/8/2019 UTC: https GET /favicon.ico from 66.249.66.152
License
MIT © Jesse T Youngblood