[Typescript/Chore] Convert LevelLogger Logger to Typescript (#34972) (#35033)

* [Typescript/Chore] Convert LevelLogger to Typescript

* prettier

* tags optional for warn

* fix shadow lint rule
This commit is contained in:
Tim Sullivan 2019-04-12 16:37:15 -07:00 committed by GitHub
parent 438bfa49ae
commit 7bee8eaab8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 42 deletions

View file

@ -1,42 +0,0 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
export class LevelLogger {
static createForServer(server, tags) {
return new LevelLogger(
(tags, msg) => server.log(tags, msg),
tags,
server.config().get('logging.verbose'));
}
constructor(logger, tags, isVerbose) {
this._logger = logger;
this._tags = tags;
this.isVerbose = isVerbose;
}
error(msg, tags = []) {
this._logger([...this._tags, ...tags, 'error'], msg);
}
warning(msg, tags = []) {
this._logger([...this._tags, ...tags, 'warning'], msg);
}
debug(msg, tags = []) {
this._logger([...this._tags, ...tags, 'debug'], msg);
}
info(msg, tags = []) {
this._logger([...this._tags, ...tags, 'info'], msg);
}
clone(tags) {
return new LevelLogger(this._logger, [...this._tags, ...tags], this.isVerbose);
}
}

View file

@ -0,0 +1,52 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { Logger } from '../../types';
type ServerLog = (tags: string[], msg: string) => void;
export class LevelLogger implements Logger {
private _logger: any;
private _tags: string[];
public warn: (msg: string, tags?: string[]) => void;
static createForServer(server: any, tags: string[]) {
const serverLog: ServerLog = (tgs: string[], msg: string) => server.log(tgs, msg);
return new LevelLogger(serverLog, tags);
}
constructor(logger: ServerLog, tags: string[]) {
this._logger = logger;
this._tags = tags;
/*
* This shortcut provides maintenance convenience: Reporting code has been
* using both .warn and .warning
*/
this.warn = this.warning.bind(this);
}
public error(msg: string, tags: string[] = []) {
this._logger([...this._tags, ...tags, 'error'], msg);
}
public warning(msg: string, tags: string[] = []) {
this._logger([...this._tags, ...tags, 'warning'], msg);
}
public debug(msg: string, tags: string[] = []) {
this._logger([...this._tags, ...tags, 'debug'], msg);
}
public info(msg: string, tags: string[] = []) {
this._logger([...this._tags, ...tags, 'info'], msg);
}
public clone(tags: string[]) {
return new LevelLogger(this._logger, [...this._tags, ...tags]);
}
}