Skip to content

Commit

Permalink
fix: Log level not honored with multi transport (#10643)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
spalladino authored Dec 11, 2024
1 parent 00c629c commit e1e5864
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions yarn-project/foundation/src/log/pino-logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand All @@ -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
Expand All @@ -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() {
Expand All @@ -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 }));
}
}

Expand Down

0 comments on commit e1e5864

Please sign in to comment.