Skip to content

Commit

Permalink
Use "light" Hapi-like server for logging instead of real Hapi server.
Browse files Browse the repository at this point in the history
  • Loading branch information
azasypkin committed Aug 2, 2018
1 parent d4efe55 commit 814289d
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 19 deletions.
25 changes: 6 additions & 19 deletions src/core/server/legacy_compat/logging/appenders/legacy_appender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,10 @@
* under the License.
*/

import { Server } from 'hapi';

// @ts-ignore: implicit any for JS file
import { Config, transformDeprecations } from '../../../../../server/config';
// @ts-ignore: implicit any for JS file
import { setupLogging } from '../../../../../server/logging';

import { schema } from '../../../config/schema';
import { DisposableAppender } from '../../../logging/appenders/appenders';
import { LogRecord } from '../../../logging/log_record';
import { LegacyLoggingServer } from './legacy_logging_server';

/**
* Simple appender that just forwards `LogRecord` to the legacy KbnServer log.
Expand All @@ -38,29 +32,22 @@ export class LegacyAppender implements DisposableAppender {
legacy: schema.any(),
});

private readonly logServer: Server;
private readonly loggingServer: LegacyLoggingServer;

constructor(legacyConfig: Readonly<Record<string, any>>) {
// The "legacy" Kibana uses Hapi server + even-better plugin to log, so to
// have the same look & feel we should use that approach here too.
this.logServer = new Server();
setupLogging(this.logServer, Config.withDefaultSchema(transformDeprecations(legacyConfig)));
this.loggingServer = new LegacyLoggingServer(legacyConfig);
}

/**
* Forwards `LogRecord` to the legacy platform that will layout and
* write record to the configured destination.
* @param record `LogRecord` instance to forward to.
*/
public append({ level, context, message, error, timestamp, meta = {} }: LogRecord) {
this.logServer.log(
[level.id.toLowerCase(), ...context.split('.'), ...(meta.tags || [])],
error || message,
timestamp.getMilliseconds()
);
public append(record: LogRecord) {
this.loggingServer.log(record);
}

public async dispose() {
this.logServer.stop();
this.loggingServer.stop();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* 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 { EventEmitter } from 'events';
// @ts-ignore: implicit any for JS file
import { Config, transformDeprecations } from '../../../../../server/config';
// @ts-ignore: implicit any for JS file
import { setupLogging } from '../../../../../server/logging';
import { LogRecord } from '../../../logging/log_record';

interface PluginRegisterParams {
register: {
register: (
server: LegacyLoggingServer,
options: PluginRegisterParams['options'],
cb: () => void
) => void;
};
options: Record<string, any>;
}

/**
* The "legacy" Kibana uses Hapi server + even-better plugin to log, so we should
* use the same approach here to have the same look & feel. But to reduce overhead
* of having full blown Hapi server instance we create our own "light" version.
*/
export class LegacyLoggingServer extends EventEmitter {
constructor(legacyConfig: Readonly<Record<string, any>>) {
super();
setupLogging(this, Config.withDefaultSchema(transformDeprecations(legacyConfig)));
}

public register({ register: plugin, options }: PluginRegisterParams, cb: () => void) {
plugin.register(this, options, cb);
}

public log({ level, context, message, error, timestamp, meta = {} }: LogRecord) {
this.emit('log', {
data: error || message,
tags: [level.id.toLowerCase(), ...context.split('.'), ...(meta.tags || [])],
timestamp: timestamp.getMilliseconds(),
});
}

public stop() {
this.emit('stop');
}

public expose() {
// noop
}
}

0 comments on commit 814289d

Please sign in to comment.