All files / libs/kernel/logger/server/src/lib/config nestjs-pino.config.ts

0% Statements 0/57
0% Branches 0/1
0% Functions 0/1
0% Lines 0/57

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58                                                                                                                   
import { type Params } from 'nestjs-pino/params';
import { multistream, stdTimeFunctions, type Level } from 'pino';
import { type PrettyOptions } from 'pino-pretty';

import { toGcpSeverity } from '../gcp-logging/mapper';

import { type LoggerConfig } from './logger.config';

const prettyPrintTransport: {
  target: 'pino-pretty';
  options: PrettyOptions;
} = {
  target: 'pino-pretty',
  options: {
    colorize: true,
    singleLine: true,
    levelFirst: true,
    messageKey: 'message',
    messageFormat: '{if company}({company.name}){end} {message}',
  },
};

export const nestjsPinoConfig = (config: LoggerConfig): Params => ({
  pinoHttp: {
    level: config.level,
    transport: config.prettyPrint ? prettyPrintTransport : undefined,
    autoLogging: config.enableAccessLogging,
    quietReqLogger: !config.enableRequestLogging,
    timestamp: stdTimeFunctions.isoTime,
    messageKey: 'message',
    errorKey: 'error',
    /**
     * @see https://getpino.io/#/docs/help?id=log-to-different-streams
     * @see https://getpino.io/#/docs/api?id=options-1
     *
     * We use stderr for error & fatal level logs and stdout as default for all other levels (e.g. debug, info, and warn).
     */
    stream: multistream(
      [
        { level: 'debug', stream: process.stdout },
        { level: 'error', stream: process.stderr },
      ],
      { dedupe: true },
    ),
    formatters: {
      bindings: (bindings) => ({
        node_version: process.version,
        ...bindings,
      }),
      level: (label: string, level: number) => ({
        level,
        ...toGcpSeverity(label as Level),
      }),
    },
  },
  forRoutes: ['*path'],
});