From e1e586479840b18f52f3218c499a476691d93e48 Mon Sep 17 00:00:00 2001 From: Santiago Palladino Date: Wed, 11 Dec 2024 20:31:50 -0300 Subject: [PATCH] fix: Log level not honored with multi transport (#10643) When multiple transports were set (eg logging to stdout and to the otel collector) the individual transports defaulted to level INFO, so nothing below that level was being logged. We fix this by setting the individual transports to TRACE, and controlling the actual log level via the logger itself. --- .../foundation/src/log/pino-logger.ts | 29 ++++++++++--------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/yarn-project/foundation/src/log/pino-logger.ts b/yarn-project/foundation/src/log/pino-logger.ts index 02068fc2930..1cc354b03bc 100644 --- a/yarn-project/foundation/src/log/pino-logger.ts +++ b/yarn-project/foundation/src/log/pino-logger.ts @@ -67,6 +67,15 @@ function isLevelEnabled(logger: pino.Logger<'verbose', boolean>, level: LogLevel const defaultLogLevel = process.env.NODE_ENV === 'test' ? 'silent' : 'info'; const [logLevel, logFilters] = parseEnv(process.env.LOG_LEVEL, defaultLogLevel); +// Define custom logging levels for pino. +const customLevels = { verbose: 25 }; +const pinoOpts = { customLevels, useOnlyCustomLevels: false, level: logLevel }; + +export const levels = { + labels: { ...pino.levels.labels, ...Object.fromEntries(Object.entries(customLevels).map(e => e.reverse())) }, + values: { ...pino.levels.values, ...customLevels }, +}; + // Transport options for pretty logging to stderr via pino-pretty. const useColor = true; const { bold, reset } = createColors({ useColor }); @@ -82,24 +91,17 @@ export const pinoPrettyOpts = { singleLine: !['1', 'true'].includes(process.env.LOG_MULTILINE ?? ''), }; -const prettyTransport: pino.TransportSingleOptions = { +const prettyTransport: pino.TransportTargetOptions = { target: 'pino-pretty', options: pinoPrettyOpts, + level: 'trace', }; // Transport for vanilla stdio logging as JSON. -const stdioTransport: pino.TransportSingleOptions = { +const stdioTransport: pino.TransportTargetOptions = { target: 'pino/file', options: { destination: 2 }, -}; - -// Define custom logging levels for pino. -const customLevels = { verbose: 25 }; -const pinoOpts = { customLevels, useOnlyCustomLevels: false, level: logLevel }; - -export const levels = { - labels: { ...pino.levels.labels, ...Object.fromEntries(Object.entries(customLevels).map(e => e.reverse())) }, - values: { ...pino.levels.values, ...customLevels }, + level: 'trace', }; // Transport for OpenTelemetry logging. While defining this here is an abstraction leakage since this @@ -110,9 +112,10 @@ export const levels = { // since pino will load this transport separately on a worker thread, to minimize disruption to the main loop. const otlpEndpoint = process.env.OTEL_EXPORTER_OTLP_LOGS_ENDPOINT; const otelOpts = { levels }; -const otelTransport: pino.TransportSingleOptions = { +const otelTransport: pino.TransportTargetOptions = { target: '@aztec/telemetry-client/otel-pino-stream', options: otelOpts, + level: 'trace', }; function makeLogger() { @@ -131,7 +134,7 @@ function makeLogger() { ['1', 'true', 'TRUE'].includes(process.env.LOG_JSON ?? '') ? stdioTransport : prettyTransport, otlpEndpoint ? otelTransport : undefined, ]); - return pino(pinoOpts, pino.transport({ targets })); + return pino(pinoOpts, pino.transport({ targets, levels: levels.values })); } }