Skip to content

Commit

Permalink
Merge d00a463 into 96887b6
Browse files Browse the repository at this point in the history
  • Loading branch information
alexghr authored Dec 18, 2024
2 parents 96887b6 + d00a463 commit 1f1224b
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion yarn-project/foundation/src/log/pino-logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,46 @@ 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 };

// inspired by https://github.com/pinojs/pino/issues/726#issuecomment-605814879
const levelToSeverityFormatter = (label: string, level: number): object => {
// Severity labels https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#LogSeverity
let severity: string;

switch (label as pino.Level | keyof typeof customLevels) {
case 'trace':
case 'debug':
severity = 'DEBUG';
break;
case 'verbose':
case 'info':
severity = 'INFO';
break;
case 'warn':
severity = 'WARNING';
break;
case 'error':
severity = 'ERROR';
break;
case 'fatal':
severity = 'CRITICAL';
break;
default:
severity = 'DEFAULT';
break;
}

return { severity, level };
};

const pinoOpts: pino.LoggerOptions<keyof typeof customLevels> = {
customLevels,
useOnlyCustomLevels: false,
level: logLevel,
formatters: {
level: levelToSeverityFormatter,
},
};

export const levels = {
labels: { ...pino.levels.labels, ...Object.fromEntries(Object.entries(customLevels).map(e => e.reverse())) },
Expand Down

0 comments on commit 1f1224b

Please sign in to comment.