From 488e6145cbe04ac0a7ec8708b26615a0fe9a6ff0 Mon Sep 17 00:00:00 2001 From: Harry Hogg Date: Thu, 14 Oct 2021 13:33:46 +0100 Subject: [PATCH] fix(rootLogger): Renamed to setRootLoggerRedactionList and do not expose publically Signed-off-by: Harry Hogg --- packages/backend-common/api-report.md | 3 --- packages/backend-common/src/config.ts | 11 ++++++----- packages/backend-common/src/logging/index.ts | 2 +- .../backend-common/src/logging/rootLogger.test.ts | 10 +++++----- packages/backend-common/src/logging/rootLogger.ts | 10 ++++++---- 5 files changed, 18 insertions(+), 18 deletions(-) diff --git a/packages/backend-common/api-report.md b/packages/backend-common/api-report.md index 92271f8521..2336a57ec3 100644 --- a/packages/backend-common/api-report.md +++ b/packages/backend-common/api-report.md @@ -539,9 +539,6 @@ export type ServiceBuilder = { start(): Promise; }; -// @public (undocumented) -export function setRedactionList(redactionList: string[]): void; - // @public (undocumented) export function setRootLogger(newLogger: winston.Logger): void; diff --git a/packages/backend-common/src/config.ts b/packages/backend-common/src/config.ts index 0e6f993d8f..7a79d1df38 100644 --- a/packages/backend-common/src/config.ts +++ b/packages/backend-common/src/config.ts @@ -23,12 +23,13 @@ import { loadConfig, ConfigSchema, } from '@backstage/config-loader'; -import { AppConfig, Config, ConfigReader, JsonValue } from '@backstage/config'; +import { AppConfig, Config, ConfigReader } from '@backstage/config'; +import { JsonValue } from '@backstage/types'; -import { setRedactionList } from './logging'; +import { setRootLoggerRedactionList } from './logging/rootLogger'; // Fetch the schema and get all the secrets to pass to the rootLogger for redaction -const updateRedactionMap = ( +const updateRedactionList = ( schema: ConfigSchema, configs: AppConfig[], logger: Logger, @@ -219,8 +220,8 @@ export async function loadBackendConfig(options: { config.setConfig(ConfigReader.fromConfigs(configs)); // Subscribe to config changes and update the redaction list for logging - updateRedactionMap(schema, configs, options.logger); - config.subscribe(() => updateRedactionMap(schema, configs, options.logger)); + updateRedactionList(schema, configs, options.logger); + config.subscribe(() => updateRedactionList(schema, configs, options.logger)); return config; } diff --git a/packages/backend-common/src/logging/index.ts b/packages/backend-common/src/logging/index.ts index 71e9618f0c..f50114a9ae 100644 --- a/packages/backend-common/src/logging/index.ts +++ b/packages/backend-common/src/logging/index.ts @@ -15,5 +15,5 @@ */ export * from './formats'; -export * from './rootLogger'; +export { createRootLogger, getRootLogger, setRootLogger } from './rootLogger'; export * from './voidLogger'; diff --git a/packages/backend-common/src/logging/rootLogger.test.ts b/packages/backend-common/src/logging/rootLogger.test.ts index f24a757155..192decb653 100644 --- a/packages/backend-common/src/logging/rootLogger.test.ts +++ b/packages/backend-common/src/logging/rootLogger.test.ts @@ -19,7 +19,7 @@ import { createRootLogger, getRootLogger, setRootLogger, - setRedactionList, + setRootLoggerRedactionList, } from './rootLogger'; describe('rootLogger', () => { @@ -38,17 +38,17 @@ describe('rootLogger', () => { it('redacts given secrets', () => { const logger = createRootLogger(); jest.spyOn(logger, 'write'); - setRedactionList(['SECRET_1', 'SECRET_2']); - logger.info('Logging SECRET_1 and SECRET_2 but not SECRET_3'); + setRootLoggerRedactionList(['SECRET-1', 'SECRET_2', 'SECRET.3']); + logger.info('Logging SECRET-1 and SECRET_2 and SECRET.3'); expect(logger.write).toHaveBeenCalledWith( expect.objectContaining({ - message: 'Logging [REDACTED] and [REDACTED] but not SECRET_3', + message: 'Logging [REDACTED] and [REDACTED] and [REDACTED]', }), ); }); - describe('createRootLoger', () => { + describe('createRootLogger', () => { it('creates a new logger', () => { const oldLogger = getRootLogger(); const newLogger = createRootLogger(); diff --git a/packages/backend-common/src/logging/rootLogger.ts b/packages/backend-common/src/logging/rootLogger.ts index 114f4b4f48..766746fd3c 100644 --- a/packages/backend-common/src/logging/rootLogger.ts +++ b/packages/backend-common/src/logging/rootLogger.ts @@ -18,8 +18,8 @@ import { merge } from 'lodash'; import * as winston from 'winston'; import { LoggerOptions } from 'winston'; import { coloredFormat } from './formats'; +import { escapeRegExp } from '../util/escapeRegExp'; -/** @public */ let rootLogger: winston.Logger; let redactionRegExp: RegExp; @@ -33,9 +33,11 @@ export function setRootLogger(newLogger: winston.Logger) { rootLogger = newLogger; } -/** @public */ -export function setRedactionList(redactionList: string[]) { - redactionRegExp = new RegExp(`(${redactionList.join('|')})`, 'g'); +export function setRootLoggerRedactionList(redactionList: string[]) { + redactionRegExp = new RegExp( + `(${redactionList.map(escapeRegExp).join('|')})`, + 'g', + ); } /**