Compare commits

..

No commits in common. "0573b4542979960f999ddfea5f52e4f13b483d89" and "51785e2b133b7ec5493d4eaa8584398d58c275f9" have entirely different histories.

4 changed files with 18 additions and 60 deletions

View File

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

View File

@ -34,23 +34,12 @@ export class LoggingBase {
}
private adapter = new Set<Adapter>();
private adapter: 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;
}
@ -64,9 +53,10 @@ export class LoggingBase {
opt = options;
}
let config: LoggingBaseOptions = {
let config = {
name: undefined,
console: true,
files: true,
...opt
};
@ -80,24 +70,12 @@ 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) {
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);
}
this.adapter.push(adapter);
let prms = Promise.resolve(adapter.init(this.messageObservable.getPublicApi(), this._name));
this.adapter_init.push(prms);
}
flush(sync: true): void;
@ -106,9 +84,7 @@ export class LoggingBase {
if (sync) {
this.adapter.forEach(elm => elm.flush(true));
} else {
let adapters: (void | Promise<void>)[] = [];
this.adapter.forEach(elm => adapters.push(elm.flush(false)));
return Promise.all(adapters).then(() => { });
return Promise.all(this.adapter.map(elm => elm.flush(false))).then(() => { });
}
}
@ -121,31 +97,25 @@ export class LoggingBase {
}
debug(...message: any[]) {
if (this._logLevel <= LoggingTypes.Debug)
this.message(LoggingTypes.Debug, message);
this.message(LoggingTypes.Debug, message);
}
log(...message: any[]) {
if (this._logLevel <= LoggingTypes.Log)
this.message(LoggingTypes.Log, message);
this.message(LoggingTypes.Log, message);
}
warning(...message: any[]) {
if (this._logLevel <= LoggingTypes.Warning)
this.message(LoggingTypes.Warning, message);
this.message(LoggingTypes.Warning, message);
}
warn(...message: any[]) {
if (this._logLevel <= LoggingTypes.Warning)
this.message(LoggingTypes.Warning, message);
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("This is a fake error, to get a stack trace");
let e = new Error()
this.message(LoggingTypes.Error, [error, "\n", e.stack]);
} else {
this.message(LoggingTypes.Error, [error.message, "\n", error.stack], getCallerFromExisting(error));
@ -153,8 +123,7 @@ export class LoggingBase {
}
errorMessage(...message: any[]) {
if (this._logLevel <= LoggingTypes.Error)
this.message(LoggingTypes.Error, message);
this.message(LoggingTypes.Error, message);
}
private message(type: LoggingTypes, message: any[], caller?: { file: string, line: number }) {

View File

@ -1,4 +1,4 @@
import { Logging, LoggingBase, LoggingTypes, Colors, withColor } from ".";
import { Logging, LoggingBase, Colors, withColor } from ".";
Logging.log("test")
Logging.log("i", "am", { a: "an" }, 1000);
@ -31,15 +31,4 @@ cus2.log("Hello from custom Logger 2")
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");
cus2.log("Hello from custom Logger 2")

View File

@ -1,10 +1,10 @@
import { ObservableInterface } from "@hibas123/utils";
export enum LoggingTypes {
Debug,
Log,
Warning,
Error
Error,
Debug
}