-
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.
Start consuming np logging config (#56480)
* pass config to the new platform * add default appenders * remove receiveAllAppenders flag it breaks legacy-appender compatibility with legacy flags: silent, quiet, verbose * add integration tests * use console mocks to simplify test setup * update tests * improve names * validate that default appender always presents on root level required during migration period to make sure logs are sent to the LP logging system * do not check condition in the loop * fix integration tests
- Loading branch information
Showing
12 changed files
with
495 additions
and
106 deletions.
There are no files selected for viewing
2 changes: 2 additions & 0 deletions
2
src/core/server/legacy/config/__snapshots__/legacy_object_to_config_adapter.test.ts.snap
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
239 changes: 239 additions & 0 deletions
239
src/core/server/legacy/integration_tests/logging.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,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", | ||
"[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 | ||
", | ||
] | ||
`); | ||
}); | ||
}); | ||
}); | ||
}); |
2 changes: 2 additions & 0 deletions
2
src/core/server/logging/__snapshots__/logging_config.test.ts.snap
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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.