1.0.5 • Published 3 years ago

custom-pino-logger v1.0.5

Weekly downloads
-
License
ISC
Repository
-
Last release
3 years ago

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