Skip to content

Commit

Permalink
create kbn-legacy-logging package (#77678)
Browse files Browse the repository at this point in the history
* create kbn-legacy-logging package and start to move things

* fix rotator tests

* fix logging system test mocks

* move logging format to the package

* move logging setup to package

* adapt legacy logging server

* remove usage of legacy config in the legacy logging server

* move legacy logging server to package

* remove `??` syntax from package

* update generated doc

* fix a few things due to month old merge

* remove typings from project

* move reconfigureLogging to package

* add basic README file

* update generated doc

* remove old typings

* add typing for legacy logging events

* remove `??` from packages

* fix / improve event types usages

* remove suffix from tsconfig
  • Loading branch information
pgayvallet authored Nov 22, 2020
1 parent 5708c5d commit e176def
Show file tree
Hide file tree
Showing 36 changed files with 748 additions and 304 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@
"@kbn/config-schema": "link:packages/kbn-config-schema",
"@kbn/i18n": "link:packages/kbn-i18n",
"@kbn/interpreter": "link:packages/kbn-interpreter",
"@kbn/legacy-logging": "link:packages/kbn-legacy-logging",
"@kbn/logging": "link:packages/kbn-logging",
"@kbn/monaco": "link:packages/kbn-monaco",
"@kbn/std": "link:packages/kbn-std",
Expand Down
4 changes: 4 additions & 0 deletions packages/kbn-legacy-logging/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# @kbn/legacy-logging

This package contains the implementation of the legacy logging
system, based on `@hapi/good`
15 changes: 15 additions & 0 deletions packages/kbn-legacy-logging/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "@kbn/legacy-logging",
"version": "1.0.0",
"private": true,
"license": "Apache-2.0",
"main": "./target/index.js",
"scripts": {
"build": "tsc",
"kbn:bootstrap": "yarn build",
"kbn:watch": "yarn build --watch"
},
"dependencies": {
"@kbn/std": "link:../kbn-std"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,25 @@
*/

import _ from 'lodash';
import { getLoggerStream } from './log_reporter';
import { getLogReporter } from './log_reporter';
import { LegacyLoggingConfig } from './schema';

export default function loggingConfiguration(config) {
const events = config.get('logging.events');
/**
* Returns the `@hapi/good` plugin configuration to be used for the legacy logging
* @param config
*/
export function getLoggingConfiguration(config: LegacyLoggingConfig, opsInterval: number) {
const events = config.events;

if (config.get('logging.silent')) {
if (config.silent) {
_.defaults(events, {});
} else if (config.get('logging.quiet')) {
} else if (config.quiet) {
_.defaults(events, {
log: ['listening', 'error', 'fatal'],
request: ['error'],
error: '*',
});
} else if (config.get('logging.verbose')) {
} else if (config.verbose) {
_.defaults(events, {
log: '*',
ops: '*',
Expand All @@ -47,24 +52,24 @@ export default function loggingConfiguration(config) {
});
}

const loggerStream = getLoggerStream({
const loggerStream = getLogReporter({
config: {
json: config.get('logging.json'),
dest: config.get('logging.dest'),
timezone: config.get('logging.timezone'),
json: config.json,
dest: config.dest,
timezone: config.timezone,

// I'm adding the default here because if you add another filter
// using the commandline it will remove authorization. I want users
// to have to explicitly set --logging.filter.authorization=none or
// --logging.filter.cookie=none to have it show up in the logs.
filter: _.defaults(config.get('logging.filter'), {
filter: _.defaults(config.filter, {
authorization: 'remove',
cookie: 'remove',
}),
},
events: _.transform(
events,
function (filtered, val, key) {
function (filtered: Record<string, string>, val: string, key: string) {
// provide a string compatible way to remove events
if (val !== '!') filtered[key] = val;
},
Expand All @@ -74,7 +79,7 @@ export default function loggingConfiguration(config) {

const options = {
ops: {
interval: config.get('ops.interval'),
interval: opsInterval,
},
includes: {
request: ['headers', 'payload'],
Expand Down
25 changes: 25 additions & 0 deletions packages/kbn-legacy-logging/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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.
*/

export { LegacyLoggingConfig, legacyLoggingConfigSchema } from './schema';
export { attachMetaData } from './metadata';
export { setupLoggingRotate } from './rotate';
export { setupLogging, reconfigureLogging } from './setup_logging';
export { getLoggingConfiguration } from './get_logging_config';
export { LegacyLoggingServer } from './legacy_logging_server';
Original file line number Diff line number Diff line change
Expand Up @@ -17,40 +17,47 @@
* under the License.
*/

jest.mock('../../../../legacy/server/config');
jest.mock('../../../../legacy/server/logging');
jest.mock('./setup_logging');

import { LogLevel } from '../../logging';
import { LegacyLoggingServer } from './legacy_logging_server';
import { LegacyLoggingServer, LogRecord } from './legacy_logging_server';

test('correctly forwards log records.', () => {
const loggingServer = new LegacyLoggingServer({ events: {} });
const onLogMock = jest.fn();
loggingServer.events.on('log', onLogMock);

const timestamp = 1554433221100;
const firstLogRecord = {
const firstLogRecord: LogRecord = {
timestamp: new Date(timestamp),
pid: 5355,
level: LogLevel.Info,
level: {
id: 'info',
value: 5,
},
context: 'some-context',
message: 'some-message',
};

const secondLogRecord = {
const secondLogRecord: LogRecord = {
timestamp: new Date(timestamp),
pid: 5355,
level: LogLevel.Error,
level: {
id: 'error',
value: 3,
},
context: 'some-context.sub-context',
message: 'some-message',
meta: { unknown: 2 },
error: new Error('some-error'),
};

const thirdLogRecord = {
const thirdLogRecord: LogRecord = {
timestamp: new Date(timestamp),
pid: 5355,
level: LogLevel.Trace,
level: {
id: 'trace',
value: 7,
},
context: 'some-context.sub-context',
message: 'some-message',
meta: { tags: ['important', 'tags'], unknown: 2 },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,40 @@
* under the License.
*/

import { ServerExtType } from '@hapi/hapi';
import Podium from '@hapi/podium';
// @ts-expect-error: implicit any for JS file
import { Config } from '../../../../legacy/server/config';
// @ts-expect-error: implicit any for JS file
import { setupLogging } from '../../../../legacy/server/logging';
import { LogLevel, LogRecord } from '../../logging';
import { LegacyVars } from '../../types';

export const metadataSymbol = Symbol('log message with metadata');
export function attachMetaData(message: string, metadata: LegacyVars = {}) {
return {
[metadataSymbol]: {
message,
metadata,
},
};
import { ServerExtType, Server } from '@hapi/hapi';
import Podium from 'podium';
import { setupLogging } from './setup_logging';
import { attachMetaData } from './metadata';
import { legacyLoggingConfigSchema } from './schema';

// these LogXXX types are duplicated to avoid a cross dependency with the @kbn/logging package.
// typescript will error if they diverge at some point.
type LogLevelId = 'all' | 'fatal' | 'error' | 'warn' | 'info' | 'debug' | 'trace' | 'off';

interface LogLevel {
id: LogLevelId;
value: number;
}

export interface LogRecord {
timestamp: Date;
level: LogLevel;
context: string;
message: string;
error?: Error;
meta?: { [name: string]: any };
pid: number;
}

const isEmptyObject = (obj: object) => Object.keys(obj).length === 0;

function getDataToLog(error: Error | undefined, metadata: object, message: string) {
if (error) return error;
if (!isEmptyObject(metadata)) return attachMetaData(message, metadata);
if (error) {
return error;
}
if (!isEmptyObject(metadata)) {
return attachMetaData(message, metadata);
}
return message;
}

Expand All @@ -50,7 +61,7 @@ interface PluginRegisterParams {
options: PluginRegisterParams['options']
) => Promise<void>;
};
options: LegacyVars;
options: Record<string, any>;
}

/**
Expand Down Expand Up @@ -84,22 +95,19 @@ export class LegacyLoggingServer {

private onPostStopCallback?: () => void;

constructor(legacyLoggingConfig: Readonly<LegacyVars>) {
constructor(legacyLoggingConfig: any) {
// We set `ops.interval` to max allowed number and `ops` filter to value
// that doesn't exist to avoid logging of ops at all, if turned on it will be
// logged by the "legacy" Kibana.
const config = {
logging: {
...legacyLoggingConfig,
events: {
...legacyLoggingConfig.events,
ops: '__no-ops__',
},
const { value: loggingConfig } = legacyLoggingConfigSchema.validate({
...legacyLoggingConfig,
events: {
...legacyLoggingConfig.events,
ops: '__no-ops__',
},
ops: { interval: 2147483647 },
};
});

setupLogging(this, Config.withDefaultSchema(config));
setupLogging((this as unknown) as Server, loggingConfig, 2147483647);
}

public register({ plugin: { register }, options }: PluginRegisterParams): Promise<void> {
Expand Down
80 changes: 80 additions & 0 deletions packages/kbn-legacy-logging/src/log_events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* 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 { EventData, isEventData } from './metadata';

export interface BaseEvent {
event: string;
timestamp: number;
pid: number;
tags?: string[];
}

export interface ResponseEvent extends BaseEvent {
event: 'response';
method: 'GET' | 'POST' | 'PUT' | 'DELETE';
statusCode: number;
path: string;
headers: Record<string, string | string[]>;
responsePayload: string;
responseTime: string;
query: Record<string, any>;
}

export interface OpsEvent extends BaseEvent {
event: 'ops';
os: {
load: string[];
};
proc: Record<string, any>;
load: string;
}

export interface ErrorEvent extends BaseEvent {
event: 'error';
error: Error;
url: string;
}

export interface UndeclaredErrorEvent extends BaseEvent {
error: Error;
}

export interface LogEvent extends BaseEvent {
data: EventData;
}

export interface UnkownEvent extends BaseEvent {
data: string | Record<string, any>;
}

export type AnyEvent =
| ResponseEvent
| OpsEvent
| ErrorEvent
| UndeclaredErrorEvent
| LogEvent
| UnkownEvent;

export const isResponseEvent = (e: AnyEvent): e is ResponseEvent => e.event === 'response';
export const isOpsEvent = (e: AnyEvent): e is OpsEvent => e.event === 'ops';
export const isErrorEvent = (e: AnyEvent): e is ErrorEvent => e.event === 'error';
export const isLogEvent = (e: AnyEvent): e is LogEvent => isEventData((e as LogEvent).data);
export const isUndeclaredErrorEvent = (e: AnyEvent): e is UndeclaredErrorEvent =>
(e as any).error instanceof Error;
Loading

0 comments on commit e176def

Please sign in to comment.