fix(rootLogger): Renamed to setRootLoggerRedactionList and do not expose publically

Signed-off-by: Harry Hogg <hhogg@spotify.com>
This commit is contained in:
Harry Hogg
2021-10-14 13:33:46 +01:00
parent a9025f70ba
commit 488e6145cb
5 changed files with 18 additions and 18 deletions
-3
View File
@@ -539,9 +539,6 @@ export type ServiceBuilder = {
start(): Promise<Server>;
};
// @public (undocumented)
export function setRedactionList(redactionList: string[]): void;
// @public (undocumented)
export function setRootLogger(newLogger: winston.Logger): void;
+6 -5
View File
@@ -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;
}
+1 -1
View File
@@ -15,5 +15,5 @@
*/
export * from './formats';
export * from './rootLogger';
export { createRootLogger, getRootLogger, setRootLogger } from './rootLogger';
export * from './voidLogger';
@@ -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();
@@ -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',
);
}
/**