npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@digipolis/request-log

v0.6.0

Published

Log express and external Requests in digipolis format

Downloads

36

Readme

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:

| Param | Description | Values | | :--- | :--- | :--- | | type (optional) | Set logging mode | log (default) / json / text /silent/ | | correlationIdLocation (optional) | Set correlation Location for BFF | undefined (default will search the req.headers) / id (point to req.id) | | correlationIdfallback (optional) | Set correlationId fallback | undefined (default will not fallback) / string (will replace correlationId in output if missing) | | logResponsePayload (optional) | log response payload | true / false (default) | | logResponseHeaders (optional) | log response headers | true (all headers) / ["headername1", "headername2"] (log headers in array) / false (default) | | logRequestPayload (optional) | log request headers | true (default) / false (return) | | logRequestHeaders (optional) | log request payload | true (all headers) / ["headername1", "headername2"] (log headers in array) / false (default) | | logRequestSearchParams (optional) | log request query parameters | true / 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