0.6.0 • Published 5 months ago

@digipolis/request-log v0.6.0

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

CI Coverage Status npm version

npm: npmjs.com/package/@digipolis/request-log

@digipolis/request-log

Request Log helper to log incoming / outgoing calls.

Table of contents:

Spec

https://github.com/digipolisantwerpdocumentation/logging-requirements

Installing

npm:

$ npm i @digipolis/request-log

Yarn:

$ yarn add @digipolis/request-log

Configuration

Params:
ParamDescriptionValues
type (optional)Set logging modelog (default) / json / text /silent/
correlationIdLocation (optional)Set correlation Location for BFFundefined (default will search the req.headers) / id (point to req.id)
correlationIdfallback (optional)Set correlationId fallbackundefined (default will not fallback) / string (will replace correlationId in output if missing)
logResponsePayload (optional)log response payloadtrue / false (default)
logResponseHeaders (optional)log response headerstrue (all headers) / ["headername1", "headername2"] (log headers in array) / false (default)
logRequestPayload (optional)log request headerstrue (default) / false (return)
logRequestHeaders (optional)log request payloadtrue (all headers) / ["headername1", "headername2"] (log headers in array) / false (default)
logRequestSearchParams (optional)log request query parameterstrue / false (default)

Example:

Example log specific headers & body:

const { requestlogger, requestMiddleware } = require('@digipolis/request-log');

const config = {
  type: 'json',
};

// log external requests
requestlogger(config);

function initializeExpress() {
   const app = express();
   // log incoming requests
   app.use(requestMiddleware(config));
   app.post('/internalcall', (req, res) => res.json({ ok: 'ok' }));
}
Request
$ curl -X "POST" "http://localhost:2000/internalcall" \
     -H 'supersecret: ???' \
     -H 'Dgp-Correlation: correlationid' \
     -d $'{
  "a": "b",
  "d": "c"
}'
output
// output
{
  timestamp: '2021-12-17T10:28:26.505Z',
  type: [ 'application' ],
  level: 'INFO',
  correlationId: 'correlationid',
  request: { host: 'localhost:2000', path: '/internalcall', method: 'POST' },
  response: { status: 200, duration: 2 },
  protocol: 'http'
}

Example log specific headers & body BFF:

const { requestlogger, requestMiddleware } = require('@digipolis/request-log');

const config = {
  type: 'json',
  correlationIdLocation: 'id', // deeper nested property 'meta.id'
};

// log external requests
requestlogger(config);

function initializeExpress() {
   const app = express();
   // For BFF type applications the browser will not set the DGP header so we set it ourselves
   app.use((req, res, next) => {
      req.id = uuidv4(); // b207d502-de0f-4467-9a1e-2dd032fbe84d
      return next();
   });
   // log incoming requests
   app.use(requestMiddleware(config);
   app.post('/internalcall', (req, res) => res.json({ ok: 'ok' }));
}
Request
$ curl -X "GET" "http://localhost:2000/home" \
output
// output
{
  timestamp: '2021-12-17T10:28:26.505Z',
  type: [ 'application' ],
  level: 'INFO',
  correlationId: 'b207d502-de0f-4467-9a1e-2dd032fbe84d',
  request: { host: 'localhost:2000', path: '/home', method: 'GET' },
  response: { status: 200, duration: 2 },
  protocol: 'http'
}

Example log specific headers & body:

const { requestlogger, requestMiddleware } = require('@digipolis/request-log');

const config = {
  type: 'json',
  logResponsePayload: true,
  logRequestHeaders: ['dgp-correlation'],
  logRequestPayload: true,
  logResponseHeaders: ['x-powered-by'],
};

// log external requests
requestlogger(config);

function initializeExpress() {
   const app = express();
   // log incoming requests
   app.use(requestMiddleware(config);
   app.post('/internalcall', (req, res) => res.json({ ok: 'ok' }));
}
Request
$ curl -X "POST" "http://localhost:2000/internalcall" \
     -H 'supersecret: ???' \
     -H 'Dgp-Correlation: correlationid' \
     -d $'{
  "a": "b",
  "d": "c"
}'
output
// output
{
  timestamp: '2021-12-17T10:26:20.205Z',
  type: [ 'application' ],
  level: 'INFO',
  correlationId: 'correlationid',
  request: {
    headers: { 'dgp-correlation': 'correlationid' },
    host: 'localhost:2000',
    path: '/internalcall',
    method: 'POST',
    payload: {}
  },
  response: {
    headers: { 'x-powered-by': 'Express' },
    status: 200,
    duration: 1,
    payload: '{"ok":"ok"}'
  },
  protocol: 'http'
}

Example with full headers & body:

const { requestlogger, requestMiddleware } = require('@digipolis/request-log');


const config = {
  type: 'json',
  logResponsePayload: true,
  logRequestHeaders: true,
  logRequestPayload: true,
  logResponseHeaders: true,
};
// log external requests
requestlogger(config);

function initializeExpress() {
   const app = express();
   // log incoming requests
   app.use(requestMiddleware(config);
   app.post('/internalcall', (req, res) => res.json({ ok: 'ok' }));
}
Request
$ curl -X "POST" "http://localhost:2000/internalcall" \
     -H 'supersecret: ???' \
     -H 'Dgp-Correlation: correlationid' \
     -d $'{
  "a": "b",
  "d": "c"
}'
output
// output
{
  timestamp: '2021-12-17T10:23:08.238Z',
  type: [ 'application' ],
  level: 'INFO',
  correlationId: 'correlationid',
  request: {
    headers: {
      supersecret: '???',
      'dgp-correlation': 'correlationid',
      host: 'localhost:2000',
      connection: 'close',
      'content-length': '17'
    },
    host: 'localhost:2000',
    path: '/internalcall',
    method: 'POST',
    payload: {}
  },
  response: {
    headers: [Object: null prototype] {
      'x-powered-by': 'Express',
      'content-type': 'application/json; charset=utf-8',
      'content-length': '11',
      etag: 'W/"b-2F/2BWc0KYbtLqL5U2Kv5B6uQUQ"'
    },
    status: 200,
    duration: 2,
    payload: '{"ok":"ok"}'
  },
  protocol: 'http'
}

Running the tests

Run the tests in this repo:

$ npm run test
$ npm run coverage

Dependencies

Versioning

We use SemVer

for versioning. For the released version check changelog / tags

Contributing

Pull requests are always welcome, however keep the following things in mind:

  • New features (both breaking and non-breaking) should always be discussed with the repo's owner. If possible, please open an issue first to discuss what you would like to change.
  • Fork this repo and issue your fix or new feature via a pull request.
  • Please make sure to update tests as appropriate. Also check possible linting errors and update the CHANGELOG.md if applicable.

Support

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License - see the LICENSE.md file for details

0.6.0

5 months ago

0.5.4

7 months ago

0.5.3

9 months ago

0.5.2

9 months ago

0.5.0

1 year ago

0.5.1

1 year ago

0.4.0

2 years ago

0.3.2

2 years ago

0.3.1

2 years ago

0.3.0

2 years ago

0.2.2

2 years ago

0.2.1

2 years ago

0.2.0

2 years ago

0.1.0

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago