34 lines
1017 B
TypeScript
34 lines
1017 B
TypeScript
import { LogLevel } from './log-message';
|
|
import Logger from './logger';
|
|
import { default as Axios, AxiosInstance } from 'axios';
|
|
|
|
/* tslint:disable:max-classes-per-file*/
|
|
export class LogService {
|
|
|
|
public static getRootLogger(): Logger { return Logger.ROOT_LOGGER; }
|
|
|
|
private loggers: { [key: string]: Logger } = { };
|
|
private http: AxiosInstance = Axios.create();
|
|
|
|
public getLogger(name: string, threshold?: LogLevel): Logger {
|
|
if (this.loggers.hasOwnProperty(name)) { return this.loggers[name]; }
|
|
|
|
let parentLogger: Logger;
|
|
|
|
const parentLoggerName = Object.keys(this.loggers)
|
|
.filter((n: string) => name.startsWith(n))
|
|
.reduce((acc: string, cur: string) => acc.length > cur.length ? acc : cur);
|
|
|
|
if (parentLoggerName) {
|
|
parentLogger = this.loggers[parentLoggerName];
|
|
} else {
|
|
parentLogger = Logger.ROOT_LOGGER;
|
|
}
|
|
|
|
this.loggers[name] = parentLogger.createChildLogger(name, threshold);
|
|
return this.loggers[name];
|
|
}
|
|
}
|
|
|
|
export default new LogService();
|