Skip to content

Commit

Permalink
Start consuming np logging config (#56480)
Browse files Browse the repository at this point in the history
* 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
mshustov authored Feb 4, 2020
1 parent 05edbda commit a36ec32
Show file tree
Hide file tree
Showing 12 changed files with 495 additions and 106 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 11 additions & 4 deletions src/core/server/legacy/config/legacy_object_to_config_adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 },
loggers,
};

if (configValue.silent) {
Expand Down
239 changes: 239 additions & 0 deletions src/core/server/legacy/integration_tests/logging.test.ts
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
",
]
`);
});
});
});
});

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 0 additions & 6 deletions src/core/server/logging/appenders/appenders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,6 @@ export type AppenderConfigType = TypeOf<typeof appendersSchema>;
*/
export interface Appender {
append(record: LogRecord): void;

/**
* Used to signal to `Logger` that log level filtering should be ignored for this appender. Defaults to `false`.
* @deprecated Should be removed once the `LegacyAppender` is removed.
*/
receiveAllLevels?: boolean;
}

/**
Expand Down
Loading

0 comments on commit a36ec32

Please sign in to comment.