Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version 3 - Work in progress #39

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 0 additions & 13 deletions .circleci/config.yml

This file was deleted.

3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/node_modules
/dist
/expressus
25 changes: 25 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"root": true,
"parser": "@typescript-eslint/parser",
"plugins": ["@typescript-eslint"],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended"
],
"parserOptions": {
"ecmaVersion": 2020,
"sourceType": "module"
},
"rules": {
"no-tabs": "error",
"camelcase": "error",
"no-extra-semi": "error",
"no-console": "off",
"@typescript-eslint/explicit-module-boundary-types": "off",
"@typescript-eslint/ban-ts-comment": "off",
"@typescript-eslint/no-explicit-any": "off",
"no-prototype-builtins": "off",
"@typescript-eslint/ban-types": "off"
}
}
29 changes: 29 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Node.js CI

on: [push, pull_request]

jobs:
test:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [10.x, 12.x, 14.x]

steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: Set up Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}

- name: Set up yarn
run: npm i -g yarn

- name: Install dependencies
run: yarn

- name: Test
run: yarn test
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
/node_modules
/dist
/expressus

.nyc_output
tap-snapshots
# for now
coverage
file_transport--temp.log
2 changes: 2 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
## the stuff we want in npm
!index.js
!index.d.ts
!/dist
!dist
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.nyc_output
tap-snapshots
coverage
6 changes: 6 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"semi": false,
"useTabs": false,
"tabWidth": 2,
"arrowParens": "avoid"
}
239 changes: 32 additions & 207 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,207 +1,32 @@
# morgan-body

So frequently in Dexter and the "morgan" library, we are left wondering, where's the body?

Well, we've found it! <br> <i>(for "morgan" library, not the show :P)</i>

Here is logging the way you always wanted it to be! <br />
Nicely colorized logging that includes Request and Response bodies.

(Now with Typescript support thanks to [@francisbrito](https://github.com/francisbrito))

[![NPM][nodei-image]][nodei-url]

## Example Use
*Note: unlike typical express middleware you must pass the actual app into the function*
```js
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);
```

Please make sure to register routers before you call morganBody, because otherwise they wouldn't be logged

<img width="657" alt="screen shot 2017-07-07 at 2 02 55 am" src="https://user-images.githubusercontent.com/7177292/27944997-74491fa6-62b8-11e7-96c8-82dbf2e6b50c.png">
*Note: console output is colorized for iTerm2, might look odd on terminals with other background colors, which can be solved by themes!

## More Usages

### Log to file

In order to do that, you just need to pass a stream into the `stream` property in options. Example:
```js
const log = fs.createWriteStream(
path.join(__dirname, "logs", "express.log"), { flags: "a" }
);

morganBody(app, {
// .. other settings
noColors: true,
stream: log,
});
```

If your log files look like this:

<img width="796" src="https://user-images.githubusercontent.com/53915302/90952129-712da980-e461-11ea-90d6-8223acb2e07a.png">

You just need disable the colors with the `noColors` property in options.

### Multiple instances of MorganBody

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.
```js
// ... express

const log = fs.createWriteStream(
path.join(__dirname, "logs", "express.log"), { flags: "a" }
);

morganBody(app, {
// .. other settings
});

morganBody(app, {
// .. other settings
noColors: true,
stream: log,
});
```

### Using different loggers

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:
```js
import winston from 'winston'

// ... express

const logger = winston.createLogger({
transports: [
new winston.transports.Console(),
new winston.transports.File({
format: format.combine(format.timestamp(), loggerFormat),
filename: path.join(__dirname, "../", "logs", "combined.log"),
}),
],
});

const loggerStream = {
write: message => {
logger.info(message);
},
};

morganBody(app, {
// .. other settings
stream: loggerStream
});

```

### Execute code X on write

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!
```js
const loggerStream = {
write: message => {
// do anything - emit to websocket? send message somewhere? log to cloud?
},
};

morganBody(app, {
// .. other settings
stream: loggerStream
});
```


## API
### morganBody(\<express instance>, \<options object>)
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,

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
}
```

### Available Themes
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).

#### defaultTheme
<img width="798" alt="screen shot 2018-06-30 at 11 40 31 pm" src="https://user-images.githubusercontent.com/7177292/42130919-b84ebdea-7cc1-11e8-806d-a8b648bfec7c.png">

#### dracula
<img width="793" alt="screen shot 2018-06-30 at 11 37 32 pm" src="https://user-images.githubusercontent.com/7177292/42130920-bc468c8e-7cc1-11e8-9c9d-be8707e665da.png">

#### usa
<img width="793" alt="screen shot 2018-06-30 at 11 14 48 pm" src="https://user-images.githubusercontent.com/7177292/42130923-ccc26ede-7cc1-11e8-9437-8ffff36afa5e.png">

#### inverted
reverse ASCII color of default
<img width="796" alt="screen shot 2018-07-01 at 12 03 57 am" src="https://user-images.githubusercontent.com/7177292/42130934-4d278bc2-7cc2-11e8-9fac-4705f15e6207.png">

#### darkened
no white (all colors rotated one away from white)
<img width="793" alt="screen shot 2018-06-30 at 11 24 42 pm" src="https://user-images.githubusercontent.com/7177292/42130921-c1cc82d0-7cc1-11e8-9cb3-62f18e46ff22.png">

#### lightened
no black (all colors rotated one away from black)
<img width="791" alt="screen shot 2018-07-01 at 12 06 48 am" src="https://user-images.githubusercontent.com/7177292/42130947-b690439c-7cc2-11e8-819a-52ae4aa11db6.png">

#### dimmed
only non-"intense" colors
<img width="786" alt="screen shot 2018-07-01 at 12 05 35 am" src="https://user-images.githubusercontent.com/7177292/42130941-8b01874a-7cc2-11e8-9f48-e1bb1ff4e039.png">

[nodei-image]: https://nodei.co/npm/morgan-body.png?downloads=true&downloadRank=true&stars=true
[nodei-url]: https://www.npmjs.com/package/morgan-body
# Morgan Body Next

This is the development branch of the next release (v3).
Three main goals of v3:

- Rewrite in **TypeScript**
- Compatibility across **more** libraries via **drivers** (not be limited only to Express)
- Easier way to log on multiple places via **transports** (let user do custom implementation of logging)

## TODO

- ~~Implement transports~~
- ~~Complete codebase transfer to TypeScript~~
- Write new tests
- Find a way to easily test registerMiddleware functions
- ~~Cleanup some old code~~
- ~~Get rid of [expressjs/morgan](https://github.com/expressjs/morgan)~~
- Make sure all missing features are implemented
- ~~Skip~~
- ~~Express Driver Request Id~~
- ~~Filter Parameters~~
- ~~Different Timezone Formats~~
- Prettifying on other Transports than Console, Disable prettifying
- Max Body Length
- Fix all known issues (see [Known Issues](#known-issues))
- Write new README and a documentation
- Create new name, as `morgan-body` will become misleading

## Known Issues

- Themes do not work, except for defaultTheme (because it's values are static)
- ThemeUtils.skewDefaults is broken
34 changes: 0 additions & 34 deletions index.d.ts

This file was deleted.

Loading