Compare commits
3 Commits
51785e2b13
...
0573b45429
Author | SHA1 | Date | |
---|---|---|---|
|
0573b45429 | ||
|
475b787612 | ||
|
cc5fc5ae08 |
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@hibas123/logging",
|
||||
"version": "2.0.8",
|
||||
"version": "2.1.1",
|
||||
"description": "",
|
||||
"main": "out/index.js",
|
||||
"types": "out/index.d.ts",
|
||||
|
43
src/base.ts
43
src/base.ts
@ -34,12 +34,23 @@ export class LoggingBase {
|
||||
}
|
||||
|
||||
|
||||
private adapter: Adapter[] = [];
|
||||
private adapter = new Set<Adapter>();
|
||||
private adapter_init: Promise<void>[] = [];
|
||||
|
||||
private messageObservable = new Observable<Message>();
|
||||
protected _name: string;
|
||||
|
||||
|
||||
private _logLevel = LoggingTypes.Debug;
|
||||
|
||||
get logLevel() {
|
||||
return this._logLevel;
|
||||
}
|
||||
|
||||
set logLevel(value: LoggingTypes) {
|
||||
this._logLevel = value;
|
||||
}
|
||||
|
||||
get name() {
|
||||
return this._name;
|
||||
}
|
||||
@ -53,10 +64,9 @@ export class LoggingBase {
|
||||
opt = options;
|
||||
}
|
||||
|
||||
let config = {
|
||||
let config: LoggingBaseOptions = {
|
||||
name: undefined,
|
||||
console: true,
|
||||
files: true,
|
||||
...opt
|
||||
};
|
||||
|
||||
@ -70,13 +80,25 @@ 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);
|
||||
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;
|
||||
flush(sync: false): Promise<void>;
|
||||
@ -84,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<void>)[] = [];
|
||||
this.adapter.forEach(elm => adapters.push(elm.flush(false)));
|
||||
return Promise.all(adapters).then(() => { });
|
||||
}
|
||||
}
|
||||
|
||||
@ -97,25 +121,31 @@ export class LoggingBase {
|
||||
}
|
||||
|
||||
debug(...message: any[]) {
|
||||
if (this._logLevel <= LoggingTypes.Debug)
|
||||
this.message(LoggingTypes.Debug, message);
|
||||
}
|
||||
|
||||
log(...message: any[]) {
|
||||
if (this._logLevel <= LoggingTypes.Log)
|
||||
this.message(LoggingTypes.Log, message);
|
||||
}
|
||||
|
||||
warning(...message: any[]) {
|
||||
if (this._logLevel <= LoggingTypes.Warning)
|
||||
this.message(LoggingTypes.Warning, message);
|
||||
}
|
||||
|
||||
warn(...message: any[]) {
|
||||
if (this._logLevel <= LoggingTypes.Warning)
|
||||
this.message(LoggingTypes.Warning, message);
|
||||
}
|
||||
|
||||
error(error: Error | string) {
|
||||
if (this._logLevel > LoggingTypes.Error)
|
||||
return;
|
||||
if (!error) error = "Empty ERROR was passed, so no informations available";
|
||||
if (typeof error === "string") {
|
||||
let e = new Error()
|
||||
let e = new Error("This is a fake error, to get a stack trace");
|
||||
this.message(LoggingTypes.Error, [error, "\n", e.stack]);
|
||||
} else {
|
||||
this.message(LoggingTypes.Error, [error.message, "\n", error.stack], getCallerFromExisting(error));
|
||||
@ -123,6 +153,7 @@ export class LoggingBase {
|
||||
}
|
||||
|
||||
errorMessage(...message: any[]) {
|
||||
if (this._logLevel <= LoggingTypes.Error)
|
||||
this.message(LoggingTypes.Error, message);
|
||||
}
|
||||
|
||||
|
13
src/test.ts
13
src/test.ts
@ -1,4 +1,4 @@
|
||||
import { Logging, LoggingBase, Colors, withColor } from ".";
|
||||
import { Logging, LoggingBase, LoggingTypes, Colors, withColor } from ".";
|
||||
|
||||
Logging.log("test")
|
||||
Logging.log("i", "am", { a: "an" }, 1000);
|
||||
@ -32,3 +32,14 @@ cus22.log("Hello from custom Logger 22")
|
||||
cus2.log("Hello from custom Logger 2")
|
||||
cus22.log("Hello from custom Logger 22")
|
||||
cus2.log("Hello from custom Logger 2")
|
||||
|
||||
Logging.debug("Only Errors should appear:")
|
||||
Logging.logLevel = LoggingTypes.Error;
|
||||
|
||||
Logging.debug("This should not be there 1");
|
||||
Logging.log("This should not be there 2");
|
||||
Logging.warn("This should not be there 3");
|
||||
Logging.warning("This should not be there 4");
|
||||
|
||||
Logging.error("This should be there 1");
|
||||
Logging.errorMessage("This should be there 2");
|
@ -1,10 +1,10 @@
|
||||
import { ObservableInterface } from "@hibas123/utils";
|
||||
|
||||
export enum LoggingTypes {
|
||||
Debug,
|
||||
Log,
|
||||
Warning,
|
||||
Error,
|
||||
Debug
|
||||
Error
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user