Adding nodes hrtimer to new time and timeEnd
This commit is contained in:
64
src/index.ts
64
src/index.ts
@ -1,23 +1,27 @@
|
||||
export { LoggingFiles } from "./filewriter";
|
||||
import { LoggingFiles } from "./filewriter";
|
||||
import { LoggingBase as LoggingBaseOriginal, LoggingBaseOptions } from "@hibas123/logging";
|
||||
|
||||
import {
|
||||
LoggingBase as LoggingBaseOriginal,
|
||||
LoggingBaseOptions,
|
||||
} from "@hibas123/logging";
|
||||
|
||||
export interface LoggingOptions extends LoggingBaseOptions {
|
||||
files: boolean | {
|
||||
/**
|
||||
* Filename/path of the logfile. Skip if generated with name.
|
||||
*
|
||||
* If not wanted pass null
|
||||
*/
|
||||
logfile?: string | null;
|
||||
/**
|
||||
* Filename/path of the logfile. Skip if generated with name.
|
||||
*
|
||||
* If not wanted pass null
|
||||
*/
|
||||
errorfile?: string | null;
|
||||
}
|
||||
files:
|
||||
| boolean
|
||||
| {
|
||||
/**
|
||||
* Filename/path of the logfile. Skip if generated with name.
|
||||
*
|
||||
* If not wanted pass null
|
||||
*/
|
||||
logfile?: string | null;
|
||||
/**
|
||||
* Filename/path of the logfile. Skip if generated with name.
|
||||
*
|
||||
* If not wanted pass null
|
||||
*/
|
||||
errorfile?: string | null;
|
||||
};
|
||||
}
|
||||
|
||||
export class LoggingBase extends LoggingBaseOriginal {
|
||||
@ -33,17 +37,30 @@ export class LoggingBase extends LoggingBaseOriginal {
|
||||
}
|
||||
|
||||
let name = this.name ? "." + this.name : "";
|
||||
if (!logfile && logfile !== null)
|
||||
logfile = `./logs/all${name}.log`;
|
||||
if (!logfile && logfile !== null) logfile = `./logs/all${name}.log`;
|
||||
if (!errorfile && errorfile !== null)
|
||||
errorfile = `./logs/error${name}.log`;
|
||||
|
||||
if (logfile)
|
||||
this.addAdapter(new LoggingFiles(logfile));
|
||||
if (logfile) this.addAdapter(new LoggingFiles(logfile));
|
||||
|
||||
if (errorfile) this.addAdapter(new LoggingFiles(errorfile, true));
|
||||
}
|
||||
}
|
||||
|
||||
if (errorfile)
|
||||
this.addAdapter(new LoggingFiles(errorfile, true));
|
||||
protected getCurrentTime() {
|
||||
if (process.hrtime.bigint) {
|
||||
return process.hrtime.bigint();
|
||||
} else {
|
||||
return process.hrtime();
|
||||
}
|
||||
}
|
||||
|
||||
protected getTimeDiff(start) {
|
||||
if (process.hrtime.bigint) {
|
||||
return Number((process.hrtime.bigint() - start) / BigInt(1000)) / 1000;
|
||||
} else {
|
||||
let diff = process.hrtime(start);
|
||||
return diff[0] * 1000 + diff[1] / 1000000;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -53,6 +70,3 @@ if (process.env.LOGGING_NO_DEFAULT !== "true") {
|
||||
Logging = new LoggingBase();
|
||||
}
|
||||
export default Logging;
|
||||
|
||||
|
||||
|
||||
|
69
src/test.ts
69
src/test.ts
@ -6,9 +6,11 @@ const deleteFolderRecursive = function (path: string) {
|
||||
if (fs.existsSync(path)) {
|
||||
fs.readdirSync(path).forEach(function (file, index) {
|
||||
var curPath = path + "/" + file;
|
||||
if (fs.lstatSync(curPath).isDirectory()) { // recurse
|
||||
if (fs.lstatSync(curPath).isDirectory()) {
|
||||
// recurse
|
||||
deleteFolderRecursive(curPath);
|
||||
} else { // delete file
|
||||
} else {
|
||||
// delete file
|
||||
fs.unlinkSync(curPath);
|
||||
}
|
||||
});
|
||||
@ -16,61 +18,70 @@ const deleteFolderRecursive = function (path: string) {
|
||||
}
|
||||
};
|
||||
|
||||
deleteFolderRecursive("./logs")
|
||||
deleteFolderRecursive("./logs");
|
||||
|
||||
Logging.log("test")
|
||||
Logging.log("test");
|
||||
Logging.log("i", "am", { a: "an" }, 1000);
|
||||
Logging.error(new Error("fehler 001"));
|
||||
Logging.debug("Some Debug infos");
|
||||
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")
|
||||
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)
|
||||
let err = new Error();
|
||||
if (typeof err.stack !== "string") console.log("Stacktrace invalid", err.stack);
|
||||
|
||||
let cus = new LoggingBase({ name: "test" });
|
||||
cus.log("Hello from custom Logger")
|
||||
cus.log("Hello from custom Logger");
|
||||
|
||||
let cus2 = new LoggingBase("test2");
|
||||
cus2.log("Hello from custom Logger 2")
|
||||
cus2.log("Hello from custom Logger 2");
|
||||
|
||||
let cus22 = new LoggingBase("test2");
|
||||
cus22.log("Hello from custom Logger 22")
|
||||
cus2.log("Hello from custom Logger 2")
|
||||
cus22.log("Hello from custom Logger 22")
|
||||
cus2.log("Hello from custom Logger 2")
|
||||
cus22.log("Hello from custom Logger 22")
|
||||
cus2.log("Hello from custom Logger 2")
|
||||
cus22.log("Hello from custom Logger 22")
|
||||
cus2.log("Hello from custom Logger 2")
|
||||
cus22.log("Hello from custom Logger 22")
|
||||
cus2.log("Hello from custom Logger 2")
|
||||
cus22.log("Hello from custom Logger 22");
|
||||
cus2.log("Hello from custom Logger 2");
|
||||
cus22.log("Hello from custom Logger 22");
|
||||
cus2.log("Hello from custom Logger 2");
|
||||
cus22.log("Hello from custom Logger 22");
|
||||
cus2.log("Hello from custom Logger 2");
|
||||
cus22.log("Hello from custom Logger 22");
|
||||
cus2.log("Hello from custom Logger 2");
|
||||
cus22.log("Hello from custom Logger 22");
|
||||
cus2.log("Hello from custom Logger 2");
|
||||
|
||||
const BenchmarkLogger = new LoggingBase({
|
||||
console: false,
|
||||
name: "bench"
|
||||
})
|
||||
name: "bench",
|
||||
});
|
||||
async function benchmark(count: number, message_size: number) {
|
||||
await BenchmarkLogger.waitForSetup();
|
||||
const randData = randomBytes(message_size).toString("hex")
|
||||
const randData = randomBytes(message_size).toString("hex");
|
||||
const t = process.hrtime();
|
||||
for (let i = 0; i < count; i++) {
|
||||
BenchmarkLogger.log(randData)
|
||||
BenchmarkLogger.log(randData);
|
||||
}
|
||||
const diff = process.hrtime(t);
|
||||
const NS_PER_SEC = 1e9;
|
||||
await BenchmarkLogger.waitForSetup();
|
||||
const ns = diff[0] * NS_PER_SEC + diff[1];
|
||||
console.log(`Benchmark took ${ns / 1000000}ms for ${count} messages with a size of ${message_size} characters`);
|
||||
console.log(`This is equal to ${(ns / 1000000) / count} ms per message`)
|
||||
console.log(
|
||||
`Benchmark took ${
|
||||
ns / 1000000
|
||||
}ms for ${count} messages with a size of ${message_size} characters`
|
||||
);
|
||||
console.log(`This is equal to ${ns / 1000000 / count} ms per message`);
|
||||
}
|
||||
|
||||
Logging.waitForSetup().then(async () => {
|
||||
return;
|
||||
console.log("Large data benchmark:")
|
||||
console.log("Large data benchmark:");
|
||||
await benchmark(7000, 50000);
|
||||
|
||||
console.log("Realdata data benchmark:")
|
||||
await benchmark(100000, 100)
|
||||
});
|
||||
console.log("Realdata data benchmark:");
|
||||
await benchmark(100000, 100);
|
||||
});
|
||||
|
||||
const timer = Logging.time("timer1", "Test Timer");
|
||||
setTimeout(() => timer.end(), 1000);
|
||||
|
Reference in New Issue
Block a user