-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make task manager code use the same logger (#192574)
In this PR, I'm making the sub-loggers within task manager use the main logger so we can observe the logs under `log.logger:"plugin.taskManager"`. To preserve separation, I moved the sub-logger name within a tag so we can still filter the logs via `tags:"taskClaimer"`. The wrapped_logger.ts file is copied from `x-pack/plugins/alerting/server/task_runner/lib/task_runner_logger.ts`. --------- Co-authored-by: Elastic Machine <[email protected]>
- Loading branch information
1 parent
61fcb0f
commit a0973d6
Showing
10 changed files
with
205 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
x-pack/plugins/task_manager/server/lib/wrapped_logger.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { loggingSystemMock } from '@kbn/core/server/mocks'; | ||
import { LogLevel, LogRecord } from '@kbn/logging'; | ||
import { createWrappedLogger } from './wrapped_logger'; | ||
|
||
describe('createWrappedLogger', () => { | ||
test('should inject baseline tags into log messages', () => { | ||
const logger: ReturnType<typeof loggingSystemMock.createLogger> = | ||
loggingSystemMock.createLogger(); | ||
const taskRunnerLogger = createWrappedLogger({ logger, tags: ['tag-1', 'tag-2'] }); | ||
|
||
taskRunnerLogger.trace('test trace message', { tags: ['tag-3'] }); | ||
taskRunnerLogger.debug('test debug message', { tags: ['tag-4'] }); | ||
taskRunnerLogger.info('test info message', { tags: ['tag-5'] }); | ||
taskRunnerLogger.warn('test warn message', { tags: ['tag-6'] }); | ||
taskRunnerLogger.error('test error message', { tags: ['tag-7'] }); | ||
taskRunnerLogger.fatal('test fatal message', { tags: ['tag-8'] }); | ||
|
||
expect(logger.trace).toHaveBeenCalledWith('test trace message', { | ||
tags: ['tag-1', 'tag-2', 'tag-3'], | ||
}); | ||
expect(logger.debug).toHaveBeenCalledWith('test debug message', { | ||
tags: ['tag-1', 'tag-2', 'tag-4'], | ||
}); | ||
expect(logger.info).toHaveBeenCalledWith('test info message', { | ||
tags: ['tag-1', 'tag-2', 'tag-5'], | ||
}); | ||
expect(logger.warn).toHaveBeenCalledWith('test warn message', { | ||
tags: ['tag-1', 'tag-2', 'tag-6'], | ||
}); | ||
expect(logger.error).toHaveBeenCalledWith('test error message', { | ||
tags: ['tag-1', 'tag-2', 'tag-7'], | ||
}); | ||
expect(logger.fatal).toHaveBeenCalledWith('test fatal message', { | ||
tags: ['tag-1', 'tag-2', 'tag-8'], | ||
}); | ||
}); | ||
|
||
test('should pass through other meta fields', () => { | ||
const logger: ReturnType<typeof loggingSystemMock.createLogger> = | ||
loggingSystemMock.createLogger(); | ||
const taskRunnerLogger = createWrappedLogger({ logger, tags: ['tag-1', 'tag-2'] }); | ||
|
||
taskRunnerLogger.trace('test trace message', { labels: { foo: 'bar' } }); | ||
taskRunnerLogger.debug('test debug message', { tags: ['tag-4'], host: { cpu: { usage: 3 } } }); | ||
taskRunnerLogger.info('test info message'); | ||
taskRunnerLogger.warn('test warn message', { user: { email: '[email protected]' } }); | ||
taskRunnerLogger.error('test error message', { agent: { id: 'agent-1' } }); | ||
taskRunnerLogger.fatal('test fatal message'); | ||
|
||
expect(logger.trace).toHaveBeenCalledWith('test trace message', { | ||
tags: ['tag-1', 'tag-2'], | ||
labels: { foo: 'bar' }, | ||
}); | ||
expect(logger.debug).toHaveBeenCalledWith('test debug message', { | ||
tags: ['tag-1', 'tag-2', 'tag-4'], | ||
host: { cpu: { usage: 3 } }, | ||
}); | ||
expect(logger.info).toHaveBeenCalledWith('test info message', { tags: ['tag-1', 'tag-2'] }); | ||
expect(logger.warn).toHaveBeenCalledWith('test warn message', { | ||
tags: ['tag-1', 'tag-2'], | ||
user: { email: '[email protected]' }, | ||
}); | ||
expect(logger.error).toHaveBeenCalledWith('test error message', { | ||
tags: ['tag-1', 'tag-2'], | ||
agent: { id: 'agent-1' }, | ||
}); | ||
expect(logger.fatal).toHaveBeenCalledWith('test fatal message', { tags: ['tag-1', 'tag-2'] }); | ||
}); | ||
|
||
test('should pass through other functions', () => { | ||
const logger: ReturnType<typeof loggingSystemMock.createLogger> = | ||
loggingSystemMock.createLogger(); | ||
const taskRunnerLogger = createWrappedLogger({ logger, tags: ['tag-1', 'tag-2'] }); | ||
|
||
taskRunnerLogger.isLevelEnabled('debug'); | ||
expect(logger.isLevelEnabled).toHaveBeenCalledWith('debug'); | ||
|
||
taskRunnerLogger.get('prefix', 'another'); | ||
expect(logger.get).toHaveBeenCalledWith('prefix', 'another'); | ||
|
||
const logRecord: LogRecord = { | ||
timestamp: new Date(), | ||
level: LogLevel.Info, | ||
context: 'context', | ||
message: 'message', | ||
pid: 1, | ||
}; | ||
taskRunnerLogger.log(logRecord); | ||
expect(logger.log).toHaveBeenCalledWith(logRecord); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import type { Logger, LogMeta } from '@kbn/core/server'; | ||
import { LogLevelId, LogMessageSource, LogRecord } from '@kbn/logging'; | ||
|
||
interface WrappedLoggerOpts { | ||
logger: Logger; | ||
tags: string[]; | ||
} | ||
|
||
export function createWrappedLogger(opts: WrappedLoggerOpts): Logger { | ||
return new WrappedLogger(opts); | ||
} | ||
|
||
class WrappedLogger implements Logger { | ||
private loggerMetaTags: string[] = []; | ||
|
||
constructor(private readonly opts: WrappedLoggerOpts) { | ||
this.loggerMetaTags = opts.tags; | ||
} | ||
|
||
trace<Meta extends LogMeta = LogMeta>(message: LogMessageSource, meta?: Meta) { | ||
this.opts.logger.trace(message, { ...meta, tags: this.combineTags(meta?.tags) }); | ||
} | ||
|
||
debug<Meta extends LogMeta = LogMeta>(message: LogMessageSource, meta?: Meta) { | ||
this.opts.logger.debug(message, { ...meta, tags: this.combineTags(meta?.tags) }); | ||
} | ||
|
||
info<Meta extends LogMeta = LogMeta>(message: LogMessageSource, meta?: Meta) { | ||
this.opts.logger.info(message, { ...meta, tags: this.combineTags(meta?.tags) }); | ||
} | ||
|
||
warn<Meta extends LogMeta = LogMeta>(errorOrMessage: LogMessageSource | Error, meta?: Meta) { | ||
this.opts.logger.warn(errorOrMessage, { ...meta, tags: this.combineTags(meta?.tags) }); | ||
} | ||
|
||
error<Meta extends LogMeta = LogMeta>(errorOrMessage: LogMessageSource | Error, meta?: Meta) { | ||
this.opts.logger.error(errorOrMessage, { ...meta, tags: this.combineTags(meta?.tags) }); | ||
} | ||
|
||
fatal<Meta extends LogMeta = LogMeta>(errorOrMessage: LogMessageSource | Error, meta?: Meta) { | ||
this.opts.logger.fatal(errorOrMessage, { ...meta, tags: this.combineTags(meta?.tags) }); | ||
} | ||
|
||
log(record: LogRecord) { | ||
this.opts.logger.log(record); | ||
} | ||
|
||
isLevelEnabled(level: LogLevelId): boolean { | ||
return this.opts.logger.isLevelEnabled(level); | ||
} | ||
|
||
get(...childContextPaths: string[]): Logger { | ||
return this.opts.logger.get(...childContextPaths); | ||
} | ||
|
||
private combineTags(tags?: string[] | string): string[] { | ||
if (!tags) { | ||
return this.loggerMetaTags; | ||
} | ||
|
||
if (typeof tags === 'string') { | ||
return [...new Set([...this.loggerMetaTags, tags])]; | ||
} | ||
|
||
return [...new Set([...this.loggerMetaTags, ...tags])]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.