Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add log severity #10835

Merged
merged 2 commits into from
Dec 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
},
Comment on lines +108 to +110
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to apply this formatter to the stdio transport only? We don't need to make this change in the OTEL transport.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the pino docs, no. But I'm not sure if that means we can't customize the log level at all or if we can't customize it per transport.

https://github.com/pinojs/pino/blob/main/docs/api.md#level

};

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