From 8e183ac1a5b8d70cb00f34837d252d1b6cfa6a7b Mon Sep 17 00:00:00 2001 From: User user Date: Tue, 18 May 2021 09:10:06 +0200 Subject: [PATCH] Add adapter based control of log level --- meta.json | 2 +- package.json | 5 ++--- src/base.ts | 27 +++++++++++---------------- src/consolewriter.ts | 5 +++++ src/types.ts | 4 ++++ 5 files changed, 23 insertions(+), 20 deletions(-) diff --git a/meta.json b/meta.json index 7a7a732..764ee5b 100644 --- a/meta.json +++ b/meta.json @@ -1,6 +1,6 @@ { "name": "logging", - "version": "3.0.8", + "version": "3.1.0", "description": "", "author": "Fabian Stamm ", "contributors": [], diff --git a/package.json b/package.json index 78e4796..9d80f9d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@hibas123/logging", - "version": "3.0.8", + "version": "3.1.0", "description": "", "main": "out/index.js", "types": "out/index.d.ts", @@ -29,6 +29,5 @@ "nodemon": "^2.0.7", "ts-node": "^9.1.1", "typescript": "^4.2.4" - }, - "dependencies": {} + } } diff --git a/src/base.ts b/src/base.ts index dc0de77..aa64ee7 100644 --- a/src/base.ts +++ b/src/base.ts @@ -77,7 +77,7 @@ export abstract class LoggingInterface implements ILoggingInterface { for (const key in this) { if (typeof this[key] === "function") { - this[key] = ((this[key] as never) as Function).bind(this); + this[key] = (this[key] as never as Function).bind(this); } } } @@ -319,17 +319,6 @@ export class LoggingBase extends LoggingInterface { new Formatted(": "), ]; - // let linePrefix = [ - // ...namePrefix, - // new Formatted(date_str, this.#format_map.date), - // new Formatted(" "), - // new Formatted(type_str, type_format), - // ...(file - // ? [new Formatted(" "), new Formatted(file, this.#format_map.file)] - // : []), - // new Formatted("| "), - // ]; - let formattedMessage: Formatted[] = [...linePrefix]; message.forEach((msg, idx) => { let format = new Formatted(); @@ -369,7 +358,11 @@ export class LoggingBase extends LoggingInterface { type, }; - this.#adapters.forEach((adapter) => adapter.onMessage(msg)); + this.#adapters.forEach((adapter) => { + if (adapter.level <= type) { + adapter.onMessage(msg); + } + }); } async close() { @@ -434,9 +427,11 @@ function getCallerFile() { return { file: undefined, line: 0 }; } -function getCallerFromExisting( - err: Error -): { file: string; line: number; column?: number } { +function getCallerFromExisting(err: Error): { + file: string; + line: number; + column?: number; +} { if (!err || !err.stack) return { file: "NOFILE", line: 0 }; let lines = err.stack.split("\n"); lines.shift(); // removing first line diff --git a/src/consolewriter.ts b/src/consolewriter.ts index 1ba57ba..4c6df36 100644 --- a/src/consolewriter.ts +++ b/src/consolewriter.ts @@ -5,6 +5,7 @@ import { TerminalFormats, Formatted, IFormatted, + LoggingTypes, } from "./types.js"; declare const Deno: any; @@ -15,11 +16,15 @@ const deno = typeof Deno !== "undefined"; const NodeJS = typeof process !== undefined; export class ConsoleAdapter implements Adapter { + level: LoggingTypes = LoggingTypes.Debug; constructor(private colors: boolean = true) {} init() {} flush() {} + setLevel(level: LoggingTypes) { + this.level = level; + } // TODO: Check if required! // private escape(text: string): string { // return text diff --git a/src/types.ts b/src/types.ts index 279b2c7..ea170af 100644 --- a/src/types.ts +++ b/src/types.ts @@ -216,6 +216,8 @@ export interface Message { } export interface Adapter { + readonly level: LoggingTypes; + /** * This function initialises the Adapter. It might be called multiple times, when added to multiple instances * @param observable An observable to subscribe to messages @@ -225,6 +227,8 @@ export interface Adapter { flush(sync: true): void; flush(sync: false): void | Promise; + setLevel(level: LoggingTypes): void; + onMessage(message: Message): void; /**