From 963be5f3dcf344dd235c485d403a3562b30a7729 Mon Sep 17 00:00:00 2001 From: Kamil Wylegala Date: Thu, 11 Jul 2024 07:51:45 +0200 Subject: [PATCH] AP-4411 Allow to provide redact option to loggers (#141) * AP-4411 Test for redacted config. * AP-4411 cleanup * AP-4411 Code coverage. --- README.md | 3 +++ package.json | 1 + src/logging/loggerConfigResolver.spec.ts | 22 ++++++++++++++++++++++ src/logging/loggerConfigResolver.ts | 7 +++++-- 4 files changed, 31 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 068528f..c109192 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/package.json b/package.json index fbd97fa..9d530bc 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/logging/loggerConfigResolver.spec.ts b/src/logging/loggerConfigResolver.spec.ts index 0e51b6a..44bc44d 100644 --- a/src/logging/loggerConfigResolver.spec.ts +++ b/src/logging/loggerConfigResolver.spec.ts @@ -1,4 +1,5 @@ import { pino } from 'pino' +import pinoTest from 'pino-test' import { expect } from 'vitest' import { resolveLoggerConfiguration } from './loggerConfigResolver' @@ -17,6 +18,7 @@ describe('loggerConfigResolver', () => { "level": [Function], }, "level": "warn", + "redact": undefined, } `) }) @@ -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.') + })) }) }) diff --git a/src/logging/loggerConfigResolver.ts b/src/logging/loggerConfigResolver.ts index d6613ba..e304104 100644 --- a/src/logging/loggerConfigResolver.ts +++ b/src/logging/loggerConfigResolver.ts @@ -1,4 +1,4 @@ -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' @@ -6,6 +6,7 @@ export type AppLoggerConfig = { logLevel: 'fatal' | 'error' | 'warn' | 'info' | 'debug' | 'trace' | 'silent' nodeEnv: 'production' | 'development' | 'test' base?: Record + redact?: redactOptions } export type MonorepoAppLoggerConfig = AppLoggerConfig & { @@ -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 { @@ -29,6 +30,7 @@ export function resolveMonorepoLoggerConfiguration( return { level: label } }, }, + redact: appConfig.redact, transport: { target: 'pino/file', options: { @@ -63,5 +65,6 @@ export function resolveLoggerConfiguration( return { level } }, }, + redact: appConfig.redact, } satisfies LoggerOptions }