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

custom-pino-logger

v1.0.5

Published

Pino logger customized

Downloads

2

Readme

Customized Pino logger

Customized logging with Pino and includes HTTP middleware for request tracing + a request logging interceptor.

Configuring the logger for Node Express

import { express } from 'express';
import { LoggerService } from 'custom-pino-logger';

const app = express();

LoggerService.register();
const logger = LoggerService.getLogger();

logger.info('Log message here', { context: 'MyApp' });

app.listen(3000, () => {
    logger.info('Example app listening at http://localhost:3000',
    { context: 'MyApp' });
});

Outputs:

[2021-07-20 07:49:03] INFO: [MyApp]: Log message here
    environment: "ENVIRONMENT NOT SET"
    context: "MyApp"
[2021-07-20 07:49:03] INFO: [MyApp]: Example app listening at http://localhost:3000
    environment: "ENVIRONMENT NOT SET"
    context: "MyApp"

Register method

The register method should be only called once and can contain a configuration parameter. The default values are:

{
    environment: 'ENVIRONMENT NOT SET',
    logLevel: 'trace',
    enableTraceId: false,
    masking: false,
    prettyPrint: true
}

Note: the logger should only get registered once. Afterwards you can simply use it by calling the getLogger method.

const logger = LoggerService.getLogger();

Log level

The logLevel setting can contain one of the values below:

type LogLevel =
    | 'silent'
    | 'trace'
    | 'debug'
    | 'info'
    | 'warn'
    | 'error'
    | 'fatal';

When you set a log level, only the logs with a more serious log level are displayed. For example, setting warn will only display the logs with levels warn, error and fatal.

The log levels trace, debug or info are commonly used for development environments. The log level warn is mostly used for production environments.

Enable trace ID

When enabled, the enableTraceId sets a unique traceId per request. Note, this is only possible when the tracing middleware is used. The logger should be accessible from the request and the traceId as well.

import { LoggerService, httpTraceMiddleware, traceIdHeaderName } from 'custom-pino-logger';
...
const logger = LoggerService.getLogger();

app.use(httpTraceMiddleware);

app.get('/', (req, res, next) => {
    req.logger.info('Trace ID logging included', {
        context: 'MyApp',
        accessTraceId: req.headers[traceIdHeaderName]
    });

    next();
});
...

Outputs:

[2021-07-16 16:53:12] INFO: [MyApp]: Trace ID logging included
    environment: "ENVIRONMENT NOT SET"
    traceId: "2b9d637b-5c63-4c66-aa6c-df8828242977"
    context: "MyApp",
    accessTraceId: "2b9d637b-5c63-4c66-aa6c-df8828242977"

Masking

To redact/remove sensitive information from the logs you can set the masking setting to true.

Currently these nested properties will be removed from the logs:

[
    '*.password',
    '*.newPassword',
    '*.oldPassword',
    '*.accessToken',
    '*.refreshToken',
    '*.token',
    '*.jwtToken',
    '*.apiKey',
]

Pretty print

When turned on, pretty, multi-line logs will be output. When turned off, one-line stringified JSON will be output. Advised to turn this off in staging or production environments.

HTTP interceptor

HTTP request can be intercepted and logged automatically by simply adding it as middleware.

import { LoggerService, loggerInterceptor } from 'custom-pino-logger';
...
const logger = LoggerService.getLogger();

app.use(loggerInterceptor(logger));
...

Outputs:

[2021-07-16 16:53:12] INFO: [Request]: GET /
    environment: "ENVIRONMENT NOT SET"
    traceId: "2b9d637b-5c63-4c66-aa6c-df8828242977"
    context: "Request"
    status: 200
    duration: "1ms"
    params: {}
    query: {}
    host: "localhost"

Sources

  • https://itnext.io/step-by-step-building-and-publishing-an-npm-typescript-package-44fe7164964c
  • https://www.npmjs.com/package/pino