All files / libs/observability/src logging.ts

100% Statements 5/5
100% Branches 10/10
100% Functions 3/3
100% Lines 5/5

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    17x             17x 6x             1x         4x            
import type { IncomingMessage } from 'node:http';
import type { Params } from 'nestjs-pino';
import { REQUEST_ID_HEADER } from './request-context.middleware';
 
/**
 * One JSON log line per request, tagged with the service name and the
 * request id minted by requestContextMiddleware. Silent under tests;
 * pipe through `pino-pretty` for local reading.
 */
export function buildLoggerOptions(serviceName: string): Params {
  return {
    pinoHttp: {
      name: serviceName,
      level:
        process.env.LOG_LEVEL ??
        (process.env.NODE_ENV === 'test' ? 'silent' : 'info'),
      genReqId: (req: IncomingMessage) =>
        req.headers[REQUEST_ID_HEADER] as string,
      redact: ['req.headers.authorization'],
      autoLogging: {
        // Probes and scrapes would drown real traffic in the logs.
        ignore: (req: IncomingMessage) =>
          (req.url ?? '').includes('/health') ||
          (req.url ?? '').includes('/metrics'),
      },
    },
  };
}