Skip to content

Commit 7ed1fe0

Browse files
authored
Move logger configuration integration test to jest (#70378)
1 parent 1d1051b commit 7ed1fe0

File tree

8 files changed

+161
-309
lines changed

8 files changed

+161
-309
lines changed

src/core/server/logging/integration_tests/logging.test.ts

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
*/
1919

2020
import * as kbnTestServer from '../../../../test_utils/kbn_server';
21+
import { InternalCoreSetup } from '../../internal_types';
22+
import { LoggerContextConfigInput } from '../logging_config';
23+
import { Subject } from 'rxjs';
2124

2225
function createRoot() {
2326
return kbnTestServer.createRoot({
@@ -111,4 +114,162 @@ describe('logging service', () => {
111114
expect(mockConsoleLog).toHaveBeenCalledTimes(0);
112115
});
113116
});
117+
118+
describe('custom context configuration', () => {
119+
const CUSTOM_LOGGING_CONFIG: LoggerContextConfigInput = {
120+
appenders: {
121+
customJsonConsole: {
122+
kind: 'console',
123+
layout: {
124+
kind: 'json',
125+
},
126+
},
127+
customPatternConsole: {
128+
kind: 'console',
129+
layout: {
130+
kind: 'pattern',
131+
pattern: 'CUSTOM - PATTERN [%logger][%level] %message',
132+
},
133+
},
134+
},
135+
136+
loggers: [
137+
{ context: 'debug_json', appenders: ['customJsonConsole'], level: 'debug' },
138+
{ context: 'debug_pattern', appenders: ['customPatternConsole'], level: 'debug' },
139+
{ context: 'info_json', appenders: ['customJsonConsole'], level: 'info' },
140+
{ context: 'info_pattern', appenders: ['customPatternConsole'], level: 'info' },
141+
{
142+
context: 'all',
143+
appenders: ['customJsonConsole', 'customPatternConsole'],
144+
level: 'debug',
145+
},
146+
],
147+
};
148+
149+
let root: ReturnType<typeof createRoot>;
150+
let setup: InternalCoreSetup;
151+
let mockConsoleLog: jest.SpyInstance;
152+
const loggingConfig$ = new Subject<LoggerContextConfigInput>();
153+
const setContextConfig = (enable: boolean) =>
154+
enable ? loggingConfig$.next(CUSTOM_LOGGING_CONFIG) : loggingConfig$.next({});
155+
beforeAll(async () => {
156+
mockConsoleLog = jest.spyOn(global.console, 'log');
157+
root = kbnTestServer.createRoot();
158+
159+
setup = await root.setup();
160+
setup.logging.configure(['plugins', 'myplugin'], loggingConfig$);
161+
}, 30000);
162+
163+
beforeEach(() => {
164+
mockConsoleLog.mockClear();
165+
});
166+
167+
afterAll(async () => {
168+
mockConsoleLog.mockRestore();
169+
await root.shutdown();
170+
});
171+
172+
it('does not write to custom appenders when not configured', async () => {
173+
const logger = root.logger.get('plugins.myplugin.debug_pattern');
174+
setContextConfig(false);
175+
logger.info('log1');
176+
setContextConfig(true);
177+
logger.debug('log2');
178+
logger.info('log3');
179+
setContextConfig(false);
180+
logger.info('log4');
181+
expect(mockConsoleLog).toHaveBeenCalledTimes(2);
182+
expect(mockConsoleLog).toHaveBeenCalledWith(
183+
'CUSTOM - PATTERN [plugins.myplugin.debug_pattern][DEBUG] log2'
184+
);
185+
expect(mockConsoleLog).toHaveBeenCalledWith(
186+
'CUSTOM - PATTERN [plugins.myplugin.debug_pattern][INFO ] log3'
187+
);
188+
});
189+
190+
it('writes debug_json context to custom JSON appender', async () => {
191+
setContextConfig(true);
192+
const logger = root.logger.get('plugins.myplugin.debug_json');
193+
logger.debug('log1');
194+
logger.info('log2');
195+
expect(mockConsoleLog).toHaveBeenCalledTimes(2);
196+
197+
const [firstCall, secondCall] = mockConsoleLog.mock.calls.map(([jsonString]) =>
198+
JSON.parse(jsonString)
199+
);
200+
expect(firstCall).toMatchObject({
201+
level: 'DEBUG',
202+
context: 'plugins.myplugin.debug_json',
203+
message: 'log1',
204+
});
205+
expect(secondCall).toMatchObject({
206+
level: 'INFO',
207+
context: 'plugins.myplugin.debug_json',
208+
message: 'log2',
209+
});
210+
});
211+
212+
it('writes info_json context to custom JSON appender', async () => {
213+
setContextConfig(true);
214+
const logger = root.logger.get('plugins.myplugin.info_json');
215+
logger.debug('i should not be logged!');
216+
logger.info('log2');
217+
218+
expect(mockConsoleLog).toHaveBeenCalledTimes(1);
219+
expect(JSON.parse(mockConsoleLog.mock.calls[0][0])).toMatchObject({
220+
level: 'INFO',
221+
context: 'plugins.myplugin.info_json',
222+
message: 'log2',
223+
});
224+
});
225+
226+
it('writes debug_pattern context to custom pattern appender', async () => {
227+
setContextConfig(true);
228+
const logger = root.logger.get('plugins.myplugin.debug_pattern');
229+
logger.debug('log1');
230+
logger.info('log2');
231+
232+
expect(mockConsoleLog).toHaveBeenCalledTimes(2);
233+
expect(mockConsoleLog).toHaveBeenCalledWith(
234+
'CUSTOM - PATTERN [plugins.myplugin.debug_pattern][DEBUG] log1'
235+
);
236+
expect(mockConsoleLog).toHaveBeenCalledWith(
237+
'CUSTOM - PATTERN [plugins.myplugin.debug_pattern][INFO ] log2'
238+
);
239+
});
240+
241+
it('writes info_pattern context to custom pattern appender', async () => {
242+
setContextConfig(true);
243+
const logger = root.logger.get('plugins.myplugin.info_pattern');
244+
logger.debug('i should not be logged!');
245+
logger.info('log2');
246+
expect(mockConsoleLog).toHaveBeenCalledTimes(1);
247+
expect(mockConsoleLog).toHaveBeenCalledWith(
248+
'CUSTOM - PATTERN [plugins.myplugin.info_pattern][INFO ] log2'
249+
);
250+
});
251+
252+
it('writes all context to both appenders', async () => {
253+
setContextConfig(true);
254+
const logger = root.logger.get('plugins.myplugin.all');
255+
logger.debug('log1');
256+
logger.info('log2');
257+
258+
expect(mockConsoleLog).toHaveBeenCalledTimes(4);
259+
const logs = mockConsoleLog.mock.calls.map(([jsonString]) => jsonString);
260+
261+
expect(JSON.parse(logs[0])).toMatchObject({
262+
level: 'DEBUG',
263+
context: 'plugins.myplugin.all',
264+
message: 'log1',
265+
});
266+
expect(logs[1]).toEqual('CUSTOM - PATTERN [plugins.myplugin.all][DEBUG] log1');
267+
expect(JSON.parse(logs[2])).toMatchObject({
268+
level: 'INFO',
269+
context: 'plugins.myplugin.all',
270+
message: 'log2',
271+
});
272+
expect(logs[3]).toEqual('CUSTOM - PATTERN [plugins.myplugin.all][INFO ] log2');
273+
});
274+
});
114275
});

test/plugin_functional/plugins/core_logging/kibana.json

Lines changed: 0 additions & 7 deletions
This file was deleted.

test/plugin_functional/plugins/core_logging/server/.gitignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/plugin_functional/plugins/core_logging/server/index.ts

Lines changed: 0 additions & 23 deletions
This file was deleted.

test/plugin_functional/plugins/core_logging/server/plugin.ts

Lines changed: 0 additions & 118 deletions
This file was deleted.

test/plugin_functional/plugins/core_logging/tsconfig.json

Lines changed: 0 additions & 13 deletions
This file was deleted.

test/plugin_functional/test_suites/core_plugins/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,5 @@ export default function ({ loadTestFile }: PluginFunctionalProviderContext) {
3030
loadTestFile(require.resolve('./application_leave_confirm'));
3131
loadTestFile(require.resolve('./application_status'));
3232
loadTestFile(require.resolve('./rendering'));
33-
loadTestFile(require.resolve('./logging'));
3433
});
3534
}

0 commit comments

Comments
 (0)