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", "name": "@hibas123/logging",
"version": "2.0.8", "version": "2.1.1",
"description": "", "description": "",
"main": "out/index.js", "main": "out/index.js",
"types": "out/index.d.ts", "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 adapter_init: Promise<void>[] = [];
private messageObservable = new Observable<Message>(); private messageObservable = new Observable<Message>();
protected _name: string; protected _name: string;
private _logLevel = LoggingTypes.Debug;
get logLevel() {
return this._logLevel;
}
set logLevel(value: LoggingTypes) {
this._logLevel = value;
}
get name() { get name() {
return this._name; return this._name;
} }
@ -53,10 +64,9 @@ export class LoggingBase {
opt = options; opt = options;
} }
let config = { let config: LoggingBaseOptions = {
name: undefined, name: undefined,
console: true, console: true,
files: true,
...opt ...opt
}; };
@ -70,13 +80,25 @@ export class LoggingBase {
if (config.console) { if (config.console) {
this.addAdapter(new ConsoleAdapter()); 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) { 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)); let prms = Promise.resolve(adapter.init(this.messageObservable.getPublicApi(), this._name));
this.adapter_init.push(prms); this.adapter_init.push(prms);
} }
}
flush(sync: true): void; flush(sync: true): void;
flush(sync: false): Promise<void>; flush(sync: false): Promise<void>;
@ -84,7 +106,9 @@ export class LoggingBase {
if (sync) { if (sync) {
this.adapter.forEach(elm => elm.flush(true)); this.adapter.forEach(elm => elm.flush(true));
} else { } 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[]) { debug(...message: any[]) {
if (this._logLevel <= LoggingTypes.Debug)
this.message(LoggingTypes.Debug, message); this.message(LoggingTypes.Debug, message);
} }
log(...message: any[]) { log(...message: any[]) {
if (this._logLevel <= LoggingTypes.Log)
this.message(LoggingTypes.Log, message); this.message(LoggingTypes.Log, message);
} }
warning(...message: any[]) { warning(...message: any[]) {
if (this._logLevel <= LoggingTypes.Warning)
this.message(LoggingTypes.Warning, message); this.message(LoggingTypes.Warning, message);
} }
warn(...message: any[]) { warn(...message: any[]) {
if (this._logLevel <= LoggingTypes.Warning)
this.message(LoggingTypes.Warning, message); this.message(LoggingTypes.Warning, message);
} }
error(error: Error | string) { error(error: Error | string) {
if (this._logLevel > LoggingTypes.Error)
return;
if (!error) error = "Empty ERROR was passed, so no informations available"; if (!error) error = "Empty ERROR was passed, so no informations available";
if (typeof error === "string") { 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]); this.message(LoggingTypes.Error, [error, "\n", e.stack]);
} else { } else {
this.message(LoggingTypes.Error, [error.message, "\n", error.stack], getCallerFromExisting(error)); this.message(LoggingTypes.Error, [error.message, "\n", error.stack], getCallerFromExisting(error));
@ -123,6 +153,7 @@ export class LoggingBase {
} }
errorMessage(...message: any[]) { errorMessage(...message: any[]) {
if (this._logLevel <= LoggingTypes.Error)
this.message(LoggingTypes.Error, message); this.message(LoggingTypes.Error, message);
} }

View File

@ -1,4 +1,4 @@
import { Logging, LoggingBase, Colors, withColor } from "."; import { Logging, LoggingBase, LoggingTypes, Colors, withColor } from ".";
Logging.log("test") Logging.log("test")
Logging.log("i", "am", { a: "an" }, 1000); 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") cus2.log("Hello from custom Logger 2")
cus22.log("Hello from custom Logger 22") 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"; import { ObservableInterface } from "@hibas123/utils";
export enum LoggingTypes { export enum LoggingTypes {
Debug,
Log, Log,
Warning, Warning,
Error, Error
Debug
} }