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

express-id-log-decorator

v1.0.1

Published

Express.js Request Unique ID logger decorator

Downloads

55

Readme

express-id-log-decorator

A logger decorator to log request id on each log output.

express-id-log-decorator checks the http context, using the express-http-context module, to get the request id to log.

If the requestId is not present in the request, the log message will remain unchanged.

Contents

Installation

npm install express-id-log-decorator

Http context and Request ID

You need to initialize and use the express-http-context middleware in your application. To generate and add request id for your app, you can use any middleware you want, but you have to set this request id in the http context. So, if you want, you can use the express-ruid package which will add the generated request id in the Express req object and in your http context for you, as in the following example:

const express = require('express');
const ruid = require('express-ruid');
const httpContext = require('express-http-context');

const app = express();
...
...
app.use(httpContext.middleware);
app.use(ruid{ setInContext: true });

Request ID attribute name

The decorator will search in context the default 'rid' attribute. If you confiure the express-ruid to use a custom request id attribute name, or if you use a different request id middleware and you set by hand the request id in the context with a different name than 'rid', than you have to configure the attribute options when you apply the decorator:

const express = require('express');
const ruid = require('express-ruid');
const httpContext = require('express-http-context');
const idLogDecorator = require('./logger/log-rid-decorator');

const app = express();
...
...
app.use(httpContext.middleware);
app.use(ruid{ setInContext: true, attribute: 'requestId' });
...
...
// the Decorator will search for 'requestId' instead of 'rid'
idLogDecorator.decorate({ logger: theLogger, attribute: 'requestId' });

Decorator

To get your id logged into each log line, you can just decorate your logger functions with the express-id-log-decorator decorate(options = {}) function. As an example, using Winston logger:

const winston = require('winston');
const winstonLogger = winston.createLogger();
const idLogDecorator = require('./logger/log-rid-decorator');
...
...
winstonLogger.add(new winston.transport.Console({
    format: new winston.transports.simple({
        level: 'info'
    })
}));

idLogDecorator.decorate({ logger: winstonLogger });

Now you will have your logger functions decorated and able to log the request id as message prefix.

Functions

By default, the decorator will be applied to the following logger functions:

  • log()
  • debug()
  • error()
  • warn()

as specified by the internal array defaultFunctions:

const defaultFunctions = [
    'log',
    'info',
    'debug',
    'error',
    'warn'
];

If you want to change the functions to decorate you can configure the functions options when you apply the decoration:

const idLogDecorator = require('./logger/log-rid-decorator');
...
...
...
// if you want the request id only in production for 'info' and 'error'
idLogDecorator.decorate({ logger: theLogger, functions: ['info', 'error'] });
...
...
// if you want the request id only in production for 'info' you can confiure
// the "functions" option with a string value
idLogDecorator.decorate({ logger: theLogger, functions: 'info' });

Request ID substring format in log line

You can specify how to format the request id sub-string in the log line, configuring the format option with a custom function. For example you can specify to print the id using color, or you can specify some prefix and suffix. The format option can be configured only with a function accepting one parameter (the request id), otherwhise you get and error. Here are some exaples:

const idLogDecorator = require('./logger/log-rid-decorator');
...
...
...
// to log the request id in green color
idLogDecorator.decorate({
    logger: theLogger,
    format: (rid) => chalk.green(`${rid}`)
});

// to log whatever you want before and after
idLogDecorator.decorate({
    logger: theLogger,
    format: function (rid) {
        return `SOMETHING BEFORE ${rid} AND OTHER AFTER`;
    }
});

The default output is [${rid}] as defined by the internal defaultFormat() function:

function defaultFormat(rid) {
    return `[${rid}]`;
}

Options

decorate() supports the following options:

  • logger: (object) the logger object with logging functions to decorated
  • attribute: (string) to specify the attribute name to search for in the Http Context
  • format: (function) which accept the requestId and return a formatted string
  • functions (array | string) array of name of the functions to decorate

License

express-id-log-decorator is MIT licensed.