Add adapter based control of log level
This commit is contained in:
parent
feed4626e6
commit
8e183ac1a5
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "logging",
|
||||
"version": "3.0.8",
|
||||
"version": "3.1.0",
|
||||
"description": "",
|
||||
"author": "Fabian Stamm <dev@fabianstamm.de>",
|
||||
"contributors": [],
|
||||
|
@ -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": {}
|
||||
}
|
||||
}
|
||||
|
27
src/base.ts
27
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<string>[] = [...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
|
||||
|
@ -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
|
||||
|
@ -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<void>;
|
||||
|
||||
setLevel(level: LoggingTypes): void;
|
||||
|
||||
onMessage(message: Message): void;
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user