-
Notifications
You must be signed in to change notification settings - Fork 8.2k
/
logging_config.ts
220 lines (193 loc) · 6.63 KB
/
logging_config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
* 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 { schema, TypeOf } from '@kbn/config-schema';
import { AppenderConfigType, Appenders } from './appenders/appenders';
// We need this helper for the types to be correct
// (otherwise it assumes an array of A|B instead of a tuple [A,B])
const toTuple = <A, B>(a: A, b: B): [A, B] => [a, b];
/**
* Separator string that used within nested context name (eg. plugins.pid).
*/
const CONTEXT_SEPARATOR = '.';
/**
* Name of the `root` context that always exists and sits at the top of logger hierarchy.
*/
const ROOT_CONTEXT_NAME = 'root';
/**
* Name of the appender that is always presented and used by `root` logger by default.
*/
const DEFAULT_APPENDER_NAME = 'default';
const createLevelSchema = schema.oneOf(
[
schema.literal('all'),
schema.literal('fatal'),
schema.literal('error'),
schema.literal('warn'),
schema.literal('info'),
schema.literal('debug'),
schema.literal('trace'),
schema.literal('off'),
],
{
defaultValue: 'info',
}
);
const createLoggerSchema = schema.object({
appenders: schema.arrayOf(schema.string(), { defaultValue: [] }),
context: schema.string(),
level: createLevelSchema,
});
/** @internal */
export type LoggerConfigType = TypeOf<typeof createLoggerSchema>;
export const config = {
path: 'logging',
schema: schema.object({
appenders: schema.mapOf(schema.string(), Appenders.configSchema, {
defaultValue: new Map<string, AppenderConfigType>(),
}),
loggers: schema.arrayOf(createLoggerSchema, {
defaultValue: [],
}),
root: schema.object({
appenders: schema.arrayOf(schema.string(), {
defaultValue: [DEFAULT_APPENDER_NAME],
minSize: 1,
}),
level: createLevelSchema,
}),
}),
};
export type LoggingConfigType = TypeOf<typeof config.schema>;
/**
* Describes the config used to fully setup logging subsystem.
* @internal
*/
export class LoggingConfig {
/**
* Helper method that joins separate string context parts into single context string.
* In case joined context is an empty string, `root` context name is returned.
* @param contextParts List of the context parts (e.g. ['parent', 'child'].
* @returns {string} Joined context string (e.g. 'parent.child').
*/
public static getLoggerContext(contextParts: string[]) {
return contextParts.join(CONTEXT_SEPARATOR) || ROOT_CONTEXT_NAME;
}
/**
* Helper method that returns parent context for the specified one.
* @param context Context to find parent for.
* @returns Name of the parent context or `root` if the context is the top level one.
*/
public static getParentLoggerContext(context: string) {
const lastIndexOfSeparator = context.lastIndexOf(CONTEXT_SEPARATOR);
if (lastIndexOfSeparator === -1) {
return ROOT_CONTEXT_NAME;
}
return context.slice(0, lastIndexOfSeparator);
}
/**
* Map of the appender unique arbitrary key and its corresponding config.
*/
public readonly appenders: Map<string, AppenderConfigType> = new Map([
[
'default',
{
kind: 'console',
layout: { kind: 'pattern', highlight: true },
} as AppenderConfigType,
],
[
'console',
{
kind: 'console',
layout: { kind: 'pattern', highlight: true },
} as AppenderConfigType,
],
[
'file',
{
kind: 'file',
layout: { kind: 'pattern', highlight: false },
} as AppenderConfigType,
],
]);
/**
* Map of the logger unique arbitrary key (context) and its corresponding config.
*/
public readonly loggers: Map<string, LoggerConfigType> = new Map();
constructor(configType: LoggingConfigType) {
this.fillAppendersConfig(configType);
this.fillLoggersConfig(configType);
}
private fillAppendersConfig(loggingConfig: LoggingConfigType) {
for (const [appenderKey, appenderSchema] of loggingConfig.appenders) {
this.appenders.set(appenderKey, appenderSchema);
}
}
private fillLoggersConfig(loggingConfig: LoggingConfigType) {
// Include `root` logger into common logger list so that it can easily be a part
// of the logger hierarchy and put all the loggers in map for easier retrieval.
const loggers = [
{ context: ROOT_CONTEXT_NAME, ...loggingConfig.root },
...loggingConfig.loggers,
];
const loggerConfigByContext = new Map(
loggers.map(loggerConfig => toTuple(loggerConfig.context, loggerConfig))
);
for (const [loggerContext, loggerConfig] of loggerConfigByContext) {
// Ensure logger config only contains valid appenders.
const unsupportedAppenderKey = loggerConfig.appenders.find(
appenderKey => !this.appenders.has(appenderKey)
);
if (unsupportedAppenderKey) {
throw new Error(
`Logger "${loggerContext}" contains unsupported appender key "${unsupportedAppenderKey}".`
);
}
const appenders = getAppenders(loggerConfig, loggerConfigByContext);
// We expect `appenders` to never be empty at this point, since the `root` context config should always
// have at least one appender that is enforced by the config schema validation.
this.loggers.set(loggerContext, {
...loggerConfig,
appenders,
});
}
}
}
/**
* Get appenders for logger config.
*
* If config for current context doesn't have any defined appenders inherit
* appenders from the parent context config.
*/
function getAppenders(
loggerConfig: LoggerConfigType,
loggerConfigByContext: Map<string, LoggerConfigType>
) {
let currentContext = loggerConfig.context;
let appenders = loggerConfig.appenders;
while (appenders.length === 0) {
const parentContext = LoggingConfig.getParentLoggerContext(currentContext);
const parentLogger = loggerConfigByContext.get(parentContext);
if (parentLogger) {
appenders = parentLogger.appenders;
}
currentContext = parentContext;
}
return appenders;
}