fix config update for root context (#70168)

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
This commit is contained in:
Mikhail Shustov 2020-06-29 19:13:00 +03:00 committed by GitHub
parent 851e7ff9b8
commit 4fe60c4a0d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 1 deletions

View file

@ -230,6 +230,49 @@ test('setContextConfig() updates config with relative contexts', () => {
);
});
test('setContextConfig() updates config for a root context', () => {
const testsLogger = system.get('tests');
const testsChildLogger = system.get('tests', 'child');
const testsGrandchildLogger = system.get('tests', 'child', 'grandchild');
system.upgrade(
config.schema.validate({
appenders: { default: { kind: 'console', layout: { kind: 'json' } } },
root: { level: 'info' },
})
);
system.setContextConfig(['tests', 'child'], {
appenders: new Map([
[
'custom',
{ kind: 'console', layout: { kind: 'pattern', pattern: '[%level][%logger] %message' } },
],
]),
loggers: [{ context: '', appenders: ['custom'], level: 'debug' }],
});
testsLogger.warn('tests log to default!');
testsChildLogger.error('tests.child log to custom!');
testsGrandchildLogger.debug('tests.child.grandchild log to custom!');
expect(mockConsoleLog).toHaveBeenCalledTimes(3);
// Parent context is unaffected
expect(JSON.parse(mockConsoleLog.mock.calls[0][0])).toMatchObject({
context: 'tests',
message: 'tests log to default!',
level: 'WARN',
});
// Customized contexts
expect(mockConsoleLog.mock.calls[1][0]).toMatchInlineSnapshot(
`"[ERROR][tests.child] tests.child log to custom!"`
);
expect(mockConsoleLog.mock.calls[2][0]).toMatchInlineSnapshot(
`"[DEBUG][tests.child.grandchild] tests.child.grandchild log to custom!"`
);
});
test('custom context configs are applied on subsequent calls to update()', () => {
system.setContextConfig(['tests', 'child'], {
appenders: new Map([

View file

@ -100,7 +100,9 @@ export class LoggingSystem implements LoggerFactory {
// Automatically prepend the base context to the logger sub-contexts
loggers: contextConfig.loggers.map((l) => ({
...l,
context: LoggingConfig.getLoggerContext([context, l.context]),
context: LoggingConfig.getLoggerContext(
l.context.length > 0 ? [context, l.context] : [context]
),
})),
});