From 475b7876123f6f28a767e343878759a4f621cdb6 Mon Sep 17 00:00:00 2001 From: Fabian Date: Sat, 12 Oct 2019 12:46:07 +0200 Subject: [PATCH] Binding logging functions to Object --- package.json | 2 +- src/base.ts | 27 ++++++++++++++++++++------- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index 0728e2d..dca01a0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@hibas123/logging", - "version": "2.1.0", + "version": "2.1.1", "description": "", "main": "out/index.js", "types": "out/index.d.ts", diff --git a/src/base.ts b/src/base.ts index 7858021..b7d0d1e 100644 --- a/src/base.ts +++ b/src/base.ts @@ -34,7 +34,7 @@ export class LoggingBase { } - private adapter: Adapter[] = []; + private adapter = new Set(); private adapter_init: Promise[] = []; private messageObservable = new Observable(); @@ -64,10 +64,9 @@ export class LoggingBase { opt = options; } - let config = { + let config: LoggingBaseOptions = { name: undefined, console: true, - files: true, ...opt }; @@ -81,12 +80,24 @@ export class LoggingBase { if (config.console) { 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) { - this.adapter.push(adapter); - let prms = Promise.resolve(adapter.init(this.messageObservable.getPublicApi(), this._name)); - this.adapter_init.push(prms); + if (!this.adapter.has(adapter)) { + this.adapter.add(adapter); + let prms = Promise.resolve(adapter.init(this.messageObservable.getPublicApi(), this._name)); + this.adapter_init.push(prms); + } } flush(sync: true): void; @@ -95,7 +106,9 @@ export class LoggingBase { if (sync) { this.adapter.forEach(elm => elm.flush(true)); } else { - return Promise.all(this.adapter.map(elm => elm.flush(false))).then(() => { }); + let adapters: (void | Promise)[] = []; + this.adapter.forEach(elm => adapters.push(elm.flush(false))); + return Promise.all(adapters).then(() => { }); } }