Add adapter based control of log level
This commit is contained in:
parent
feed4626e6
commit
8e183ac1a5
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "logging",
|
"name": "logging",
|
||||||
"version": "3.0.8",
|
"version": "3.1.0",
|
||||||
"description": "",
|
"description": "",
|
||||||
"author": "Fabian Stamm <dev@fabianstamm.de>",
|
"author": "Fabian Stamm <dev@fabianstamm.de>",
|
||||||
"contributors": [],
|
"contributors": [],
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@hibas123/logging",
|
"name": "@hibas123/logging",
|
||||||
"version": "3.0.8",
|
"version": "3.1.0",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "out/index.js",
|
"main": "out/index.js",
|
||||||
"types": "out/index.d.ts",
|
"types": "out/index.d.ts",
|
||||||
@ -29,6 +29,5 @@
|
|||||||
"nodemon": "^2.0.7",
|
"nodemon": "^2.0.7",
|
||||||
"ts-node": "^9.1.1",
|
"ts-node": "^9.1.1",
|
||||||
"typescript": "^4.2.4"
|
"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) {
|
for (const key in this) {
|
||||||
if (typeof this[key] === "function") {
|
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(": "),
|
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];
|
let formattedMessage: Formatted<string>[] = [...linePrefix];
|
||||||
message.forEach((msg, idx) => {
|
message.forEach((msg, idx) => {
|
||||||
let format = new Formatted();
|
let format = new Formatted();
|
||||||
@ -369,7 +358,11 @@ export class LoggingBase extends LoggingInterface {
|
|||||||
type,
|
type,
|
||||||
};
|
};
|
||||||
|
|
||||||
this.#adapters.forEach((adapter) => adapter.onMessage(msg));
|
this.#adapters.forEach((adapter) => {
|
||||||
|
if (adapter.level <= type) {
|
||||||
|
adapter.onMessage(msg);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async close() {
|
async close() {
|
||||||
@ -434,9 +427,11 @@ function getCallerFile() {
|
|||||||
return { file: undefined, line: 0 };
|
return { file: undefined, line: 0 };
|
||||||
}
|
}
|
||||||
|
|
||||||
function getCallerFromExisting(
|
function getCallerFromExisting(err: Error): {
|
||||||
err: Error
|
file: string;
|
||||||
): { file: string; line: number; column?: number } {
|
line: number;
|
||||||
|
column?: number;
|
||||||
|
} {
|
||||||
if (!err || !err.stack) return { file: "NOFILE", line: 0 };
|
if (!err || !err.stack) return { file: "NOFILE", line: 0 };
|
||||||
let lines = err.stack.split("\n");
|
let lines = err.stack.split("\n");
|
||||||
lines.shift(); // removing first line
|
lines.shift(); // removing first line
|
||||||
|
@ -5,6 +5,7 @@ import {
|
|||||||
TerminalFormats,
|
TerminalFormats,
|
||||||
Formatted,
|
Formatted,
|
||||||
IFormatted,
|
IFormatted,
|
||||||
|
LoggingTypes,
|
||||||
} from "./types.js";
|
} from "./types.js";
|
||||||
|
|
||||||
declare const Deno: any;
|
declare const Deno: any;
|
||||||
@ -15,11 +16,15 @@ const deno = typeof Deno !== "undefined";
|
|||||||
const NodeJS = typeof process !== undefined;
|
const NodeJS = typeof process !== undefined;
|
||||||
|
|
||||||
export class ConsoleAdapter implements Adapter {
|
export class ConsoleAdapter implements Adapter {
|
||||||
|
level: LoggingTypes = LoggingTypes.Debug;
|
||||||
constructor(private colors: boolean = true) {}
|
constructor(private colors: boolean = true) {}
|
||||||
|
|
||||||
init() {}
|
init() {}
|
||||||
flush() {}
|
flush() {}
|
||||||
|
|
||||||
|
setLevel(level: LoggingTypes) {
|
||||||
|
this.level = level;
|
||||||
|
}
|
||||||
// TODO: Check if required!
|
// TODO: Check if required!
|
||||||
// private escape(text: string): string {
|
// private escape(text: string): string {
|
||||||
// return text
|
// return text
|
||||||
|
@ -216,6 +216,8 @@ export interface Message {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface Adapter {
|
export interface Adapter {
|
||||||
|
readonly level: LoggingTypes;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This function initialises the Adapter. It might be called multiple times, when added to multiple instances
|
* This function initialises the Adapter. It might be called multiple times, when added to multiple instances
|
||||||
* @param observable An observable to subscribe to messages
|
* @param observable An observable to subscribe to messages
|
||||||
@ -225,6 +227,8 @@ export interface Adapter {
|
|||||||
flush(sync: true): void;
|
flush(sync: true): void;
|
||||||
flush(sync: false): void | Promise<void>;
|
flush(sync: false): void | Promise<void>;
|
||||||
|
|
||||||
|
setLevel(level: LoggingTypes): void;
|
||||||
|
|
||||||
onMessage(message: Message): void;
|
onMessage(message: Message): void;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user