Improve get child behavior
This commit is contained in:
parent
a1cd860688
commit
30f5e241ae
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@hibas123/logging",
|
||||
"version": "2.3.4",
|
||||
"version": "2.3.5",
|
||||
"description": "",
|
||||
"main": "out/index.js",
|
||||
"types": "out/index.d.ts",
|
||||
|
78
src/base.ts
78
src/base.ts
@ -1,4 +1,4 @@
|
||||
import { Observable } from "@hibas123/utils";
|
||||
import { Observable, ObservableInterface } from "@hibas123/utils";
|
||||
import { ConsoleAdapter } from "./consolewriter";
|
||||
import inspect from "./inspect";
|
||||
import {
|
||||
@ -13,7 +13,6 @@ import {
|
||||
FormattedText,
|
||||
FormattedLine,
|
||||
} from "./types";
|
||||
import Logging from ".";
|
||||
|
||||
const browser = typeof window !== "undefined";
|
||||
|
||||
@ -43,6 +42,24 @@ export interface LoggingBaseOptions {
|
||||
}
|
||||
|
||||
const adapterCache = new WeakMap<Adapter, number>();
|
||||
|
||||
class AdapterSet {
|
||||
change = new Observable<{ type: "add" | "remove"; adapter: Adapter }>();
|
||||
adapters: Set<Adapter> = new Set();
|
||||
|
||||
addAdapter(adapter: Adapter) {
|
||||
if (!this.adapters.has(adapter)) {
|
||||
this.adapters.add(adapter);
|
||||
this.change.send({
|
||||
type: "add",
|
||||
adapter: adapter,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const consoleAdapter = new ConsoleAdapter();
|
||||
|
||||
export class LoggingBase {
|
||||
private _formatMap: FormatConfig = new DefaultFormatConfig();
|
||||
|
||||
@ -50,7 +67,7 @@ export class LoggingBase {
|
||||
this._formatMap = value;
|
||||
}
|
||||
|
||||
private adapter = new Set<Adapter>();
|
||||
private adapterSet: AdapterSet;
|
||||
private adapter_init: Promise<void>[] = [];
|
||||
|
||||
private timerMap = new Map<string, { name: string; start: any }>();
|
||||
@ -72,7 +89,10 @@ export class LoggingBase {
|
||||
return this._name;
|
||||
}
|
||||
|
||||
constructor(options?: Partial<LoggingBaseOptions> | string) {
|
||||
constructor(
|
||||
options?: Partial<LoggingBaseOptions> | string,
|
||||
adapterSet?: AdapterSet
|
||||
) {
|
||||
let opt: Partial<LoggingBaseOptions>;
|
||||
if (!options) opt = {};
|
||||
else if (typeof options === "string") {
|
||||
@ -94,8 +114,21 @@ export class LoggingBase {
|
||||
this[key] = (<any>this[key]).bind(this);
|
||||
}
|
||||
|
||||
if (adapterSet) {
|
||||
this.adapterSet = adapterSet;
|
||||
this.adapterSet.adapters.forEach((a) => this.initAdapter(a));
|
||||
} else {
|
||||
this.adapterSet = new AdapterSet();
|
||||
}
|
||||
|
||||
this.adapterSet.change.subscribe((change) => {
|
||||
if (change.type === "add") {
|
||||
this.initAdapter(change.adapter);
|
||||
}
|
||||
});
|
||||
|
||||
if (config.console) {
|
||||
this.addAdapter(new ConsoleAdapter());
|
||||
this.addAdapter(consoleAdapter);
|
||||
}
|
||||
|
||||
//Binding function to this
|
||||
@ -109,22 +142,27 @@ export class LoggingBase {
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new logging instance, with all the adapters from this one. After that the new Instance operates independent
|
||||
* Can be used to override function from super class
|
||||
* @param child New child logging instance
|
||||
*/
|
||||
protected postGetChild(child: LoggingBase) {}
|
||||
|
||||
/**
|
||||
* Creates a new logging instance, with the adapters liked together.
|
||||
* @param name Name/Prefix of the new child. The actual name will resolve as "<parent-name>/<name>"
|
||||
*/
|
||||
getChild(name: string) {
|
||||
let lg = new LoggingBase({
|
||||
let lg = new LoggingBase(
|
||||
{
|
||||
console: false,
|
||||
name: this.name ? this.name + "/" + name : name,
|
||||
});
|
||||
this.adapter.forEach((a) => lg.addAdapter(a));
|
||||
},
|
||||
this.adapterSet
|
||||
);
|
||||
return lg;
|
||||
}
|
||||
|
||||
addAdapter(adapter: Adapter) {
|
||||
if (!this.adapter.has(adapter)) {
|
||||
this.adapter.add(adapter);
|
||||
|
||||
private initAdapter(adapter: Adapter) {
|
||||
let cached = adapterCache.get(adapter) || 0;
|
||||
adapterCache.set(adapter, cached + 1);
|
||||
|
||||
@ -133,16 +171,21 @@ export class LoggingBase {
|
||||
);
|
||||
this.adapter_init.push(prms);
|
||||
}
|
||||
|
||||
addAdapter(adapter: Adapter) {
|
||||
this.adapterSet.addAdapter(adapter);
|
||||
}
|
||||
|
||||
flush(sync: true): void;
|
||||
flush(sync: false): Promise<void>;
|
||||
flush(sync: boolean): void | Promise<void> {
|
||||
if (sync) {
|
||||
this.adapter.forEach((elm) => elm.flush(true));
|
||||
this.adapterSet.adapters.forEach((elm) => elm.flush(true));
|
||||
} else {
|
||||
let adapters: (void | Promise<void>)[] = [];
|
||||
this.adapter.forEach((elm) => adapters.push(elm.flush(false)));
|
||||
this.adapterSet.adapters.forEach((elm) =>
|
||||
adapters.push(elm.flush(false))
|
||||
);
|
||||
return Promise.all(adapters).then(() => {});
|
||||
}
|
||||
}
|
||||
@ -153,7 +196,7 @@ export class LoggingBase {
|
||||
if (this.$closed) return;
|
||||
this.$closed = true;
|
||||
|
||||
this.adapter.forEach((adapter) => {
|
||||
this.adapterSet.adapters.forEach((adapter) => {
|
||||
let cached = adapterCache.get(adapter);
|
||||
|
||||
if (cached) {
|
||||
@ -167,8 +210,7 @@ export class LoggingBase {
|
||||
adapter.close ? adapter.close() : undefined;
|
||||
});
|
||||
|
||||
this.adapter = undefined;
|
||||
|
||||
this.adapterSet = undefined;
|
||||
this.messageObservable.close();
|
||||
}
|
||||
|
||||
|
26
src/test.ts
26
src/test.ts
@ -49,5 +49,31 @@ Logging.warning("This should not be there 4");
|
||||
Logging.error("This should be there 1");
|
||||
Logging.errorMessage("This should be there 2");
|
||||
|
||||
const c1 = Logging.getChild("child-level-1");
|
||||
|
||||
c1.log("Hello from Child 1");
|
||||
|
||||
const c2 = c1.getChild("child-level-2");
|
||||
|
||||
c2.log("Hello from Child 2");
|
||||
|
||||
c2.addAdapter({
|
||||
init: (obs) =>
|
||||
obs.subscribe((msg) =>
|
||||
console.log(
|
||||
"Adapter adden on child level 2: ",
|
||||
"---",
|
||||
msg.name,
|
||||
msg.text.raw
|
||||
)
|
||||
),
|
||||
flush: () => undefined,
|
||||
close: () => undefined,
|
||||
});
|
||||
|
||||
c2.log("MSG from C2");
|
||||
c1.log("MSG from C1");
|
||||
Logging.log("MSG from root");
|
||||
|
||||
const timer = Logging.time("timer1", "Test Timer");
|
||||
setTimeout(() => timer.end(), 1000);
|
||||
|
Loading…
Reference in New Issue
Block a user