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 logging #339

Merged
merged 1 commit into from
Dec 3, 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
70 changes: 35 additions & 35 deletions src/util/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { randomUUID } from 'crypto'
import { FastifyReply, FastifyRequest, HookHandlerDoneFunction } from 'fastify'
import { AsyncLocalStorage } from 'node:async_hooks'
import pino from 'pino'
import pretty from 'pino-pretty'
import { BaseSettingsDefinition } from '../config'
import { EmptyInputParameters } from '../validation/input-params'
import CensorList, { CensorKeyValue } from './censor/censor-list'
Expand All @@ -13,48 +14,47 @@ export type Store = {
correlationId: string
}

const debugTransport = {
target: 'pino-pretty',
options: {
levelFirst: true,
levelLabel: 'level',
ignore: 'layer,pid,hostname,correlationId,color',
messageFormat: `\x1b[0m[{correlationId}] {color}[{layer}]\x1b[0m {msg}`,
translateTime: 'yyyy-mm-dd HH:MM:ss.l',
},
}
const stream = pretty({
levelFirst: true,
levelLabel: 'level',
ignore: 'layer,pid,hostname,correlationId,color',
messageFormat: `\x1b[0m[{correlationId}] {color}[{layer}]\x1b[0m {msg}`,
translateTime: 'yyyy-mm-dd HH:MM:ss.l',
})

// Base logger, shouldn't be used because we want layers to be specified
const baseLogger = pino({
level: process.env['LOG_LEVEL']?.toLowerCase() || BaseSettingsDefinition.LOG_LEVEL.default,
formatters: {
level(label) {
return { level: label }
const baseLogger = pino(
{
level: process.env['LOG_LEVEL']?.toLowerCase() || BaseSettingsDefinition.LOG_LEVEL.default,
formatters: {
level(label) {
return { level: label }
},
},
},
hooks: {
logMethod(inputArgs, method) {
// Censor each argument of logger
const censorList = CensorList.getAll()
return method.apply(
this,
inputArgs.map((arg) => censor(arg, censorList)) as [string, ...unknown[]],
)
hooks: {
logMethod(inputArgs, method) {
// Censor each argument of logger
const censorList = CensorList.getAll()
return method.apply(
this,
inputArgs.map((arg) => censor(arg, censorList)) as [string, ...unknown[]],
)
},
},
},
mixin() {
if (process.env['CORRELATION_ID_ENABLED'] !== 'false') {
const store = asyncLocalStorage.getStore() as Store
if (store) {
return {
correlationId: store.correlationId,
mixin() {
if (process.env['CORRELATION_ID_ENABLED'] !== 'false') {
const store = asyncLocalStorage.getStore() as Store
if (store) {
return {
correlationId: store.correlationId,
}
}
}
}
return {}
return {}
},
},
transport: process.env['DEBUG'] === 'true' ? debugTransport : undefined,
})
process.env['DEBUG'] === 'true' ? stream : undefined,
Copy link
Contributor

Choose a reason for hiding this comment

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

you don't need to set transport here?

)

export const COLORS = [
'\u001b[31;1m',
Expand Down
Loading