Merge pull request #15657 from backstage/rugvip/loglabel
backend-common: make it possible to override service log label
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
---
|
||||
'@backstage/backend-common': patch
|
||||
---
|
||||
|
||||
Updated the logger created by `createRootLogger` to make it possible to override the default `service` log label.
|
||||
@@ -36,12 +36,13 @@ describe('rootLogger', () => {
|
||||
});
|
||||
|
||||
it('redacts given secrets', () => {
|
||||
const logger = createRootLogger();
|
||||
jest.spyOn(logger, 'write');
|
||||
const transport = new winston.transports.Console();
|
||||
const logger = createRootLogger({ transports: [transport] });
|
||||
jest.spyOn(transport, 'write');
|
||||
setRootLoggerRedactionList(['SECRET-1', 'SECRET_2', 'SECRET.3']);
|
||||
logger.info('Logging SECRET-1 and SECRET_2 and SECRET.3');
|
||||
|
||||
expect(logger.write).toHaveBeenCalledWith(
|
||||
expect(transport.write).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
message: 'Logging [REDACTED] and [REDACTED] and [REDACTED]',
|
||||
}),
|
||||
@@ -49,12 +50,13 @@ describe('rootLogger', () => {
|
||||
});
|
||||
|
||||
it('redacts but ignores empty and one-character secrets', () => {
|
||||
const logger = createRootLogger();
|
||||
jest.spyOn(logger, 'write');
|
||||
const transport = new winston.transports.Console();
|
||||
const logger = createRootLogger({ transports: [transport] });
|
||||
jest.spyOn(transport, 'write');
|
||||
setRootLoggerRedactionList(['SECRET-1', 'SECRET_2', 'Q', '']);
|
||||
logger.info('Logging SECRET-1 and SECRET_2 and Q');
|
||||
|
||||
expect(logger.write).toHaveBeenCalledWith(
|
||||
expect(transport.write).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
message: 'Logging [REDACTED] and [REDACTED] and Q',
|
||||
}),
|
||||
@@ -125,5 +127,38 @@ describe('rootLogger', () => {
|
||||
logger.format.transform({ message: 'hello', level: 'info' }),
|
||||
).toBeFalsy();
|
||||
});
|
||||
|
||||
it('can override the service label', () => {
|
||||
const transport = new winston.transports.Console();
|
||||
const logger = createRootLogger({ transports: [transport] });
|
||||
const writeSpy = jest
|
||||
.spyOn(transport, 'write')
|
||||
.mockImplementation((_c, _e) => true);
|
||||
|
||||
logger.info('msg-a');
|
||||
logger.child({ service: 'b' }).info('msg-b');
|
||||
logger.info('msg-c', { service: 'c' });
|
||||
|
||||
expect(writeSpy.mock.calls).toEqual([
|
||||
[
|
||||
expect.objectContaining({
|
||||
message: 'msg-a',
|
||||
service: 'backstage',
|
||||
}),
|
||||
],
|
||||
[
|
||||
expect.objectContaining({
|
||||
message: 'msg-b',
|
||||
service: 'b',
|
||||
}),
|
||||
],
|
||||
[
|
||||
expect.objectContaining({
|
||||
message: 'msg-c',
|
||||
service: 'c',
|
||||
}),
|
||||
],
|
||||
]);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -101,26 +101,27 @@ export function createRootLogger(
|
||||
options: winston.LoggerOptions = {},
|
||||
env = process.env,
|
||||
): winston.Logger {
|
||||
const logger = winston.createLogger(
|
||||
merge<LoggerOptions, LoggerOptions>(
|
||||
{
|
||||
level: env.LOG_LEVEL || 'info',
|
||||
format: winston.format.combine(
|
||||
winston.format(redactWinstonLogLine)(),
|
||||
env.NODE_ENV === 'production' ? winston.format.json() : coloredFormat,
|
||||
),
|
||||
defaultMeta: {
|
||||
service: 'backstage',
|
||||
const logger = winston
|
||||
.createLogger(
|
||||
merge<LoggerOptions, LoggerOptions>(
|
||||
{
|
||||
level: env.LOG_LEVEL || 'info',
|
||||
format: winston.format.combine(
|
||||
winston.format(redactWinstonLogLine)(),
|
||||
env.NODE_ENV === 'production'
|
||||
? winston.format.json()
|
||||
: coloredFormat,
|
||||
),
|
||||
transports: [
|
||||
new winston.transports.Console({
|
||||
silent: env.JEST_WORKER_ID !== undefined && !env.LOG_LEVEL,
|
||||
}),
|
||||
],
|
||||
},
|
||||
transports: [
|
||||
new winston.transports.Console({
|
||||
silent: env.JEST_WORKER_ID !== undefined && !env.LOG_LEVEL,
|
||||
}),
|
||||
],
|
||||
},
|
||||
options,
|
||||
),
|
||||
);
|
||||
options,
|
||||
),
|
||||
)
|
||||
.child({ service: 'backstage' });
|
||||
|
||||
setRootLogger(logger);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user