So frequently in Dexter and the "morgan" library, we are left wondering, where's the body?
Well, we've found it!
(for "morgan" library, not the show :P)
Here is logging the way you always wanted it to be!
Nicely colorized logging that includes Request and Response bodies.
(Now with Typescript support thanks to @francisbrito)
Note: unlike typical express middleware you must pass the actual app into the function
import morganBody from 'morgan-body';
import express from 'express';
import bodyParser from 'body-parser';
const app = express();
// must parse body before morganBody as body will be logged
app.use(bodyParser.json());
// hook morganBody to express app
morganBody(app);
Options are:
{
noColors: (default: false), gets rid of colors in logs, while they're awesome, they don't look so good in log files as @rserentill pointed out
maxBodyLength: (default: 1000), caps the length of the console output of a single request/response to specified length,
prettify: (default: true), prettifies the JSON request/response body (may want to turn off for server logs),
logReqDateTime: (default: true), setting to false disables logging request date + time,
dateTimeFormat: (default: 'utc', available: ['edt', clf', 'iso', 'utc']), lets you specify dateTime format logged if "logDateTime" option is true (otherwise dateTime not logged anyways)
timezone: (default : server's local timezone), time will be logged in the specified timezone. e.g. "EST", "America/Los_Angeles", "Asia/Kolkata" (for Indian Standard Time), etc. Internally uses "momentjs" for interpreting the timezone, and if specified value is not understood by momentjs, falls back to using the local timezone. (Please have a look at the TZ column here for a lit of supported timezone strings: https://wikipedia.org/wiki/List_of_tz_database_time_zones#List).
logReqUserAgent: (default: true), setting to false disables logging request user agent,
logRequestBody: (default: true), setting to false disables logging request body,
logReqHeaderList: (default: false), takes in a list of request headers to be displayed in the log.
logAllReqHeader: (default: false), true will log All request headers and take precedence over logReqHeaderList; false otherwise.
logResponseBody: (default: true), setting to false disables logging response body,
logResHeaderList: (default: false), takes in a list of response headers to be displayed in the log.
logAllResHeader: (default: false), true will log All response headers and take precedence over logResHeaderList; false otherwise.
skip: (default: null), optionally provide function of the signature "(req, res) => <bool>" to conditionally skip logging of requests (if provided function returns true),
stream: (default: null), optionally provide a stream (or any object of the shape { write: <Function> }) to be used instead of "process.stdout" for logging to,
theme: (default: 'defaultTheme'), alter the color scheme of your logger with a theme, see available themes below
filterParameters: (default: []), set the properties you don't want to be shown, such as passwords or credit card numbers
}
Can be passed in as "theme" option, screenshots taken in iTerm2 (note that some text is not visible in some screenshots, this is because this text is colored non-intense black, it would show up on white-background terminals).
reverse ASCII color of default
no white (all colors rotated one away from white)