Compare commits
No commits in common. "0573b4542979960f999ddfea5f52e4f13b483d89" and "51785e2b133b7ec5493d4eaa8584398d58c275f9" have entirely different histories.
0573b45429
...
51785e2b13
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@hibas123/logging",
|
"name": "@hibas123/logging",
|
||||||
"version": "2.1.1",
|
"version": "2.0.8",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "out/index.js",
|
"main": "out/index.js",
|
||||||
"types": "out/index.d.ts",
|
"types": "out/index.d.ts",
|
||||||
|
43
src/base.ts
43
src/base.ts
@ -34,23 +34,12 @@ export class LoggingBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private adapter = new Set<Adapter>();
|
private adapter: 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;
|
||||||
}
|
}
|
||||||
@ -64,9 +53,10 @@ export class LoggingBase {
|
|||||||
opt = options;
|
opt = options;
|
||||||
}
|
}
|
||||||
|
|
||||||
let config: LoggingBaseOptions = {
|
let config = {
|
||||||
name: undefined,
|
name: undefined,
|
||||||
console: true,
|
console: true,
|
||||||
|
files: true,
|
||||||
...opt
|
...opt
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -80,25 +70,13 @@ 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) {
|
||||||
if (!this.adapter.has(adapter)) {
|
this.adapter.push(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>;
|
||||||
@ -106,9 +84,7 @@ export class LoggingBase {
|
|||||||
if (sync) {
|
if (sync) {
|
||||||
this.adapter.forEach(elm => elm.flush(true));
|
this.adapter.forEach(elm => elm.flush(true));
|
||||||
} else {
|
} else {
|
||||||
let adapters: (void | Promise<void>)[] = [];
|
return Promise.all(this.adapter.map(elm => elm.flush(false))).then(() => { });
|
||||||
this.adapter.forEach(elm => adapters.push(elm.flush(false)));
|
|
||||||
return Promise.all(adapters).then(() => { });
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -121,31 +97,25 @@ 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("This is a fake error, to get a stack trace");
|
let e = new Error()
|
||||||
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));
|
||||||
@ -153,7 +123,6 @@ export class LoggingBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
errorMessage(...message: any[]) {
|
errorMessage(...message: any[]) {
|
||||||
if (this._logLevel <= LoggingTypes.Error)
|
|
||||||
this.message(LoggingTypes.Error, message);
|
this.message(LoggingTypes.Error, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
13
src/test.ts
13
src/test.ts
@ -1,4 +1,4 @@
|
|||||||
import { Logging, LoggingBase, LoggingTypes, Colors, withColor } from ".";
|
import { Logging, LoggingBase, Colors, withColor } from ".";
|
||||||
|
|
||||||
Logging.log("test")
|
Logging.log("test")
|
||||||
Logging.log("i", "am", { a: "an" }, 1000);
|
Logging.log("i", "am", { a: "an" }, 1000);
|
||||||
@ -32,14 +32,3 @@ 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");
|
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user