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

koa-mw-logger

v1.7.9

Published

Koa middleware for logging

Downloads

307

Readme

koa-mw-logger

NPM

Koa middleware for logging

What it does

  • Every time a request is received, a request id will forwarded or generated and attached to the ctx. It reads the following header keys for re-using the request id: x-request-id, x-amzn-RequestId and requestId.
  • At the completion of the request, it will log the completion of the request with some useful information like request headers, response status code and message, any custom log context attached to it etc.
  • It will also attach a logger instance at the ctx. It can be used to log the objects. Every call to the logging the object will be appended by the request id generated during the start of the request.
  • Appends a function called addCustomLogCtx to add additional info to the final log statement.

How to use

const Koa = require("koa");
const getLogger = require("koa-mw-logger");

const app = Koa();

app.use(getLogger());

to use it in a handler

async function handler(ctx, next) {
  const logger = ctx.logger;
  logger.info({"message": "logging something"});

  // to append custom log context to final staetment

  ctx.addCustomLogCtx = {
    "foo": "bar"
  };
}

Config

There is an option to pass in the config as per the following structure to define some properties of logger.

// config structure
{
  "name": "foo", // name of the logger
  "recordIp": false // whether to extract and record ip from the request object or not. Default value is false.
  "recordHeaders": false // whether to extract and record request headers from the request object or not. Default value is false It will also obfuscate some headers from logging them.
  "excludePaths": [] // strings to match against the path. If the path includes those strings, the log will not be printed.
}
const Koa = require("koa");
const getLogger = require("koa-mw-logger");

const config = {
  "name": "test"
};

const app = Koa();

app.use(getLogger(config));

Graphql Partial Errors

If you are using the middleware with a Graphql server implemented using Koa, the partial errors from resolvers are also logged by this middleware. The response in case of the partial error is returned as 200 with errors in the body for a graphql API. The middleware will look for a key errors in the response and will log it in error object.

Support or Contact

Having trouble with koa-mw-logger or have any questions? Please raise an issue.