Adding LogLevel support

This commit is contained in:
Fabian
2019-07-13 12:10:20 +02:00
parent ca41468dc1
commit cc5fc5ae08
5 changed files with 138 additions and 112 deletions

View File

@ -40,6 +40,17 @@ export class LoggingBase {
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;
}
@ -97,25 +108,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 +140,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[] | string, caller?: { file: string, line: number }) {

View File

@ -1,4 +1,5 @@
import { Logging, LoggingBase } from ".";
import { LoggingTypes } from "./types";
Logging.log("test")
Logging.log("i", "am", { a: "an" }, 1000);
@ -28,4 +29,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
}