Compare commits
9 Commits
6daf815ea8
...
master
Author | SHA1 | Date | |
---|---|---|---|
3de5f368ef | |||
e0b51625d8 | |||
153aca0ccb | |||
7ca0c4fd72 | |||
8e183ac1a5 | |||
feed4626e6 | |||
9d4e521619 | |||
7d75f65dd3 | |||
eeed068ddd |
54
benchmark.js
Normal file
54
benchmark.js
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
const { LoggingBase } = require("./out/index.js");
|
||||||
|
let results = {};
|
||||||
|
|
||||||
|
function benchmark(name, count, runner) {
|
||||||
|
console.profile(name);
|
||||||
|
const start = process.hrtime.bigint()
|
||||||
|
|
||||||
|
runner(count);
|
||||||
|
|
||||||
|
const diffNS = process.hrtime.bigint() - start;
|
||||||
|
const diffMS = Number(diffNS / 1000n / 1000n);
|
||||||
|
console.profileEnd(name)
|
||||||
|
|
||||||
|
results[name]= {
|
||||||
|
count,
|
||||||
|
time: diffMS,
|
||||||
|
timePerI: (diffMS / count).toFixed(4)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
benchmark("simple", 10000000, (cnt) => {
|
||||||
|
const l = new LoggingBase({
|
||||||
|
console: false
|
||||||
|
});
|
||||||
|
for (let i = 0; i < cnt; i++) {
|
||||||
|
l.log("simple log")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
benchmark("complex", 1000000, (cnt) => {
|
||||||
|
const l = new LoggingBase({
|
||||||
|
console: false
|
||||||
|
});
|
||||||
|
for (let i = 0; i < cnt; i++) {
|
||||||
|
l.log("complex log", {
|
||||||
|
a: 1,
|
||||||
|
b: {
|
||||||
|
c:"test"
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
benchmark("very long", 10000000, (cnt) => {
|
||||||
|
const l = new LoggingBase({
|
||||||
|
console: false
|
||||||
|
});
|
||||||
|
const longText = "complex log".repeat(100);
|
||||||
|
for (let i = 0; i < cnt; i++) {
|
||||||
|
l.log(longText)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
console.table(results)
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "logging",
|
"name": "logging",
|
||||||
"version": "3.0.3",
|
"version": "3.1.2",
|
||||||
"description": "",
|
"description": "",
|
||||||
"author": "Fabian Stamm <dev@fabianstamm.de>",
|
"author": "Fabian Stamm <dev@fabianstamm.de>",
|
||||||
"contributors": [],
|
"contributors": [],
|
||||||
|
5839
package-lock.json
generated
Normal file
5839
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
24
package.json
24
package.json
@ -1,15 +1,18 @@
|
|||||||
{
|
{
|
||||||
"name": "@hibas123/logging",
|
"name": "@hibas123/logging",
|
||||||
"version": "3.0.3",
|
"version": "4.0.1",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "out/index.js",
|
"type": "module",
|
||||||
"types": "out/index.d.ts",
|
"main": "esm/index.js",
|
||||||
|
"types": "esm/index.d.ts",
|
||||||
"module": "esm/index.js",
|
"module": "esm/index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"prepublish": "npm run build",
|
"prepublishOnly": "npm run build",
|
||||||
"build": "tsc && tsc -p tsconfig.esm.json",
|
"build": "tsc",
|
||||||
"test": "tsc && node out/test.js",
|
"test": "tsc && node esm/test.js",
|
||||||
"postpublish": "denreg publish"
|
"postpublish": "denreg publish",
|
||||||
|
"bench": "tsc && node benchmark.js",
|
||||||
|
"prof": "tsc && ndb --prof benchmark.js"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
@ -19,16 +22,15 @@
|
|||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"files": [
|
"files": [
|
||||||
"src/",
|
"src/",
|
||||||
"out/",
|
|
||||||
"esm/",
|
"esm/",
|
||||||
"tsconfig.json",
|
"tsconfig.json",
|
||||||
"readme.md"
|
"readme.md"
|
||||||
],
|
],
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"concurrently": "^6.0.2",
|
"concurrently": "^6.0.2",
|
||||||
|
"ndb": "^1.1.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": {}
|
}
|
||||||
}
|
|
98
src/base.ts
98
src/base.ts
@ -6,28 +6,13 @@ import {
|
|||||||
Format,
|
Format,
|
||||||
FormatConfig,
|
FormatConfig,
|
||||||
Formatted,
|
Formatted,
|
||||||
|
ILoggingInterface,
|
||||||
LoggingTypes,
|
LoggingTypes,
|
||||||
Message,
|
Message,
|
||||||
} from "./types.js";
|
} from "./types.js";
|
||||||
|
|
||||||
const browser = typeof window !== "undefined";
|
const browser = typeof window !== "undefined";
|
||||||
|
|
||||||
export interface ILoggingTimer {
|
|
||||||
end: () => number;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface ILoggingInterface {
|
|
||||||
debug(...message: any[]): void;
|
|
||||||
log(...message: any[]): void;
|
|
||||||
warn(...message: any[]): void;
|
|
||||||
error(...message: any[]): void;
|
|
||||||
|
|
||||||
time(name?: string): ILoggingTimer;
|
|
||||||
timeEnd(id: string): number;
|
|
||||||
|
|
||||||
getChild(name: string): ILoggingInterface;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface ILoggingOptions {
|
export interface ILoggingOptions {
|
||||||
/**
|
/**
|
||||||
* Name will be prefixed on Console output and added to logfiles, if not specified here
|
* Name will be prefixed on Console output and added to logfiles, if not specified here
|
||||||
@ -89,6 +74,12 @@ export abstract class LoggingInterface implements ILoggingInterface {
|
|||||||
|
|
||||||
constructor(names: string[]) {
|
constructor(names: string[]) {
|
||||||
this.#names = names;
|
this.#names = names;
|
||||||
|
|
||||||
|
for (const key in this) {
|
||||||
|
if (typeof this[key] === "function") {
|
||||||
|
this[key] = (this[key] as never as Function).bind(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
debug(...message: any[]) {
|
debug(...message: any[]) {
|
||||||
@ -179,7 +170,10 @@ export class LoggingBase extends LoggingInterface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
getChild(name: string) {
|
getChild(name: string) {
|
||||||
return new LoggingBase.DecoupledLogging([this.names, name], this.#lg);
|
return new LoggingBase.DecoupledLogging(
|
||||||
|
[...this.names, name],
|
||||||
|
this.#lg
|
||||||
|
);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -208,12 +202,6 @@ export class LoggingBase extends LoggingInterface {
|
|||||||
if (options.console) {
|
if (options.console) {
|
||||||
this.addAdapter(consoleAdapter);
|
this.addAdapter(consoleAdapter);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const key in this) {
|
|
||||||
if (typeof this[key] === "function") {
|
|
||||||
this[key] = ((this[key] as never) as Function).bind(this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async addAdapter(adapter: Adapter) {
|
async addAdapter(adapter: Adapter) {
|
||||||
@ -234,7 +222,7 @@ export class LoggingBase extends LoggingInterface {
|
|||||||
if (!init) {
|
if (!init) {
|
||||||
add();
|
add();
|
||||||
} else {
|
} else {
|
||||||
Promise.resolve(init).then(add);
|
await Promise.resolve(init).then(add);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -255,7 +243,8 @@ export class LoggingBase extends LoggingInterface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const date = new Date();
|
const date = new Date();
|
||||||
const date_str = date.toISOString().replace(/T/, " ").replace(/\..+/, "");
|
const isoStr = date.toISOString();
|
||||||
|
const date_str = isoStr.substring(0, 10) + " " + isoStr.substring(11, 19);
|
||||||
|
|
||||||
let file: string | undefined = undefined;
|
let file: string | undefined = undefined;
|
||||||
if (this.#resolve_filename) {
|
if (this.#resolve_filename) {
|
||||||
@ -331,23 +320,14 @@ 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: Formatted;
|
||||||
if (msg instanceof Formatted) {
|
if (msg instanceof Formatted) {
|
||||||
format = msg;
|
format = msg;
|
||||||
msg = msg.content;
|
msg = msg.content;
|
||||||
|
} else {
|
||||||
|
format = new Formatted();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof msg !== "string") {
|
if (typeof msg !== "string") {
|
||||||
@ -358,18 +338,17 @@ export class LoggingBase extends LoggingInterface {
|
|||||||
}) as string;
|
}) as string;
|
||||||
}
|
}
|
||||||
|
|
||||||
removeColors(msg)
|
// removeColors(msg) // Remove colors is uncommented for now, since there are no real benefits of having it and it reduces performance
|
||||||
.split("\n")
|
msg.split("\n").forEach((text, index, { length }) => {
|
||||||
.forEach((text, index, { length }) => {
|
if (index != length - 1) {
|
||||||
if (index != length - 1) {
|
formattedMessage.push(
|
||||||
formattedMessage.push(
|
new Formatted(text + "\n", format),
|
||||||
new Formatted(text + "\n", format),
|
...linePrefix
|
||||||
...linePrefix
|
);
|
||||||
);
|
} else {
|
||||||
} else {
|
formattedMessage.push(new Formatted(text, format));
|
||||||
formattedMessage.push(new Formatted(text, format));
|
}
|
||||||
}
|
});
|
||||||
});
|
|
||||||
formattedMessage.push(new Formatted(" "));
|
formattedMessage.push(new Formatted(" "));
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -381,26 +360,30 @@ 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() {
|
||||||
if (this.#closed) return;
|
if (this.#closed) return;
|
||||||
this.#closed = true;
|
this.#closed = true;
|
||||||
|
|
||||||
this.#adapters.forEach((adapter) => {
|
for (const adapter of this.#adapters) {
|
||||||
const cnt = LoggingBase[InitialisedAdapters].get(adapter);
|
const cnt = LoggingBase[InitialisedAdapters].get(adapter);
|
||||||
if (!cnt) {
|
if (!cnt) {
|
||||||
//TODO: should not happen!
|
//TODO: should not happen!
|
||||||
} else {
|
} else {
|
||||||
if (cnt <= 1) {
|
if (cnt <= 1) {
|
||||||
adapter.close();
|
if (adapter.close) await adapter.close();
|
||||||
LoggingBase[InitialisedAdapters].delete(adapter);
|
LoggingBase[InitialisedAdapters].delete(adapter);
|
||||||
} else {
|
} else {
|
||||||
LoggingBase[InitialisedAdapters].set(adapter, cnt - 1);
|
LoggingBase[InitialisedAdapters].set(adapter, cnt - 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -446,9 +429,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
|
||||||
@ -479,6 +464,7 @@ function getCallerFromExisting(
|
|||||||
|
|
||||||
export function removeColors(text: string) {
|
export function removeColors(text: string) {
|
||||||
text = text.replace(
|
text = text.replace(
|
||||||
|
// Putting regex here directly instead of externally actually improves performance. The cause of that is not clear for now.
|
||||||
/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g,
|
/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g,
|
||||||
""
|
""
|
||||||
);
|
);
|
||||||
|
@ -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
|
||||||
|
@ -14,6 +14,8 @@ export {
|
|||||||
TerminalFormats,
|
TerminalFormats,
|
||||||
Formatted,
|
Formatted,
|
||||||
IFormatted,
|
IFormatted,
|
||||||
|
ILoggingInterface,
|
||||||
|
ILoggingTimer,
|
||||||
} from "./types.js";
|
} from "./types.js";
|
||||||
|
|
||||||
const Logging = new LoggingBase();
|
const Logging = new LoggingBase();
|
||||||
|
22
src/types.ts
22
src/types.ts
@ -203,7 +203,7 @@ export class DefaultFormatConfig implements FormatConfig {
|
|||||||
|
|
||||||
date = new Formatted()._color(IColors.NONE);
|
date = new Formatted()._color(IColors.NONE);
|
||||||
file = new Formatted()._color(IColors.NONE);
|
file = new Formatted()._color(IColors.NONE);
|
||||||
names = new Formatted()._bold()._color(IColors.GREEN);
|
names = new Formatted()._bold()._color(IColors.CYAN);
|
||||||
names_delimiter = new Formatted(" -> ")._bold();
|
names_delimiter = new Formatted(" -> ")._bold();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -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;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -234,3 +238,19 @@ export interface Adapter {
|
|||||||
*/
|
*/
|
||||||
close?(): void;
|
close?(): void;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface ILoggingTimer {
|
||||||
|
end: () => number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ILoggingInterface {
|
||||||
|
debug(...message: any[]): void;
|
||||||
|
log(...message: any[]): void;
|
||||||
|
warn(...message: any[]): void;
|
||||||
|
error(...message: any[]): void;
|
||||||
|
|
||||||
|
time(name?: string): ILoggingTimer;
|
||||||
|
timeEnd(id: string): number;
|
||||||
|
|
||||||
|
getChild(name: string): ILoggingInterface;
|
||||||
|
}
|
||||||
|
@ -1,11 +0,0 @@
|
|||||||
{
|
|
||||||
"extends": "./tsconfig.json",
|
|
||||||
"compilerOptions": {
|
|
||||||
"module": "ESNext",
|
|
||||||
"target": "ES2017",
|
|
||||||
"moduleResolution": "node",
|
|
||||||
"outDir": "esm"
|
|
||||||
},
|
|
||||||
"exclude": ["node_modules"],
|
|
||||||
"include": ["src"]
|
|
||||||
}
|
|
@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"module": "commonjs",
|
"module": "ESNext",
|
||||||
"target": "esnext",
|
"target": "ESNext",
|
||||||
|
"moduleResolution": "node",
|
||||||
|
"outDir": "esm",
|
||||||
"noImplicitAny": false,
|
"noImplicitAny": false,
|
||||||
"sourceMap": true,
|
"sourceMap": true,
|
||||||
"outDir": "out",
|
"declaration": true
|
||||||
"declaration": true,
|
|
||||||
"typeRoots": ["node_modules/@types"]
|
|
||||||
},
|
},
|
||||||
"exclude": ["node_modules"],
|
"exclude": ["node_modules"],
|
||||||
"include": ["src"]
|
"include": ["src"]
|
||||||
|
Reference in New Issue
Block a user