Compare commits

...

3 Commits

Author SHA1 Message Date
Fabian
0573b45429 Merging 2019-11-17 16:44:57 +01:00
Fabian
475b787612 Binding logging functions to Object 2019-10-12 12:46:07 +02:00
Fabian
cc5fc5ae08 Adding LogLevel support 2019-07-13 12:10:20 +02:00
4 changed files with 60 additions and 18 deletions

View File

@ -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",

View File

@ -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,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;
@ -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[]) {
this.message(LoggingTypes.Debug, message);
if (this._logLevel <= LoggingTypes.Debug)
this.message(LoggingTypes.Debug, message);
}
log(...message: any[]) {
this.message(LoggingTypes.Log, message);
if (this._logLevel <= LoggingTypes.Log)
this.message(LoggingTypes.Log, message);
}
warning(...message: any[]) {
this.message(LoggingTypes.Warning, message);
if (this._logLevel <= LoggingTypes.Warning)
this.message(LoggingTypes.Warning, message);
}
warn(...message: any[]) {
this.message(LoggingTypes.Warning, message);
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,7 +153,8 @@ export class LoggingBase {
}
errorMessage(...message: any[]) {
this.message(LoggingTypes.Error, message);
if (this._logLevel <= LoggingTypes.Error)
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, Colors, withColor } from ".";
import { Logging, LoggingBase, LoggingTypes, Colors, withColor } from ".";
Logging.log("test")
Logging.log("i", "am", { a: "an" }, 1000);
@ -31,4 +31,15 @@ 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")
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");

View File

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