1
0
mirror of https://git.stamm.me/OpenServer/NodeLogging.git synced 2024-11-21 21:39:23 +00:00
This commit is contained in:
Fabian Stamm 2021-05-08 22:11:15 +02:00
parent f01a4ffb21
commit 249f701cb7
5 changed files with 2529 additions and 362 deletions

2752
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
{ {
"name": "@hibas123/nodelogging", "name": "@hibas123/nodelogging",
"version": "2.4.4", "version": "3.0.0",
"description": "", "description": "",
"main": "out/index.js", "main": "out/index.js",
"types": "out/index.d.ts", "types": "out/index.d.ts",
@ -10,7 +10,7 @@
"watch-ts": "tsc --watch", "watch-ts": "tsc --watch",
"watch-js": "nodemon out/test.js", "watch-js": "nodemon out/test.js",
"watch": "concurrently npm:watch-*", "watch": "concurrently npm:watch-*",
"test": "node out/test.js", "test": "npm run build && node out/test.js",
"live": "nodemon out/test.js" "live": "nodemon out/test.js"
}, },
"repository": { "repository": {
@ -26,13 +26,13 @@
"readme.md" "readme.md"
], ],
"devDependencies": { "devDependencies": {
"@types/node": "^13.13.4", "@types/node": "^15.0.2",
"concurrently": "^5.2.0", "concurrently": "^6.0.2",
"nodemon": "^2.0.3", "nodemon": "^2.0.7",
"typescript": "^3.8.3" "typescript": "^4.2.4"
}, },
"dependencies": { "dependencies": {
"@hibas123/logging": "^2.4.4", "@hibas123/logging": "^3.0.0",
"@hibas123/utils": "^2.2.4" "@hibas123/utils": "^2.2.18"
} }
} }

View File

@ -1,7 +1,7 @@
import { Lock, ObservableInterface } from "@hibas123/utils"; import { Lock } from "@hibas123/utils";
import * as fs from "fs"; import * as fs from "fs";
import * as path from "path"; import * as path from "path";
import { Adapter, Message, LoggingTypes } from "@hibas123/logging"; import { Adapter, Message, Formatted } from "@hibas123/logging";
const MAX_FILE_SIZE = 500000000; const MAX_FILE_SIZE = 500000000;
@ -14,11 +14,10 @@ export class LoggingFiles implements Adapter {
private noPrefix = false private noPrefix = false
) {} ) {}
init(observable: ObservableInterface<Message>) { init() {
observable.subscribe(this.onMessage.bind(this));
if (!this.file) { if (!this.file) {
this.file = Files.getFile(this.filename); this.file = Files.getFile(this.filename);
return this.file.init(this.maxFileSize); this.file.init(this.maxFileSize);
} }
} }
@ -27,16 +26,7 @@ export class LoggingFiles implements Adapter {
} }
onMessage(message: Message) { onMessage(message: Message) {
// Just ignore all non error messages, if this.error is set let msg = Buffer.from(Formatted.strip(message.text) + "\n");
if (this.error && message.type !== LoggingTypes.Error) return;
let prefix = "";
if (message.name) prefix = `[${message.name}]=>`;
let txt = message.text.formatted
.map((fmt) => prefix + fmt.map((f) => f.text).join("") + "\n")
.join("");
let msg = Buffer.from(txt);
this.file.write(msg); this.file.write(msg);
} }
@ -46,6 +36,8 @@ export class LoggingFiles implements Adapter {
} }
} }
//TODO: Optimise write path
export class Files { export class Files {
private open = 0; private open = 0;
@ -180,7 +172,7 @@ export class Files {
} }
function fsUnlink(path) { function fsUnlink(path) {
return new Promise((resolve, reject) => { return new Promise<void>((resolve, reject) => {
fs.unlink(path, (err) => { fs.unlink(path, (err) => {
if (err) reject(err); if (err) reject(err);
else resolve(); else resolve();
@ -198,7 +190,7 @@ function fsStat(path: string) {
} }
function fsMove(oldPath: string, newPath: string) { function fsMove(oldPath: string, newPath: string) {
return new Promise((resolve, reject) => { return new Promise<void>((resolve, reject) => {
let callback = (err?) => { let callback = (err?) => {
if (err) reject(err); if (err) reject(err);
else resolve(); else resolve();
@ -232,7 +224,7 @@ function fsExists(path: string) {
} }
function fsMkDir(path: string) { function fsMkDir(path: string) {
return new Promise((resolve, reject) => { return new Promise<void>((resolve, reject) => {
fs.mkdir(path, (err) => (err ? reject(err) : resolve())); fs.mkdir(path, (err) => (err ? reject(err) : resolve()));
}); });
} }

View File

@ -1,78 +1,28 @@
export { LoggingFiles } from "./filewriter"; export { LoggingFiles } from "./filewriter";
import { LoggingFiles } from "./filewriter"; import { LoggingFiles } from "./filewriter";
import { import { Logging as LoggingBase } from "@hibas123/logging";
LoggingBase as LoggingBaseOriginal, import Logging from "@hibas123/logging";
LoggingBaseOptions,
} from "@hibas123/logging";
export interface LoggingOptions extends LoggingBaseOptions { LoggingBase.nativeFunctions = {
files: startTimer: () => {
| 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 {
constructor(config: Partial<LoggingOptions> | string = {}) {
super(config);
if (typeof config === "string" || config.files !== false) {
let logfile: string;
let errorfile: string;
if (typeof config !== "string" && typeof config.files === "object") {
logfile = config.files.logfile;
errorfile = config.files.errorfile;
}
let name = this.name ? "." + this.name : "";
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 (errorfile) this.addAdapter(new LoggingFiles(errorfile, true));
}
}
protected postGetChild(child: LoggingBase) {
child.getCurrentTime = this.getCurrentTime.bind(child);
child.getTimeDiff = this.getTimeDiff.bind(child);
child.postGetChild = this.postGetChild.bind(child);
}
protected getCurrentTime() {
if (process.hrtime.bigint) { if (process.hrtime.bigint) {
return process.hrtime.bigint(); return process.hrtime.bigint();
} else { } else {
return process.hrtime(); return process.hrtime();
} }
} },
endTimer: (start) => {
protected getTimeDiff(start) {
if (process.hrtime.bigint) { if (process.hrtime.bigint) {
return Number((process.hrtime.bigint() - start) / BigInt(1000)) / 1000; return Number((process.hrtime.bigint() - start) / BigInt(1000)) / 1000;
} else { } else {
let diff = process.hrtime(start); let diff = process.hrtime(start);
return diff[0] * 1000 + diff[1] / 1000000; return diff[0] * 1000 + diff[1] / 1000000;
} }
} },
} };
export const DefaultLoggingFile = new LoggingFiles("./logs/all.log");
Logging.addAdapter(DefaultLoggingFile);
export let Logging: LoggingBase = undefined;
if (process.env.LOGGING_NO_DEFAULT !== "true") {
Logging = new LoggingBase();
}
export default Logging; export default Logging;

View File

@ -1,6 +1,7 @@
import { randomBytes } from "crypto"; import { randomBytes } from "crypto";
import * as fs from "fs"; import * as fs from "fs";
import { Logging, LoggingBase } from "."; import { Logging as LoggingBase } from "@hibas123/logging";
import Logging, { DefaultLoggingFile } from ".";
const deleteFolderRecursive = function (path: string) { const deleteFolderRecursive = function (path: string) {
if (fs.existsSync(path)) { if (fs.existsSync(path)) {
@ -24,7 +25,7 @@ Logging.log("test");
Logging.log("i", "am", { a: "an" }, 1000); Logging.log("i", "am", { a: "an" }, 1000);
Logging.error(new Error("fehler 001")); Logging.error(new Error("fehler 001"));
Logging.debug("Some Debug infos"); Logging.debug("Some Debug infos");
Logging.errorMessage("i", "am", "an", "error"); Logging.error("i", "am", "an", "error");
Logging.log( Logging.log(
"\x1b[31m\x1b[31m\x1b[31m\x1b[31m\x1b[31m\x1b[31m TEST \x1b[31m\x1b[31m\x1b[31m" "\x1b[31m\x1b[31m\x1b[31m\x1b[31m\x1b[31m\x1b[31m TEST \x1b[31m\x1b[31m\x1b[31m"
@ -36,10 +37,10 @@ if (typeof err.stack !== "string") console.log("Stacktrace invalid", err.stack);
let cus = new LoggingBase({ name: "test" }); let cus = new LoggingBase({ name: "test" });
cus.log("Hello from custom Logger"); cus.log("Hello from custom Logger");
let cus2 = new LoggingBase("test2"); let cus2 = Logging.getChild("test2");
cus2.log("Hello from custom Logger 2"); cus2.log("Hello from custom Logger 2");
let cus22 = new LoggingBase("test2"); let cus22 = Logging.getChild("test2");
cus22.log("Hello from custom Logger 22"); cus22.log("Hello from custom Logger 22");
cus2.log("Hello from custom Logger 2"); cus2.log("Hello from custom Logger 2");
cus22.log("Hello from custom Logger 22"); cus22.log("Hello from custom Logger 22");
@ -55,8 +56,10 @@ const BenchmarkLogger = new LoggingBase({
console: false, console: false,
name: "bench", name: "bench",
}); });
BenchmarkLogger.addAdapter(DefaultLoggingFile);
async function benchmark(count: number, message_size: number) { 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(); const t = process.hrtime();
for (let i = 0; i < count; i++) { for (let i = 0; i < count; i++) {
@ -64,7 +67,6 @@ async function benchmark(count: number, message_size: number) {
} }
const diff = process.hrtime(t); const diff = process.hrtime(t);
const NS_PER_SEC = 1e9; const NS_PER_SEC = 1e9;
await BenchmarkLogger.waitForSetup();
const ns = diff[0] * NS_PER_SEC + diff[1]; const ns = diff[0] * NS_PER_SEC + diff[1];
console.log( console.log(
`Benchmark took ${ `Benchmark took ${
@ -74,14 +76,15 @@ async function benchmark(count: number, message_size: number) {
console.log(`This is equal to ${ns / 1000000 / count} ms per message`); console.log(`This is equal to ${ns / 1000000 / count} ms per message`);
} }
Logging.waitForSetup().then(async () => { const benchTimer = Logging.time("benchmark");
return; Promise.resolve().then(async () => {
console.log("Large data benchmark:"); console.log("Large data benchmark:");
await benchmark(7000, 50000); await benchmark(7000, 50000);
console.log("Realdata data benchmark:"); console.log("Realdata data benchmark:");
await benchmark(100000, 100); await benchmark(100000, 100);
benchTimer.end();
}); });
const timer = Logging.time("timer1", "Test Timer"); const timer = Logging.time("Test Timer");
setTimeout(() => timer.end(), 1000); setTimeout(() => timer.end(), 1000);