Making filename output optional

This commit is contained in:
Fabian Stamm 2020-09-30 16:46:16 +02:00
parent b92caf6468
commit bcff79fc90
4 changed files with 88 additions and 63 deletions

62
package-lock.json generated
View File

@ -1,13 +1,13 @@
{ {
"name": "@hibas123/logging", "name": "@hibas123/logging",
"version": "2.5.0", "version": "2.6.0",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {
"@hibas123/utils": { "@hibas123/utils": {
"version": "2.2.9", "version": "2.2.10",
"resolved": "https://registry.npmjs.org/@hibas123/utils/-/utils-2.2.9.tgz", "resolved": "https://registry.npmjs.org/@hibas123/utils/-/utils-2.2.10.tgz",
"integrity": "sha512-g5+Fjx9Dqscn7kcS1hJXRqTanxvlWk3Uc5PEF0nluDOgv3RsPyVzahXLbsgJM3t/UIo8E68EDT9eydqweu94eA==" "integrity": "sha512-hRLnTEvyisC690DS/YciVo/6dvykfDh/KvKi2wZ6VCdXQPK7pSqunCrwjuxruigjF+aknv9N8i602uQOzoAkEQ=="
}, },
"@sindresorhus/is": { "@sindresorhus/is": {
"version": "0.14.0", "version": "0.14.0",
@ -184,9 +184,9 @@
} }
}, },
"supports-color": { "supports-color": {
"version": "7.1.0", "version": "7.2.0",
"resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.1.0.tgz", "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz",
"integrity": "sha512-oRSIpR8pxT1Wr2FquTNnGet79b3BWljqOuoW/h4oBhxJ/HUbX5nX6JSruTkvXDCFMwDPvsaTTbvMLKZWSy0R5g==", "integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==",
"dev": true, "dev": true,
"requires": { "requires": {
"has-flag": "^4.0.0" "has-flag": "^4.0.0"
@ -302,9 +302,9 @@
"dev": true "dev": true
}, },
"cli-boxes": { "cli-boxes": {
"version": "2.2.0", "version": "2.2.1",
"resolved": "https://registry.npmjs.org/cli-boxes/-/cli-boxes-2.2.0.tgz", "resolved": "https://registry.npmjs.org/cli-boxes/-/cli-boxes-2.2.1.tgz",
"integrity": "sha512-gpaBrMAizVEANOpfZp/EEUixTXDyGt7DFzdK5hU+UbWt/J0lB0w20ncZj59Z9a93xHb9u12zF5BS6i9RKbtg4w==", "integrity": "sha512-y4coMcylgSCdVinjiDBuR8PCC2bLjyGTwEmPb9NHR/QaNU6EUOXcTY/s6VjGMD6ENSEaeQYHCY0GNGS5jfMwPw==",
"dev": true "dev": true
}, },
"cliui": { "cliui": {
@ -386,9 +386,9 @@
"dev": true "dev": true
}, },
"date-fns": { "date-fns": {
"version": "2.15.0", "version": "2.16.1",
"resolved": "https://registry.npmjs.org/date-fns/-/date-fns-2.15.0.tgz", "resolved": "https://registry.npmjs.org/date-fns/-/date-fns-2.16.1.tgz",
"integrity": "sha512-ZCPzAMJZn3rNUvvQIMlXhDr4A+Ar07eLeGsGREoWU19a3Pqf5oYa+ccd+B3F6XVtQY6HANMFdOQ8A+ipFnvJdQ==", "integrity": "sha512-sAJVKx/FqrLYHAQeN7VpJrPhagZc9R4ImZIWYRFZaaohR3KzmuK88touwsSwSVT8Qcbd4zoDsnGfX4GFB4imyQ==",
"dev": true "dev": true
}, },
"debug": { "debug": {
@ -434,9 +434,9 @@
"dev": true "dev": true
}, },
"dot-prop": { "dot-prop": {
"version": "5.2.0", "version": "5.3.0",
"resolved": "https://registry.npmjs.org/dot-prop/-/dot-prop-5.2.0.tgz", "resolved": "https://registry.npmjs.org/dot-prop/-/dot-prop-5.3.0.tgz",
"integrity": "sha512-uEUyaDKoSQ1M4Oq8l45hSE26SnTxL6snNnqvK/VWx5wJhmff5z0FUVJDKDanor/6w3kzE3i7XZOk+7wC0EXr1A==", "integrity": "sha512-QM8q3zDe58hqUqjraQOmzZ1LIH9SWQJTlEKCH4kJ2oQvLZk7RbQXvtDM2XEq3fwkV9CCvvH4LA0AV+ogFsBM2Q==",
"dev": true, "dev": true,
"requires": { "requires": {
"is-obj": "^2.0.0" "is-obj": "^2.0.0"
@ -1075,9 +1075,9 @@
} }
}, },
"rxjs": { "rxjs": {
"version": "6.6.2", "version": "6.6.3",
"resolved": "https://registry.npmjs.org/rxjs/-/rxjs-6.6.2.tgz", "resolved": "https://registry.npmjs.org/rxjs/-/rxjs-6.6.3.tgz",
"integrity": "sha512-BHdBMVoWC2sL26w//BCu3YzKT4s2jip/WhwsGEDmeKYBhKDZeYezVUnHatYB7L85v5xs0BAQmg6BEYJEKxBabg==", "integrity": "sha512-trsQc+xYYXZ3urjOiJOuCOa5N3jAZ3eiSpQB5hIT8zGlL2QfnHLJ2r7GMkBGuIausdJN1OneaI6gQlsqNHHmZQ==",
"dev": true, "dev": true,
"requires": { "requires": {
"tslib": "^1.9.0" "tslib": "^1.9.0"
@ -1167,9 +1167,9 @@
} }
}, },
"spdx-license-ids": { "spdx-license-ids": {
"version": "3.0.5", "version": "3.0.6",
"resolved": "https://registry.npmjs.org/spdx-license-ids/-/spdx-license-ids-3.0.5.tgz", "resolved": "https://registry.npmjs.org/spdx-license-ids/-/spdx-license-ids-3.0.6.tgz",
"integrity": "sha512-J+FWzZoynJEXGphVIS+XEh3kFSjZX/1i9gFBaWQcB+/tmpe2qUsSBABpcxqxnAxFdiUFEgAX1bjYGQvIZmoz9Q==", "integrity": "sha512-+orQK83kyMva3WyPf59k1+Y525csj5JejicWut55zeTWANuN17qSiSLUXWtzHeNWORSvT7GLDJ/E/XiIWoXBTw==",
"dev": true "dev": true
}, },
"string-width": { "string-width": {
@ -1278,9 +1278,9 @@
} }
}, },
"typescript": { "typescript": {
"version": "4.0.2", "version": "4.0.3",
"resolved": "https://registry.npmjs.org/typescript/-/typescript-4.0.2.tgz", "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.0.3.tgz",
"integrity": "sha512-e4ERvRV2wb+rRZ/IQeb3jm2VxBsirQLpQhdxplZ2MEzGvDkkMmPglecnNDfSUBivMjP93vRbngYYDQqQ/78bcQ==", "integrity": "sha512-tEu6DGxGgRJPb/mVPIZ48e69xCn2yRmCgYmDugAVwmJ6o+0u1RI18eO7E7WBTLYLaEVVOhwQmcdhQHweux/WPg==",
"dev": true "dev": true
}, },
"undefsafe": { "undefsafe": {
@ -1319,9 +1319,9 @@
} }
}, },
"update-notifier": { "update-notifier": {
"version": "4.1.1", "version": "4.1.3",
"resolved": "https://registry.npmjs.org/update-notifier/-/update-notifier-4.1.1.tgz", "resolved": "https://registry.npmjs.org/update-notifier/-/update-notifier-4.1.3.tgz",
"integrity": "sha512-9y+Kds0+LoLG6yN802wVXoIfxYEwh3FlZwzMwpCZp62S2i1/Jzeqb9Eeeju3NSHccGGasfGlK5/vEHbAifYRDg==", "integrity": "sha512-Yld6Z0RyCYGB6ckIjffGOSOmHXj1gMeE7aROz4MG+XMkmixBX4jUngrGXNYz7wPKBmtoD4MnBa2Anu7RSKht/A==",
"dev": true, "dev": true,
"requires": { "requires": {
"boxen": "^4.2.0", "boxen": "^4.2.0",
@ -1381,9 +1381,9 @@
"dev": true "dev": true
}, },
"supports-color": { "supports-color": {
"version": "7.1.0", "version": "7.2.0",
"resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.1.0.tgz", "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz",
"integrity": "sha512-oRSIpR8pxT1Wr2FquTNnGet79b3BWljqOuoW/h4oBhxJ/HUbX5nX6JSruTkvXDCFMwDPvsaTTbvMLKZWSy0R5g==", "integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==",
"dev": true, "dev": true,
"requires": { "requires": {
"has-flag": "^4.0.0" "has-flag": "^4.0.0"

View File

@ -1,6 +1,6 @@
{ {
"name": "@hibas123/logging", "name": "@hibas123/logging",
"version": "2.5.4", "version": "2.6.0",
"description": "", "description": "",
"main": "out/index.js", "main": "out/index.js",
"types": "out/index.d.ts", "types": "out/index.d.ts",
@ -27,7 +27,7 @@
"concurrently": "^5.3.0", "concurrently": "^5.3.0",
"nodemon": "^2.0.4", "nodemon": "^2.0.4",
"ts-node": "^9.0.0", "ts-node": "^9.0.0",
"typescript": "^4.0.2" "typescript": "^4.0.3"
}, },
"dependencies": { "dependencies": {
"@hibas123/utils": "^2.2.10" "@hibas123/utils": "^2.2.10"

View File

@ -39,6 +39,11 @@ export interface LoggingBaseOptions {
* Prints output to console * Prints output to console
*/ */
console: boolean; console: boolean;
/**
* Enables printing of calling file
*/
resolve_filename: boolean;
} }
const adapterCache = new WeakMap<Adapter, number>(); const adapterCache = new WeakMap<Adapter, number>();
@ -79,6 +84,8 @@ export class LoggingBase {
private messageObservable = new Observable<Message>(); private messageObservable = new Observable<Message>();
protected _name: string; protected _name: string;
private resolve_filename: boolean;
private _logLevel = LoggingTypes.Debug; private _logLevel = LoggingTypes.Debug;
get logLevel() { get logLevel() {
@ -108,11 +115,14 @@ export class LoggingBase {
let config: LoggingBaseOptions = { let config: LoggingBaseOptions = {
name: undefined, name: undefined,
console: true, console: true,
resolve_filename: true,
...opt, ...opt,
}; };
if (config.name) this._name = config.name; if (config.name) this._name = config.name;
this.resolve_filename = config.resolve_filename;
for (let key in this) { for (let key in this) {
if (typeof this[key] === "function") if (typeof this[key] === "function")
this[key] = (<any>this[key]).bind(this); this[key] = (<any>this[key]).bind(this);
@ -242,17 +252,23 @@ export class LoggingBase {
this.message(LoggingTypes.Warning, message); this.message(LoggingTypes.Warning, message);
} }
error(error: Error | string) { error(error: Error | string, ...message: any[]) {
if (this._logLevel > LoggingTypes.Error) return; if (this._logLevel > LoggingTypes.Error) return;
if (!error) if (!error)
error = "Empty ERROR was passed, so no informations available"; 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 is a fake error, to get a stack trace");
this.message(LoggingTypes.Error, [error, "\n", e.stack]); this.message(LoggingTypes.Error, [
error,
"\n",
e.stack,
"\n",
...message,
]);
} else { } else {
this.message( this.message(
LoggingTypes.Error, LoggingTypes.Error,
[error.message, "\n", error.stack], [error.message, "\n", error.stack, "\n", ...message],
getCallerFromExisting(error) getCallerFromExisting(error)
); );
} }
@ -323,35 +339,38 @@ export class LoggingBase {
let date = new Date().toISOString().replace(/T/, " ").replace(/\..+/, ""); let date = new Date().toISOString().replace(/T/, " ").replace(/\..+/, "");
let file_raw = caller; let file: string | undefined = undefined;
if (!file_raw) { if (this.resolve_filename) {
try { let file_raw = caller;
file_raw = getCallerFile(); if (!file_raw) {
} catch (err) { try {
file_raw = { file_raw = getCallerFile();
file: "<unknown>", } catch (err) {
line: 0, file_raw = {
column: 0, file: "<unknown>",
}; line: 0,
column: 0,
};
}
} }
if (
PROJECT_ROOT &&
file_raw.file &&
file_raw.file.startsWith(PROJECT_ROOT)
) {
let newF = file_raw.file.substring(PROJECT_ROOT.length);
if (newF.startsWith("/") || newF.startsWith("\\"))
newF = newF.substring(1);
file_raw.file = newF;
}
file = `${file_raw.file || "<unknown>"}:${file_raw.line}:${
file_raw.column || 0
}`;
} }
if (
PROJECT_ROOT &&
file_raw.file &&
file_raw.file.startsWith(PROJECT_ROOT)
) {
let newF = file_raw.file.substring(PROJECT_ROOT.length);
if (newF.startsWith("/") || newF.startsWith("\\"))
newF = newF.substring(1);
file_raw.file = newF;
}
let file = `${file_raw.file || "<unknown>"}:${file_raw.line}:${
file_raw.column || 0
}`;
let type_str = LoggingTypes[type].toUpperCase().padEnd(5, " "); let type_str = LoggingTypes[type].toUpperCase().padEnd(5, " ");
let type_format: Format[] = []; let type_format: Format[] = [];
switch (type) { switch (type) {
@ -381,7 +400,7 @@ export class LoggingBase {
a(date, this._formatMap.date); a(date, this._formatMap.date);
a("]["); a("][");
a(type_str, type_format); a(type_str, type_format);
if (file_raw.file) { if (file) {
a("]["); a("][");
a(file, this._formatMap.file); a(file, this._formatMap.file);
} }

View File

@ -83,3 +83,9 @@ Logging.log("MSG from root");
const timer = Logging.time("timer1", "Test Timer"); const timer = Logging.time("timer1", "Test Timer");
setTimeout(() => timer.end(), 1000); setTimeout(() => timer.end(), 1000);
const withoutFile = new LoggingBase({
resolve_filename: false,
});
withoutFile.log("This should not have a file attached");