Skip to content

Commit

Permalink
Address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
joshdover committed Jun 18, 2020
1 parent c0b7ce0 commit 88f6173
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 25 deletions.
24 changes: 14 additions & 10 deletions src/core/server/logging/logging_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const ROOT_CONTEXT_NAME = 'root';
*/
const DEFAULT_APPENDER_NAME = 'default';

const createLevelSchema = schema.oneOf(
const levelSchema = schema.oneOf(
[
schema.literal('all'),
schema.literal('fatal'),
Expand All @@ -55,21 +55,21 @@ const createLevelSchema = schema.oneOf(
}
);

const createLoggerSchema = schema.object({
const loggerSchema = schema.object({
appenders: schema.arrayOf(schema.string(), { defaultValue: [] }),
context: schema.string(),
level: createLevelSchema,
level: levelSchema,
});

/** @public */
export type LoggerConfigType = TypeOf<typeof createLoggerSchema>;
export type LoggerConfigType = TypeOf<typeof loggerSchema>;
export const config = {
path: 'logging',
schema: schema.object({
appenders: schema.mapOf(schema.string(), Appenders.configSchema, {
defaultValue: new Map<string, AppenderConfigType>(),
}),
loggers: schema.arrayOf(createLoggerSchema, {
loggers: schema.arrayOf(loggerSchema, {
defaultValue: [],
}),
root: schema.object(
Expand All @@ -78,7 +78,7 @@ export const config = {
defaultValue: [DEFAULT_APPENDER_NAME],
minSize: 1,
}),
level: createLevelSchema,
level: levelSchema,
},
{
validate(rawConfig) {
Expand All @@ -98,7 +98,7 @@ export const loggerContextConfigSchema = schema.object({
defaultValue: new Map<string, AppenderConfigType>(),
}),

loggers: schema.arrayOf(createLoggerSchema, { defaultValue: [] }),
loggers: schema.arrayOf(loggerSchema, { defaultValue: [] }),
});

/** @public */
Expand Down Expand Up @@ -178,11 +178,15 @@ export class LoggingConfig {
* @param contextConfig
*/
public extend(contextConfig: LoggerContextConfigType) {
// Use a Map to de-dupe any loggers for the same context. contextConfig overrides existing config.
const mergedLoggers = new Map<string, LoggerConfigType>([
...this.configType.loggers.map((l) => [l.context, l] as [string, LoggerConfigType]),
...contextConfig.loggers.map((l) => [l.context, l] as [string, LoggerConfigType]),
]);

const mergedConfig: LoggingConfigType = {
appenders: new Map([...this.configType.appenders, ...contextConfig.appenders]),
// Could include duplicate contexts, however the logic in `fillLoggersConfig` should
// ensure the latest value wins (from the new config)
loggers: [...this.configType.loggers, ...contextConfig.loggers],
loggers: [...mergedLoggers.values()],
root: this.configType.root,
};

Expand Down
18 changes: 15 additions & 3 deletions src/core/server/logging/logging_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,16 @@ describe('LoggingService', () => {
};

setup.configure(['test', 'context'], of(config1, config2));
expect(loggingSystem.setContextConfig).toHaveBeenCalledWith(['test', 'context'], config1);
expect(loggingSystem.setContextConfig).toHaveBeenCalledWith(['test', 'context'], config2);
expect(loggingSystem.setContextConfig).toHaveBeenNthCalledWith(
1,
['test', 'context'],
config1
);
expect(loggingSystem.setContextConfig).toHaveBeenNthCalledWith(
2,
['test', 'context'],
config2
);
});

it('stops forwarding first observable when called a second time', () => {
Expand All @@ -66,7 +74,11 @@ describe('LoggingService', () => {
setup.configure(['test', 'context'], updates$);
setup.configure(['test', 'context'], of(config1));
updates$.next(config2);
expect(loggingSystem.setContextConfig).toHaveBeenCalledWith(['test', 'context'], config1);
expect(loggingSystem.setContextConfig).toHaveBeenNthCalledWith(
1,
['test', 'context'],
config1
);
expect(loggingSystem.setContextConfig).not.toHaveBeenCalledWith(['test', 'context'], config2);
});
});
Expand Down
2 changes: 1 addition & 1 deletion src/core/server/logging/logging_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export interface LoggingServiceSetup {
*
* @remarks
* Assumes that that the `context` property of the individual `logger` items emitted by `config$`
* are relative to the `baseContextParts`.
* are relative to the plugin's logging context (defaults to `plugins.<plugin_id>`).
*
* @example
* Customize the configuration for the plugins.data.search context.
Expand Down
15 changes: 4 additions & 11 deletions src/core/server/logging/logging_system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export class LoggingSystem implements LoggerFactory {
*/
public upgrade(rawConfig: LoggingConfigType) {
const config = new LoggingConfig(rawConfig)!;
this.applyConfig(config);
this.applyBaseConfig(config);
}

/**
Expand Down Expand Up @@ -107,7 +107,7 @@ export class LoggingSystem implements LoggerFactory {
// If we already have a base config, apply the config. If not, custom context configs
// will be picked up on next call to `upgrade`.
if (this.baseConfig) {
this.applyConfig(this.baseConfig);
this.applyBaseConfig(this.baseConfig);
}
}

Expand All @@ -117,9 +117,7 @@ export class LoggingSystem implements LoggerFactory {
* @returns Promise that is resolved once all loggers are successfully disposed.
*/
public async stop() {
for (const appender of this.appenders.values()) {
await appender.dispose();
}
await Promise.all([...this.appenders.values()].map((a) => a.dispose()));

await this.bufferAppender.dispose();

Expand Down Expand Up @@ -153,12 +151,7 @@ export class LoggingSystem implements LoggerFactory {
return this.getLoggerConfigByContext(config, LoggingConfig.getParentLoggerContext(context));
}

private applyConfig(newBaseConfig: LoggingConfig) {
// Config update is asynchronous and may require some time to complete, so we should invalidate
// config so that new loggers will be using BufferAppender until newly configured appenders are ready.
this.baseConfig = undefined;
this.computedConfig = undefined;

private applyBaseConfig(newBaseConfig: LoggingConfig) {
const computedConfig = [...this.contextConfigs.values()].reduce(
(baseConfig, contextConfig) => baseConfig.extend(contextConfig),
newBaseConfig
Expand Down

0 comments on commit 88f6173

Please sign in to comment.