Skip to content

Commit

Permalink
AP-4411 Allow to provide redact option to loggers (#141)
Browse files Browse the repository at this point in the history
* AP-4411 Test for redacted config.

* AP-4411 cleanup

* AP-4411 Code coverage.
  • Loading branch information
kamilwylegala authored Jul 11, 2024
1 parent 57cf2b4 commit 963be5f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ The library provides methods to resolve the default logging configuration. Publi
const loggerConfig = resolveLoggerConfiguration({
logLevel: 'warn',
nodeEnv: 'production',
redact: {
paths: ['path1', 'path2'],
},
})

const logger = pino(loggerConfig)
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"eslint": "^8.57.0",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-vitest": "0.4.1",
"pino-test": "^1.0.1",
"prettier": "^3.3.1",
"tmp": "^0.2.3",
"typescript": "^5.4.5",
Expand Down
22 changes: 22 additions & 0 deletions src/logging/loggerConfigResolver.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { pino } from 'pino'
import pinoTest from 'pino-test'
import { expect } from 'vitest'

import { resolveLoggerConfiguration } from './loggerConfigResolver'
Expand All @@ -17,6 +18,7 @@ describe('loggerConfigResolver', () => {
"level": [Function],
},
"level": "warn",
"redact": undefined,
}
`)
})
Expand Down Expand Up @@ -61,5 +63,25 @@ describe('loggerConfigResolver', () => {
logger.warn('test')
}).not.toThrow()
})

it('redacts logs via provided config', () =>
new Promise((done) => {
const loggerConfig = resolveLoggerConfiguration({
logLevel: 'info',
nodeEnv: 'production',
redact: {
paths: ['password'],
},
})

const stream = pinoTest.sink()
stream.on('data', (obj) => {
expect(obj.password).toBe('[Redacted]')
done(true)
})

const logger = pino(loggerConfig, stream)
logger.info({ password: 'super password' }, 'Auth attempt.')
}))
})
})
7 changes: 5 additions & 2 deletions src/logging/loggerConfigResolver.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import type { Level, Logger, LoggerOptions } from 'pino'
import type { Level, Logger, LoggerOptions, redactOptions } from 'pino'
import { levels, pino } from 'pino'
import pretty from 'pino-pretty'

export type AppLoggerConfig = {
logLevel: 'fatal' | 'error' | 'warn' | 'info' | 'debug' | 'trace' | 'silent'
nodeEnv: 'production' | 'development' | 'test'
base?: Record<string, unknown>
redact?: redactOptions
}

export type MonorepoAppLoggerConfig = AppLoggerConfig & {
Expand All @@ -14,7 +15,7 @@ export type MonorepoAppLoggerConfig = AppLoggerConfig & {
}

// Note that transports do not work in vitest, likely because pino attempts to run them in a separate worker
/* c8 ignore next 24 */
/* c8 ignore next 25 */
export function resolveMonorepoLoggerConfiguration(
appConfig: MonorepoAppLoggerConfig,
): LoggerOptions | Logger | boolean {
Expand All @@ -29,6 +30,7 @@ export function resolveMonorepoLoggerConfiguration(
return { level: label }
},
},
redact: appConfig.redact,
transport: {
target: 'pino/file',
options: {
Expand Down Expand Up @@ -63,5 +65,6 @@ export function resolveLoggerConfiguration(
return { level }
},
},
redact: appConfig.redact,
} satisfies LoggerOptions
}

0 comments on commit 963be5f

Please sign in to comment.