Extracting file from error object
This commit is contained in:
parent
29fee51b63
commit
6bf59ede32
2
out/index.d.ts
vendored
2
out/index.d.ts
vendored
@ -24,7 +24,7 @@ export declare class LoggingBase {
|
|||||||
logWithCustomColors(type: LoggingTypes, colors: string, ...message: any[]): void;
|
logWithCustomColors(type: LoggingTypes, colors: string, ...message: any[]): void;
|
||||||
error(error: Error | string): void;
|
error(error: Error | string): void;
|
||||||
errorMessage(...message: any[]): void;
|
errorMessage(...message: any[]): void;
|
||||||
private message(type, message, customColors?);
|
private message(type, message, customColors?, caller?);
|
||||||
private writeMessageToFile(message, error?);
|
private writeMessageToFile(message, error?);
|
||||||
private checkQueue();
|
private checkQueue();
|
||||||
private writeToLogFile(data);
|
private writeToLogFile(data);
|
||||||
|
23
out/index.js
23
out/index.js
@ -88,13 +88,13 @@ class LoggingBase {
|
|||||||
this.message(LoggingTypes.Error, [error, ":", e.stack]);
|
this.message(LoggingTypes.Error, [error, ":", e.stack]);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
this.message(LoggingTypes.Error, [error.name, ":", error.message, ":", error.stack]);
|
this.message(LoggingTypes.Error, [error.name, ":", error.message, ":", error.stack], undefined, getCallerFromExisting(error));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
errorMessage(...message) {
|
errorMessage(...message) {
|
||||||
this.message(LoggingTypes.Error, message);
|
this.message(LoggingTypes.Error, message);
|
||||||
}
|
}
|
||||||
async message(type, message, customColors) {
|
async message(type, message, customColors, caller) {
|
||||||
var consoleLogFormat = Reset;
|
var consoleLogFormat = Reset;
|
||||||
if (!customColors) {
|
if (!customColors) {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
@ -131,7 +131,7 @@ class LoggingBase {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
let file = getCallerFile();
|
let file = caller || getCallerFile();
|
||||||
let date = new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '');
|
let date = new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '');
|
||||||
let prefix = `[${LoggingTypes[type]}][${file.file}:${file.line}][${date}]: `;
|
let prefix = `[${LoggingTypes[type]}][${file.file}:${file.line}][${date}]: `;
|
||||||
let message_lines = mb.split("\n").map(line => prefix + line);
|
let message_lines = mb.split("\n").map(line => prefix + line);
|
||||||
@ -345,6 +345,23 @@ function getCallerFile() {
|
|||||||
catch (err) { }
|
catch (err) { }
|
||||||
return { file: undefined, line: 0 };
|
return { file: undefined, line: 0 };
|
||||||
}
|
}
|
||||||
|
function getCallerFromExisting(err) {
|
||||||
|
let lines = err.stack.split("\n");
|
||||||
|
let current = path.basename(__filename);
|
||||||
|
lines.shift(); // removing first line
|
||||||
|
while (lines.length > 0) {
|
||||||
|
let line = lines.shift();
|
||||||
|
let matches = line.match(/[a-zA-Z]+[.][a-zA-Z]+[:][0-9]+/g);
|
||||||
|
if (matches && matches.length > 0) {
|
||||||
|
let [f, line] = matches[0].split(":");
|
||||||
|
if (f != current) {
|
||||||
|
return {
|
||||||
|
file: f, line: Number(line)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
var LoggingTypes;
|
var LoggingTypes;
|
||||||
(function (LoggingTypes) {
|
(function (LoggingTypes) {
|
||||||
LoggingTypes[LoggingTypes["Log"] = 0] = "Log";
|
LoggingTypes[LoggingTypes["Log"] = 0] = "Log";
|
||||||
|
File diff suppressed because one or more lines are too long
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@hibas123/nodelogging",
|
"name": "@hibas123/nodelogging",
|
||||||
"version": "1.2.2",
|
"version": "1.3.2",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "out/index.js",
|
"main": "out/index.js",
|
||||||
"types": "out/index.d.ts",
|
"types": "out/index.d.ts",
|
||||||
|
24
src/index.ts
24
src/index.ts
@ -113,7 +113,7 @@ export class LoggingBase {
|
|||||||
let e = new Error()
|
let e = new Error()
|
||||||
this.message(LoggingTypes.Error, [error, ":", e.stack]);
|
this.message(LoggingTypes.Error, [error, ":", e.stack]);
|
||||||
} else {
|
} else {
|
||||||
this.message(LoggingTypes.Error, [error.name, ":", error.message, ":", error.stack]);
|
this.message(LoggingTypes.Error, [error.name, ":", error.message, ":", error.stack], undefined, getCallerFromExisting(error));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -121,7 +121,7 @@ export class LoggingBase {
|
|||||||
this.message(LoggingTypes.Error, message);
|
this.message(LoggingTypes.Error, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async message(type: LoggingTypes, message: any[] | string, customColors?: string) {
|
private async message(type: LoggingTypes, message: any[] | string, customColors?: string, caller?: { file: string, line: number }) {
|
||||||
var consoleLogFormat = Reset;
|
var consoleLogFormat = Reset;
|
||||||
if (!customColors) {
|
if (!customColors) {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
@ -154,7 +154,7 @@ export class LoggingBase {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
let file = getCallerFile()
|
let file = caller || getCallerFile();
|
||||||
let date = new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '');
|
let date = new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '');
|
||||||
let prefix = `[${LoggingTypes[type]}][${file.file}:${file.line}][${date}]: `;
|
let prefix = `[${LoggingTypes[type]}][${file.file}:${file.line}][${date}]: `;
|
||||||
let message_lines = mb.split("\n").map(line => prefix + line);
|
let message_lines = mb.split("\n").map(line => prefix + line);
|
||||||
@ -381,6 +381,24 @@ function getCallerFile() {
|
|||||||
return { file: undefined, line: 0 };
|
return { file: undefined, line: 0 };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getCallerFromExisting(err: Error): { file: string, line: number } {
|
||||||
|
let lines = err.stack.split("\n");
|
||||||
|
let current = path.basename(__filename);
|
||||||
|
lines.shift();// removing first line
|
||||||
|
while (lines.length > 0) {
|
||||||
|
let line = lines.shift();
|
||||||
|
let matches = line.match(/[a-zA-Z]+[.][a-zA-Z]+[:][0-9]+/g)
|
||||||
|
if (matches && matches.length > 0) {
|
||||||
|
let [f, line] = matches[0].split(":")
|
||||||
|
if (f != current) {
|
||||||
|
return {
|
||||||
|
file: f, line: Number(line)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export enum LoggingTypes {
|
export enum LoggingTypes {
|
||||||
Log,
|
Log,
|
||||||
Warning,
|
Warning,
|
||||||
|
Loading…
Reference in New Issue
Block a user