1
0
mirror of https://git.stamm.me/OpenServer/NodeLogging.git synced 2024-12-23 04:39:24 +00:00

making shure to not destroy Error stack permanently

This commit is contained in:
Fabian Stamm 2018-05-10 20:11:53 +02:00
parent 4018b380ea
commit 11bc947205
8 changed files with 72 additions and 28 deletions

View File

@ -28,6 +28,7 @@ const BgMagenta = "\x1b[45m";
const BgCyan = "\x1b[46m";
const BgWhite = "\x1b[47m";
const maxFileSize = 500000000;
const OriginalErrorStackFunction = Error.prototype.prepareStackTrace;
class Logging {
static config(logfolder, stdout) {
this.logFileLocation = logfolder;
@ -50,11 +51,11 @@ class Logging {
Logging.errorMessage(error);
return;
}
let m = "";
error.stack.forEach(e => {
m += e.toString() + "\n";
});
var message = error.name + " " + error.message + "\n" + m;
// let m = "";
// (<any>error.stack).forEach(e => {
// m += e.toString() + "\n";
// })
var message = error.name + " " + error.message + "\n" + error.stack;
Logging.message(LoggingTypes.Error, [message]);
}
static errorMessage(...message) {
@ -284,15 +285,29 @@ function fsMkDir(path) {
fs.exists(path, resolve);
});
}
function getStack() {
// Save original Error.prepareStackTrace
let origPrepareStackTrace = Error.prepareStackTrace;
// Override with function that just returns `stack`
Error.prepareStackTrace = function (_, stack) {
return stack;
};
// Create a new `Error`, which automatically gets `stack`
let err = new Error();
// Evaluate `err.stack`, which calls our new `Error.prepareStackTrace`
let stack = err.stack;
// Restore original `Error.prepareStackTrace`
Error.prepareStackTrace = origPrepareStackTrace;
// Remove superfluous function call on stack
stack.shift(); // getStack --> Error
return stack;
}
function _getCallerFile() {
try {
var err = new Error();
var caller_file;
var current_file;
Error.prepareStackTrace = function (err, stack) { return stack; };
current_file = err.stack.shift().getFileName();
while (err.stack.length) {
caller_file = err.stack.shift().getFileName();
let stack = getStack();
let current_file = stack.shift().getFileName();
while (stack.length) {
let caller_file = stack.shift().getFileName();
if (current_file !== caller_file)
return path.basename(caller_file);
}

File diff suppressed because one or more lines are too long

1
out/test.d.ts vendored
View File

@ -0,0 +1 @@
export {};

View File

@ -8,6 +8,9 @@ index_1.Logging.error(new Error("fehler 001"));
index_1.Logging.debug("Some Debug infos");
index_1.Logging.errorMessage("i", "am", "an", "error");
index_1.Logging.log("\x1b[31m\x1b[31m\x1b[31m\x1b[31m\x1b[31m\x1b[31m TEST \x1b[31m\x1b[31m\x1b[31m");
let err = new Error();
if (typeof err.stack !== "string")
console.log("Stacktrace invalid", err.stack);
index_1.Logging.stdout = false;
for (let i = 0; i < 7000; i++) {
index_1.Logging.log(crypto_1.randomBytes(50000).toString("hex"));

View File

@ -1 +1 @@
{"version":3,"file":"test.js","sourceRoot":"","sources":["../src/test.ts"],"names":[],"mappings":";;AAAA,mCAAkC;AAClC,mCAAqC;AAErC,eAAO,CAAC,GAAG,CAAC,MAAM,CAAC,CAAA;AACnB,eAAO,CAAC,GAAG,CAAC,GAAG,EAAE,IAAI,EAAE,EAAE,CAAC,EAAE,IAAI,EAAE,EAAE,IAAI,CAAC,CAAC;AAC1C,eAAO,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,YAAY,CAAC,CAAC,CAAC;AACvC,eAAO,CAAC,KAAK,CAAC,kBAAkB,CAAC,CAAC;AAClC,eAAO,CAAC,YAAY,CAAC,GAAG,EAAE,IAAI,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC;AAE/C,eAAO,CAAC,GAAG,CAAC,gFAAgF,CAAC,CAAA;AAE7F,eAAO,CAAC,MAAM,GAAG,KAAK,CAAC;AACvB,GAAG,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,EAAE,CAAC,EAAE,EAAE,CAAC;IAC7B,eAAO,CAAC,GAAG,CAAC,oBAAW,CAAC,KAAK,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAA;AAClD,CAAC"}
{"version":3,"file":"test.js","sourceRoot":"","sources":["../src/test.ts"],"names":[],"mappings":";;AAAA,mCAAkC;AAClC,mCAAqC;AAErC,eAAO,CAAC,GAAG,CAAC,MAAM,CAAC,CAAA;AACnB,eAAO,CAAC,GAAG,CAAC,GAAG,EAAE,IAAI,EAAE,EAAE,CAAC,EAAE,IAAI,EAAE,EAAE,IAAI,CAAC,CAAC;AAC1C,eAAO,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,YAAY,CAAC,CAAC,CAAC;AACvC,eAAO,CAAC,KAAK,CAAC,kBAAkB,CAAC,CAAC;AAClC,eAAO,CAAC,YAAY,CAAC,GAAG,EAAE,IAAI,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC;AAE/C,eAAO,CAAC,GAAG,CAAC,gFAAgF,CAAC,CAAA;AAE7F,IAAI,GAAG,GAAG,IAAI,KAAK,EAAE,CAAA;AACrB,IAAI,OAAO,GAAG,CAAC,KAAK,KAAK,QAAQ;IAAE,OAAO,CAAC,GAAG,CAAC,oBAAoB,EAAE,GAAG,CAAC,KAAK,CAAC,CAAA;AAE/E,eAAO,CAAC,MAAM,GAAG,KAAK,CAAC;AACvB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,EAAE,CAAC,EAAE,EAAE;IAC5B,eAAO,CAAC,GAAG,CAAC,oBAAW,CAAC,KAAK,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAA;CACjD"}

View File

@ -1,6 +1,6 @@
{
"name": "logger-perfcloud",
"version": "1.1.3",
"version": "1.2.0",
"description": "",
"main": "out/index.js",
"types": "out/index.d.ts",

View File

@ -31,6 +31,8 @@ const BgWhite = "\x1b[47m"
const maxFileSize = 500000000;
const OriginalErrorStackFunction = (<any>Error.prototype).prepareStackTrace
export class Logging {
private static logFileLocation: string = "./logs/";
public static stdout: boolean = true;
@ -71,11 +73,11 @@ export class Logging {
Logging.errorMessage(error);
return;
}
let m = "";
(<any>error.stack).forEach(e => {
m += e.toString() + "\n";
})
var message = error.name + " " + error.message + "\n" + m;
// let m = "";
// (<any>error.stack).forEach(e => {
// m += e.toString() + "\n";
// })
var message = error.name + " " + error.message + "\n" + error.stack;
Logging.message(LoggingTypes.Error, [message]);
}
@ -298,18 +300,38 @@ function fsMkDir(path) {
});
}
function getStack() {
// Save original Error.prepareStackTrace
let origPrepareStackTrace = (<any>Error).prepareStackTrace;
// Override with function that just returns `stack`
(<any>Error).prepareStackTrace = function (_, stack) {
return stack
}
// Create a new `Error`, which automatically gets `stack`
let err = new Error();
// Evaluate `err.stack`, which calls our new `Error.prepareStackTrace`
let stack: any[] = <any>err.stack;
// Restore original `Error.prepareStackTrace`
(<any>Error).prepareStackTrace = origPrepareStackTrace;
// Remove superfluous function call on stack
stack.shift(); // getStack --> Error
return stack
}
function _getCallerFile() {
try {
var err = new Error();
var caller_file: string;
var current_file: string;
let stack = getStack()
(<any>Error).prepareStackTrace = function (err, stack) { return stack; };
let current_file = stack.shift().getFileName();
current_file = (<any>err.stack).shift().getFileName();
while (err.stack.length) {
caller_file = (<any>err.stack).shift().getFileName();
while (stack.length) {
let caller_file = stack.shift().getFileName();
if (current_file !== caller_file) return path.basename(caller_file);
}
} catch (err) { }

View File

@ -9,6 +9,9 @@ Logging.errorMessage("i", "am", "an", "error");
Logging.log("\x1b[31m\x1b[31m\x1b[31m\x1b[31m\x1b[31m\x1b[31m TEST \x1b[31m\x1b[31m\x1b[31m")
let err = new Error()
if (typeof err.stack !== "string") console.log("Stacktrace invalid", err.stack)
Logging.stdout = false;
for (let i = 0; i < 7000; i++) {
Logging.log(randomBytes(50000).toString("hex"))