Skip to content

Commit

Permalink
Move logger configuration integration test to jest
Browse files Browse the repository at this point in the history
  • Loading branch information
joshdover committed Jun 30, 2020
1 parent 56aac44 commit 2c86ca5
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 309 deletions.
161 changes: 161 additions & 0 deletions src/core/server/logging/integration_tests/logging.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
*/

import * as kbnTestServer from '../../../../test_utils/kbn_server';
import { InternalCoreSetup } from '../../internal_types';
import { LoggerContextConfigInput } from '../logging_config';
import { Subject } from 'rxjs';

function createRoot() {
return kbnTestServer.createRoot({
Expand Down Expand Up @@ -111,4 +114,162 @@ describe('logging service', () => {
expect(mockConsoleLog).toHaveBeenCalledTimes(0);
});
});

describe('custom context configuration', () => {
const CUSTOM_LOGGING_CONFIG: LoggerContextConfigInput = {
appenders: {
customJsonConsole: {
kind: 'console',
layout: {
kind: 'json',
},
},
customPatternConsole: {
kind: 'console',
layout: {
kind: 'pattern',
pattern: 'CUSTOM - PATTERN [%logger][%level] %message',
},
},
},

loggers: [
{ context: 'debug_json', appenders: ['customJsonConsole'], level: 'debug' },
{ context: 'debug_pattern', appenders: ['customPatternConsole'], level: 'debug' },
{ context: 'info_json', appenders: ['customJsonConsole'], level: 'info' },
{ context: 'info_pattern', appenders: ['customPatternConsole'], level: 'info' },
{
context: 'all',
appenders: ['customJsonConsole', 'customPatternConsole'],
level: 'debug',
},
],
};

let root: ReturnType<typeof createRoot>;
let setup: InternalCoreSetup;
let mockConsoleLog: jest.SpyInstance;
const loggingConfig$ = new Subject<LoggerContextConfigInput>();
const setContextConfig = (enable: boolean) =>
enable ? loggingConfig$.next(CUSTOM_LOGGING_CONFIG) : loggingConfig$.next({});
beforeAll(async () => {
mockConsoleLog = jest.spyOn(global.console, 'log');
root = kbnTestServer.createRoot();

setup = await root.setup();
setup.logging.configure(['plugins', 'myplugin'], loggingConfig$);
}, 30000);

beforeEach(() => {
mockConsoleLog.mockClear();
});

afterAll(async () => {
mockConsoleLog.mockRestore();
await root.shutdown();
});

it('does not write to custom appenders when not configured', async () => {
const logger = root.logger.get('plugins.myplugin.debug_pattern');
setContextConfig(false);
logger.info('log1');
setContextConfig(true);
logger.debug('log2');
logger.info('log3');
setContextConfig(false);
logger.info('log4');
expect(mockConsoleLog).toHaveBeenCalledTimes(2);
expect(mockConsoleLog).toHaveBeenCalledWith(
'CUSTOM - PATTERN [plugins.myplugin.debug_pattern][DEBUG] log2'
);
expect(mockConsoleLog).toHaveBeenCalledWith(
'CUSTOM - PATTERN [plugins.myplugin.debug_pattern][INFO ] log3'
);
});

it('writes debug_json context to custom JSON appender', async () => {
setContextConfig(true);
const logger = root.logger.get('plugins.myplugin.debug_json');
logger.debug('log1');
logger.info('log2');
expect(mockConsoleLog).toHaveBeenCalledTimes(2);

const [firstCall, secondCall] = mockConsoleLog.mock.calls.map(([jsonString]) =>
JSON.parse(jsonString)
);
expect(firstCall).toMatchObject({
level: 'DEBUG',
context: 'plugins.myplugin.debug_json',
message: 'log1',
});
expect(secondCall).toMatchObject({
level: 'INFO',
context: 'plugins.myplugin.debug_json',
message: 'log2',
});
});

it('writes info_json context to custom JSON appender', async () => {
setContextConfig(true);
const logger = root.logger.get('plugins.myplugin.info_json');
logger.debug('i should not be logged!');
logger.info('log2');

expect(mockConsoleLog).toHaveBeenCalledTimes(1);
expect(JSON.parse(mockConsoleLog.mock.calls[0][0])).toMatchObject({
level: 'INFO',
context: 'plugins.myplugin.info_json',
message: 'log2',
});
});

it('writes debug_pattern context to custom pattern appender', async () => {
setContextConfig(true);
const logger = root.logger.get('plugins.myplugin.debug_pattern');
logger.debug('log1');
logger.info('log2');

expect(mockConsoleLog).toHaveBeenCalledTimes(2);
expect(mockConsoleLog).toHaveBeenCalledWith(
'CUSTOM - PATTERN [plugins.myplugin.debug_pattern][DEBUG] log1'
);
expect(mockConsoleLog).toHaveBeenCalledWith(
'CUSTOM - PATTERN [plugins.myplugin.debug_pattern][INFO ] log2'
);
});

it('writes info_pattern context to custom pattern appender', async () => {
setContextConfig(true);
const logger = root.logger.get('plugins.myplugin.info_pattern');
logger.debug('i should not be logged!');
logger.info('log2');
expect(mockConsoleLog).toHaveBeenCalledTimes(1);
expect(mockConsoleLog).toHaveBeenCalledWith(
'CUSTOM - PATTERN [plugins.myplugin.info_pattern][INFO ] log2'
);
});

it('writes all context to both appenders', async () => {
setContextConfig(true);
const logger = root.logger.get('plugins.myplugin.all');
logger.debug('log1');
logger.info('log2');

expect(mockConsoleLog).toHaveBeenCalledTimes(4);
const logs = mockConsoleLog.mock.calls.map(([jsonString]) => jsonString);

expect(JSON.parse(logs[0])).toMatchObject({
level: 'DEBUG',
context: 'plugins.myplugin.all',
message: 'log1',
});
expect(logs[1]).toEqual('CUSTOM - PATTERN [plugins.myplugin.all][DEBUG] log1');
expect(JSON.parse(logs[2])).toMatchObject({
level: 'INFO',
context: 'plugins.myplugin.all',
message: 'log2',
});
expect(logs[3]).toEqual('CUSTOM - PATTERN [plugins.myplugin.all][INFO ] log2');
});
});
});
7 changes: 0 additions & 7 deletions test/plugin_functional/plugins/core_logging/kibana.json

This file was deleted.

This file was deleted.

23 changes: 0 additions & 23 deletions test/plugin_functional/plugins/core_logging/server/index.ts

This file was deleted.

118 changes: 0 additions & 118 deletions test/plugin_functional/plugins/core_logging/server/plugin.ts

This file was deleted.

13 changes: 0 additions & 13 deletions test/plugin_functional/plugins/core_logging/tsconfig.json

This file was deleted.

1 change: 0 additions & 1 deletion test/plugin_functional/test_suites/core_plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,5 @@ export default function ({ loadTestFile }: PluginFunctionalProviderContext) {
loadTestFile(require.resolve('./application_leave_confirm'));
loadTestFile(require.resolve('./application_status'));
loadTestFile(require.resolve('./rendering'));
loadTestFile(require.resolve('./logging'));
});
}
Loading

0 comments on commit 2c86ca5

Please sign in to comment.