-
Notifications
You must be signed in to change notification settings - Fork 8.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Start consuming np logging config #56480
Changes from all commits
9374878
a51c3b4
de25412
003cd43
7f51269
e6bbae4
bae5b6f
e533d54
b4b698c
554f26f
8f5f392
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,12 +19,13 @@ | |
|
||
import { ConfigPath } from '../../config'; | ||
import { ObjectToConfigAdapter } from '../../config/object_to_config_adapter'; | ||
import { LoggingConfigType } from '../../logging/logging_config'; | ||
import { LegacyVars } from '../types'; | ||
|
||
/** | ||
* Represents logging config supported by the legacy platform. | ||
*/ | ||
interface LegacyLoggingConfig { | ||
export interface LegacyLoggingConfig { | ||
silent?: boolean; | ||
verbose?: boolean; | ||
quiet?: boolean; | ||
|
@@ -33,18 +34,24 @@ interface LegacyLoggingConfig { | |
events?: Record<string, string>; | ||
} | ||
|
||
type MixedLoggingConfig = LegacyLoggingConfig & Partial<LoggingConfigType>; | ||
|
||
/** | ||
* Represents adapter between config provided by legacy platform and `Config` | ||
* supported by the current platform. | ||
* @internal | ||
*/ | ||
export class LegacyObjectToConfigAdapter extends ObjectToConfigAdapter { | ||
private static transformLogging(configValue: LegacyLoggingConfig = {}) { | ||
private static transformLogging(configValue: MixedLoggingConfig = {}) { | ||
const { appenders, root, loggers, ...legacyLoggingConfig } = configValue; | ||
|
||
const loggingConfig = { | ||
appenders: { | ||
default: { kind: 'legacy-appender', legacyLoggingConfig: configValue }, | ||
...appenders, | ||
default: { kind: 'legacy-appender', legacyLoggingConfig }, | ||
}, | ||
root: { level: 'info' }, | ||
root: { level: 'info', ...root }, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: We allow users to configure roots. Although if a user specifies There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Logging a warning makes sense to me 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added config validation to prevent server start |
||
loggers, | ||
}; | ||
|
||
if (configValue.silent) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,239 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
import * as kbnTestServer from '../../../../test_utils/kbn_server'; | ||
|
||
import { | ||
getPlatformLogsFromMock, | ||
getLegacyPlatformLogsFromMock, | ||
} from '../../logging/integration_tests/utils'; | ||
|
||
import { LegacyLoggingConfig } from '../config/legacy_object_to_config_adapter'; | ||
|
||
function createRoot(legacyLoggingConfig: LegacyLoggingConfig = {}) { | ||
return kbnTestServer.createRoot({ | ||
migrations: { skip: true }, // otherwise stuck in polling ES | ||
logging: { | ||
// legacy platform config | ||
silent: false, | ||
json: false, | ||
...legacyLoggingConfig, | ||
events: { | ||
log: ['test-file-legacy'], | ||
}, | ||
// platform config | ||
appenders: { | ||
'test-console': { | ||
kind: 'console', | ||
layout: { | ||
highlight: false, | ||
kind: 'pattern', | ||
}, | ||
}, | ||
}, | ||
loggers: [ | ||
{ | ||
context: 'test-file', | ||
appenders: ['test-console'], | ||
level: 'info', | ||
}, | ||
], | ||
}, | ||
}); | ||
} | ||
|
||
describe('logging service', () => { | ||
let mockConsoleLog: jest.SpyInstance; | ||
let mockStdout: jest.SpyInstance; | ||
|
||
beforeAll(async () => { | ||
mockConsoleLog = jest.spyOn(global.console, 'log'); | ||
mockStdout = jest.spyOn(global.process.stdout, 'write'); | ||
}); | ||
|
||
afterAll(async () => { | ||
mockConsoleLog.mockRestore(); | ||
mockStdout.mockRestore(); | ||
}); | ||
|
||
describe('compatibility', () => { | ||
describe('uses configured loggers', () => { | ||
let root: ReturnType<typeof createRoot>; | ||
beforeAll(async () => { | ||
root = createRoot(); | ||
|
||
await root.setup(); | ||
await root.start(); | ||
}, 30000); | ||
|
||
afterAll(async () => { | ||
await root.shutdown(); | ||
}); | ||
|
||
beforeEach(() => { | ||
mockConsoleLog.mockClear(); | ||
mockStdout.mockClear(); | ||
}); | ||
|
||
it('when context matches', async () => { | ||
root.logger.get('test-file').info('handled by NP'); | ||
|
||
expect(mockConsoleLog).toHaveBeenCalledTimes(1); | ||
const loggedString = getPlatformLogsFromMock(mockConsoleLog); | ||
expect(loggedString).toMatchInlineSnapshot(` | ||
Array [ | ||
"[xxxx-xx-xxTxx:xx:xx.xxxZ][INFO ][test-file] handled by NP", | ||
] | ||
`); | ||
}); | ||
|
||
it('falls back to the root legacy logger otherwise', async () => { | ||
root.logger.get('test-file-legacy').info('handled by LP'); | ||
|
||
expect(mockStdout).toHaveBeenCalledTimes(1); | ||
|
||
const loggedString = getLegacyPlatformLogsFromMock(mockStdout); | ||
expect(loggedString).toMatchInlineSnapshot(` | ||
Array [ | ||
" log [xx:xx:xx.xxx] [info][test-file-legacy] handled by LP | ||
", | ||
] | ||
`); | ||
}); | ||
}); | ||
|
||
describe('logging config respects legacy logging settings', () => { | ||
let root: ReturnType<typeof createRoot>; | ||
|
||
afterEach(async () => { | ||
mockConsoleLog.mockClear(); | ||
mockStdout.mockClear(); | ||
await root.shutdown(); | ||
}); | ||
|
||
it('"silent": true', async () => { | ||
root = createRoot({ silent: true }); | ||
|
||
await root.setup(); | ||
await root.start(); | ||
|
||
const platformLogger = root.logger.get('test-file'); | ||
platformLogger.info('info'); | ||
platformLogger.warn('warn'); | ||
platformLogger.error('error'); | ||
|
||
expect(mockConsoleLog).toHaveBeenCalledTimes(3); | ||
|
||
expect(getPlatformLogsFromMock(mockConsoleLog)).toMatchInlineSnapshot(` | ||
Array [ | ||
"[xxxx-xx-xxTxx:xx:xx.xxxZ][INFO ][test-file] info", | ||
"[xxxx-xx-xxTxx:xx:xx.xxxZ][WARN ][test-file] warn", | ||
"[xxxx-xx-xxTxx:xx:xx.xxxZ][ERROR][test-file] error", | ||
] | ||
`); | ||
|
||
mockStdout.mockClear(); | ||
|
||
const legacyPlatformLogger = root.logger.get('test-file-legacy'); | ||
legacyPlatformLogger.info('info'); | ||
legacyPlatformLogger.warn('warn'); | ||
legacyPlatformLogger.error('error'); | ||
|
||
expect(mockStdout).toHaveBeenCalledTimes(0); | ||
}); | ||
|
||
it('"quiet": true', async () => { | ||
root = createRoot({ quiet: true }); | ||
|
||
await root.setup(); | ||
await root.start(); | ||
|
||
const platformLogger = root.logger.get('test-file'); | ||
platformLogger.info('info'); | ||
platformLogger.warn('warn'); | ||
platformLogger.error('error'); | ||
|
||
expect(mockConsoleLog).toHaveBeenCalledTimes(3); | ||
|
||
expect(getPlatformLogsFromMock(mockConsoleLog)).toMatchInlineSnapshot(` | ||
Array [ | ||
"[xxxx-xx-xxTxx:xx:xx.xxxZ][INFO ][test-file] info", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can stripe timestamps. I decided to keep it to see the difference in the format until we have separate tests for them. |
||
"[xxxx-xx-xxTxx:xx:xx.xxxZ][WARN ][test-file] warn", | ||
"[xxxx-xx-xxTxx:xx:xx.xxxZ][ERROR][test-file] error", | ||
] | ||
`); | ||
|
||
mockStdout.mockClear(); | ||
|
||
const legacyPlatformLogger = root.logger.get('test-file-legacy'); | ||
legacyPlatformLogger.info('info'); | ||
legacyPlatformLogger.warn('warn'); | ||
legacyPlatformLogger.error('error'); | ||
|
||
expect(mockStdout).toHaveBeenCalledTimes(1); | ||
expect(getLegacyPlatformLogsFromMock(mockStdout)).toMatchInlineSnapshot(` | ||
Array [ | ||
" log [xx:xx:xx.xxx] [error][test-file-legacy] error | ||
", | ||
] | ||
`); | ||
}); | ||
|
||
it('"verbose": true', async () => { | ||
root = createRoot({ verbose: true }); | ||
|
||
await root.setup(); | ||
await root.start(); | ||
|
||
const platformLogger = root.logger.get('test-file'); | ||
platformLogger.info('info'); | ||
platformLogger.warn('warn'); | ||
platformLogger.error('error'); | ||
|
||
expect(mockConsoleLog).toHaveBeenCalledTimes(3); | ||
|
||
expect(getPlatformLogsFromMock(mockConsoleLog)).toMatchInlineSnapshot(` | ||
Array [ | ||
"[xxxx-xx-xxTxx:xx:xx.xxxZ][INFO ][test-file] info", | ||
"[xxxx-xx-xxTxx:xx:xx.xxxZ][WARN ][test-file] warn", | ||
"[xxxx-xx-xxTxx:xx:xx.xxxZ][ERROR][test-file] error", | ||
] | ||
`); | ||
|
||
mockStdout.mockClear(); | ||
|
||
const legacyPlatformLogger = root.logger.get('test-file-legacy'); | ||
legacyPlatformLogger.info('info'); | ||
legacyPlatformLogger.warn('warn'); | ||
legacyPlatformLogger.error('error'); | ||
|
||
expect(mockStdout).toHaveBeenCalledTimes(3); | ||
expect(getLegacyPlatformLogsFromMock(mockStdout)).toMatchInlineSnapshot(` | ||
Array [ | ||
" log [xx:xx:xx.xxx] [info][test-file-legacy] info | ||
", | ||
" log [xx:xx:xx.xxx] [warning][test-file-legacy] warn | ||
", | ||
" log [xx:xx:xx.xxx] [error][test-file-legacy] error | ||
", | ||
] | ||
`); | ||
}); | ||
}); | ||
}); | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rewrites a default appender
kibana/src/core/server/logging/logging_config.ts
Line 155 in e6bbae4