Skip to content
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

Merged
merged 11 commits into from
Feb 4, 2020

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 },
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rewrites a default appender

this.appenders.set(appenderKey, appenderSchema);

},
root: { level: 'info' },
root: { level: 'info', ...root },
Copy link
Contributor Author

@mshustov mshustov Feb 3, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: We allow users to configure roots. Although if a user specifies root.appenders without defaults NP logging service won't send logs to the legacy system. I think logging a warning is enough because I don't want to extend user-defined config value, nor enforce default value presence in root.appenders(since we will remove this logic in v8). WDYT?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logging a warning makes sense to me 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added config validation to prevent server start

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",
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

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