Binding logging functions to Object

This commit is contained in:
Fabian 2019-10-12 12:46:07 +02:00
parent cc5fc5ae08
commit 475b787612
2 changed files with 21 additions and 8 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "@hibas123/logging", "name": "@hibas123/logging",
"version": "2.1.0", "version": "2.1.1",
"description": "", "description": "",
"main": "out/index.js", "main": "out/index.js",
"types": "out/index.d.ts", "types": "out/index.d.ts",

View File

@ -34,7 +34,7 @@ export class LoggingBase {
} }
private adapter: Adapter[] = []; private adapter = new Set<Adapter>();
private adapter_init: Promise<void>[] = []; private adapter_init: Promise<void>[] = [];
private messageObservable = new Observable<Message>(); private messageObservable = new Observable<Message>();
@ -64,10 +64,9 @@ export class LoggingBase {
opt = options; opt = options;
} }
let config = { let config: LoggingBaseOptions = {
name: undefined, name: undefined,
console: true, console: true,
files: true,
...opt ...opt
}; };
@ -81,13 +80,25 @@ export class LoggingBase {
if (config.console) { if (config.console) {
this.addAdapter(new ConsoleAdapter()); this.addAdapter(new ConsoleAdapter());
} }
//Binding function to this
this.debug = this.debug.bind(this);
this.log = this.log.bind(this);
this.warn = this.warn.bind(this);
this.warning = this.warning.bind(this);
this.error = this.error.bind(this);
this.errorMessage = this.errorMessage.bind(this);
this.flush = this.flush.bind(this);
} }
addAdapter(adapter: Adapter) { addAdapter(adapter: Adapter) {
this.adapter.push(adapter); if (!this.adapter.has(adapter)) {
this.adapter.add(adapter);
let prms = Promise.resolve(adapter.init(this.messageObservable.getPublicApi(), this._name)); let prms = Promise.resolve(adapter.init(this.messageObservable.getPublicApi(), this._name));
this.adapter_init.push(prms); this.adapter_init.push(prms);
} }
}
flush(sync: true): void; flush(sync: true): void;
flush(sync: false): Promise<void>; flush(sync: false): Promise<void>;
@ -95,7 +106,9 @@ export class LoggingBase {
if (sync) { if (sync) {
this.adapter.forEach(elm => elm.flush(true)); this.adapter.forEach(elm => elm.flush(true));
} else { } else {
return Promise.all(this.adapter.map(elm => elm.flush(false))).then(() => { }); let adapters: (void | Promise<void>)[] = [];
this.adapter.forEach(elm => adapters.push(elm.flush(false)));
return Promise.all(adapters).then(() => { });
} }
} }