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

View File

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

View File

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

View File

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