1
0
mirror of https://git.stamm.me/OpenServer/NodeLogging.git synced 2024-11-25 02:29:23 +00:00

Extracting file from error object

This commit is contained in:
Fabian Stamm 2018-08-19 16:39:04 +02:00
parent 29fee51b63
commit 6bf59ede32
5 changed files with 44 additions and 9 deletions

2
out/index.d.ts vendored
View File

@ -24,7 +24,7 @@ export declare class LoggingBase {
logWithCustomColors(type: LoggingTypes, colors: string, ...message: any[]): void;
error(error: Error | string): void;
errorMessage(...message: any[]): void;
private message(type, message, customColors?);
private message(type, message, customColors?, caller?);
private writeMessageToFile(message, error?);
private checkQueue();
private writeToLogFile(data);

View File

@ -88,13 +88,13 @@ class LoggingBase {
this.message(LoggingTypes.Error, [error, ":", e.stack]);
}
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) {
this.message(LoggingTypes.Error, message);
}
async message(type, message, customColors) {
async message(type, message, customColors, caller) {
var consoleLogFormat = Reset;
if (!customColors) {
switch (type) {
@ -131,7 +131,7 @@ class LoggingBase {
}
});
}
let file = getCallerFile();
let file = caller || getCallerFile();
let date = new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '');
let prefix = `[${LoggingTypes[type]}][${file.file}:${file.line}][${date}]: `;
let message_lines = mb.split("\n").map(line => prefix + line);
@ -345,6 +345,23 @@ function getCallerFile() {
catch (err) { }
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;
(function (LoggingTypes) {
LoggingTypes[LoggingTypes["Log"] = 0] = "Log";

File diff suppressed because one or more lines are too long

View File

@ -1,6 +1,6 @@
{
"name": "@hibas123/nodelogging",
"version": "1.2.2",
"version": "1.3.2",
"description": "",
"main": "out/index.js",
"types": "out/index.d.ts",

View File

@ -113,7 +113,7 @@ export class LoggingBase {
let e = new Error()
this.message(LoggingTypes.Error, [error, ":", e.stack]);
} 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);
}
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;
if (!customColors) {
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 prefix = `[${LoggingTypes[type]}][${file.file}:${file.line}][${date}]: `;
let message_lines = mb.split("\n").map(line => prefix + line);
@ -381,6 +381,24 @@ function getCallerFile() {
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 {
Log,
Warning,