Gathering detailed insights and metrics for morgan-body
Gathering detailed insights and metrics for morgan-body
Gathering detailed insights and metrics for morgan-body
Gathering detailed insights and metrics for morgan-body
npm install morgan-body
Typescript
Module System
Node Version
NPM Version
68.4
Supply Chain
95.4
Quality
75.6
Maintenance
100
Vulnerability
100
License
JavaScript (100%)
Total Downloads
4,604,680
Last Day
917
Last Week
10,653
Last Month
70,078
Last Year
903,930
101 Stars
190 Commits
20 Forks
5 Watching
12 Branches
10 Contributors
Latest Version
2.6.9
Package Id
morgan-body@2.6.9
Unpacked Size
37.75 kB
Size
11.87 kB
File Count
5
NPM Version
8.19.3
Node Version
16.19.0
Publised On
06 May 2023
Cumulative downloads
Total Downloads
Last day
-66.6%
917
Compared to previous day
Last week
-33.2%
10,653
Compared to previous week
Last month
-17.3%
70,078
Compared to previous month
Last year
-12.4%
903,930
Compared to previous year
4
5
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
1import morganBody from 'morgan-body'; 2import express from 'express'; 3import bodyParser from 'body-parser'; 4 5const app = express(); 6 7// must parse body before morganBody as body will be logged 8app.use(bodyParser.json()); 9 10// hook morganBody to express app 11morganBody(app);
Please make sure to call morganBody before registering routers (but after registering body-parser), because otherwise they wouldn't be logged.
*Note: console output is colorized for iTerm2, might look odd on terminals with other background colors, which can be solved by themes!In order to do that, you just need to pass a stream into the stream
property in options. Example:
1const log = fs.createWriteStream(
2 path.join(__dirname, "logs", "express.log"), { flags: "a" }
3);
4
5morganBody(app, {
6 // .. other settings
7 noColors: true,
8 stream: log,
9});
If your log files look like this:
You just need disable the colors with the noColors
property in options.
If you want to use morganBody to log on multiple places, it can be done by just calling the function multiple times. As shown in the previous example, you can log to write streams. But what if you want to log to console as well? Easy.
1// ... express
2
3const log = fs.createWriteStream(
4 path.join(__dirname, "logs", "express.log"), { flags: "a" }
5);
6
7morganBody(app, {
8 // .. other settings
9});
10
11morganBody(app, {
12 // .. other settings
13 noColors: true,
14 stream: log,
15});
What if you use a different logging library to log all your important information about what's happening with your application? You can use MorganBody with it as well!
Example with winston:
1import winston from 'winston'
2
3// ... express
4
5const logger = winston.createLogger({
6 transports: [
7 new winston.transports.Console(),
8 new winston.transports.File({
9 format: format.combine(format.timestamp(), loggerFormat),
10 filename: path.join(__dirname, "../", "logs", "combined.log"),
11 }),
12 ],
13});
14
15const loggerStream = {
16 write: message => {
17 logger.info(message);
18 },
19};
20
21morganBody(app, {
22 // .. other settings
23 stream: loggerStream
24});
25
As you could've seen in the winston example, we don't even have a WriteStream here! You can just create an object with a write function inside it,and do whatever you want inside it!
1const loggerStream = {
2 write: message => {
3 // do anything - emit to websocket? send message somewhere? log to cloud?
4 },
5};
6
7morganBody(app, {
8 // .. other settings
9 stream: loggerStream
10});
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),
includeNewLine: (default: true), adds new line after each log entry (a common use case for making this `false` is if `prettify` is false).
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,
logRequestId: (default: false), setting to true will log "req.id" at the beginning of each line (must be setting req.id elsewhere upstream),
logIP: (default: true), setting to true will log request IP,
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
immediateReqLog: (default: true), logs request immediately (instead of waiting until response goes out)
}
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)
no black (all colors rotated one away from black)
only non-"intense" colors
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 3/12 approved changesets -- score normalized to 2
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
11 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-12-16
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More