Skip to content

Commit 92243ba

Browse files
committed
move logging types to @kbn/logging
1 parent b913f21 commit 92243ba

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+383
-164
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@
140140
"@kbn/config-schema": "1.0.0",
141141
"@kbn/i18n": "1.0.0",
142142
"@kbn/interpreter": "1.0.0",
143+
"@kbn/logging": "1.0.0",
143144
"@kbn/pm": "1.0.0",
144145
"@kbn/telemetry-tools": "1.0.0",
145146
"@kbn/test-subj-selector": "0.2.1",

packages/kbn-logging/README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# kbn-logging
2+
3+
Base types for the kibana platform logging system.
4+
5+
Note that this package currently only contains logging types. The only concrete implementation
6+
is still in `core`.
7+
8+
- [Loggers, Appenders and Layouts](#loggers-appenders-and-layouts)
9+
- [Logger hierarchy](#logger-hierarchy)
10+
- [Log level](#log-level)
11+
- [Layouts](#layouts)
12+
13+
The way logging works in Kibana is inspired by `log4j 2` logging framework used by [Elasticsearch](https://www.elastic.co/guide/en/elasticsearch/reference/current/settings.html#logging).
14+
The main idea is to have consistent logging behaviour (configuration, log format etc.) across the entire Elastic Stack
15+
where possible.
16+
17+
## Loggers, Appenders and Layouts
18+
19+
Kibana logging system has three main components: _loggers_, _appenders_ and _layouts_. These components allow us to log
20+
messages according to message type and level, and to control how these messages are formatted and where the final logs
21+
will be displayed or stored.
22+
23+
__Loggers__ define what logging settings should be applied at the particular context.
24+
25+
__Appenders__ define where log messages are displayed (eg. stdout or console) and stored (eg. file on the disk).
26+
27+
__Layouts__ define how log messages are formatted and what type of information they include.
28+
29+
30+
## Logger hierarchy
31+
32+
Every logger has its unique name or context that follows hierarchical naming rule. The logger is considered to be an
33+
ancestor of another logger if its name followed by a `.` is a prefix of the descendant logger name. For example logger
34+
with `a.b` context is an ancestor of logger with `a.b.c` context. All top-level loggers are descendants of special
35+
logger with `root` context that resides at the top of the logger hierarchy. This logger always exists and
36+
fully configured.
37+
38+
Developer can configure _log level_ and _appenders_ that should be used within particular context. If logger configuration
39+
specifies only _log level_ then _appenders_ configuration will be inherited from the ancestor logger.
40+
41+
__Note:__ in the current implementation log messages are only forwarded to appenders configured for a particular logger
42+
context or to appenders of the closest ancestor if current logger doesn't have any appenders configured. That means that
43+
we __don't support__ so called _appender additivity_ when log messages are forwarded to _every_ distinct appender within
44+
ancestor chain including `root`.
45+
46+
## Log level
47+
48+
Currently we support the following log levels: _all_, _fatal_, _error_, _warn_, _info_, _debug_, _trace_, _off_.
49+
Levels are ordered, so _all_ > _fatal_ > _error_ > _warn_ > _info_ > _debug_ > _trace_ > _off_.
50+
A log record is being logged by the logger if its level is higher than or equal to the level of its logger. Otherwise,
51+
the log record is ignored.
52+
53+
The _all_ and _off_ levels can be used only in configuration and are just handy shortcuts that allow developer to log every
54+
log record or disable logging entirely for the specific context.
55+
56+
## Layouts
57+
58+
Every appender should know exactly how to format log messages before they are written to the console or file on the disk.
59+
This behaviour is controlled by the layouts and configured through `appender.layout` configuration property for every
60+
custom appender (see examples in [Configuration](#configuration)). Currently we don't define any default layout for the
61+
custom appenders, so one should always make the choice explicitly.

packages/kbn-logging/package.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "@kbn/logging",
3+
"version": "1.0.0",
4+
"private": true,
5+
"license": "Apache-2.0",
6+
"main": "./target/index.js",
7+
"scripts": {
8+
"build": "tsc",
9+
"kbn:bootstrap": "yarn build",
10+
"kbn:watch": "yarn build --watch"
11+
},
12+
"dependencies": {
13+
"rxjs": "^6.5.5",
14+
"@kbn/config-schema": "1.0.0",
15+
"moment-timezone": "^0.5.27"
16+
},
17+
"devDependencies": {
18+
"typescript": "4.0.2"
19+
}
20+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
import { LogRecord } from './log_record';
21+
22+
/**
23+
* Entity that can append `LogRecord` instances to file, stdout, memory or whatever
24+
* is implemented internally. It's supposed to be used by `Logger`.
25+
* @internal
26+
*/
27+
export interface Appender {
28+
append(record: LogRecord): void;
29+
}
30+
31+
/**
32+
* This interface should be additionally implemented by the `Appender`'s if they are supposed
33+
* to be properly disposed. It's intentionally separated from `Appender` interface so that `Logger`
34+
* that interacts with `Appender` doesn't have control over appender lifetime.
35+
* @internal
36+
*/
37+
export interface DisposableAppender extends Appender {
38+
dispose: () => void;
39+
}

packages/kbn-logging/src/index.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
export { LogLevel, LogLevelId } from './log_level';
21+
export { LogRecord } from './log_record';
22+
export { Logger, LogMeta } from './logger';
23+
export { LoggerFactory } from './logger_factory';
24+
export { Layout } from './layout';
25+
export { Appender, DisposableAppender } from './appenders';

packages/kbn-logging/src/layout.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
import { LogRecord } from './log_record';
21+
22+
/**
23+
* Entity that can format `LogRecord` instance into a string.
24+
* @internal
25+
*/
26+
export interface Layout {
27+
format(record: LogRecord): string;
28+
}

src/core/server/logging/log_level.ts renamed to packages/kbn-logging/src/log_level.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* under the License.
1818
*/
1919

20-
import { assertNever } from '../../utils';
20+
import { assertNever } from './utils';
2121

2222
/**
2323
* Possible log level string values.
File renamed without changes.

packages/kbn-logging/src/logger.ts

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
import { LogRecord } from './log_record';
21+
22+
/**
23+
* Contextual metadata
24+
*
25+
* @public
26+
*/
27+
export interface LogMeta {
28+
[key: string]: any;
29+
}
30+
31+
/**
32+
* Logger exposes all the necessary methods to log any type of information and
33+
* this is the interface used by the logging consumers including plugins.
34+
*
35+
* @public
36+
*/
37+
export interface Logger {
38+
/**
39+
* Log messages at the most detailed log level
40+
*
41+
* @param message - The log message
42+
* @param meta -
43+
*/
44+
trace(message: string, meta?: LogMeta): void;
45+
46+
/**
47+
* Log messages useful for debugging and interactive investigation
48+
* @param message - The log message
49+
* @param meta -
50+
*/
51+
debug(message: string, meta?: LogMeta): void;
52+
53+
/**
54+
* Logs messages related to general application flow
55+
* @param message - The log message
56+
* @param meta -
57+
*/
58+
info(message: string, meta?: LogMeta): void;
59+
60+
/**
61+
* Logs abnormal or unexpected errors or messages
62+
* @param errorOrMessage - An Error object or message string to log
63+
* @param meta -
64+
*/
65+
warn(errorOrMessage: string | Error, meta?: LogMeta): void;
66+
67+
/**
68+
* Logs abnormal or unexpected errors or messages that caused a failure in the application flow
69+
*
70+
* @param errorOrMessage - An Error object or message string to log
71+
* @param meta -
72+
*/
73+
error(errorOrMessage: string | Error, meta?: LogMeta): void;
74+
75+
/**
76+
* Logs abnormal or unexpected errors or messages that caused an unrecoverable failure
77+
*
78+
* @param errorOrMessage - An Error object or message string to log
79+
* @param meta -
80+
*/
81+
fatal(errorOrMessage: string | Error, meta?: LogMeta): void;
82+
83+
/** @internal */
84+
log(record: LogRecord): void;
85+
86+
/**
87+
* Returns a new {@link Logger} instance extending the current logger context.
88+
*
89+
* @example
90+
* ```typescript
91+
* const logger = loggerFactory.get('plugin', 'service'); // 'plugin.service' context
92+
* const subLogger = logger.get('feature'); // 'plugin.service.feature' context
93+
* ```
94+
*/
95+
get(...childContextPaths: string[]): Logger;
96+
}

0 commit comments

Comments
 (0)