mirror of
				https://git.hibas.dev/OpenServer/NodeLogging.git
				synced 2025-11-03 22:40:45 +00:00 
			
		
		
		
	V3
This commit is contained in:
		@ -1,7 +1,7 @@
 | 
			
		||||
import { Lock, ObservableInterface } from "@hibas123/utils";
 | 
			
		||||
import { Lock } from "@hibas123/utils";
 | 
			
		||||
import * as fs from "fs";
 | 
			
		||||
import * as path from "path";
 | 
			
		||||
import { Adapter, Message, LoggingTypes } from "@hibas123/logging";
 | 
			
		||||
import { Adapter, Message, Formatted } from "@hibas123/logging";
 | 
			
		||||
 | 
			
		||||
const MAX_FILE_SIZE = 500000000;
 | 
			
		||||
 | 
			
		||||
@ -14,11 +14,10 @@ export class LoggingFiles implements Adapter {
 | 
			
		||||
      private noPrefix = false
 | 
			
		||||
   ) {}
 | 
			
		||||
 | 
			
		||||
   init(observable: ObservableInterface<Message>) {
 | 
			
		||||
      observable.subscribe(this.onMessage.bind(this));
 | 
			
		||||
   init() {
 | 
			
		||||
      if (!this.file) {
 | 
			
		||||
         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) {
 | 
			
		||||
      // Just ignore all non error messages, if this.error is set
 | 
			
		||||
      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);
 | 
			
		||||
      let msg = Buffer.from(Formatted.strip(message.text) + "\n");
 | 
			
		||||
      this.file.write(msg);
 | 
			
		||||
   }
 | 
			
		||||
 | 
			
		||||
@ -46,6 +36,8 @@ export class LoggingFiles implements Adapter {
 | 
			
		||||
   }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
//TODO: Optimise write path
 | 
			
		||||
 | 
			
		||||
export class Files {
 | 
			
		||||
   private open = 0;
 | 
			
		||||
 | 
			
		||||
@ -180,7 +172,7 @@ export class Files {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function fsUnlink(path) {
 | 
			
		||||
   return new Promise((resolve, reject) => {
 | 
			
		||||
   return new Promise<void>((resolve, reject) => {
 | 
			
		||||
      fs.unlink(path, (err) => {
 | 
			
		||||
         if (err) reject(err);
 | 
			
		||||
         else resolve();
 | 
			
		||||
@ -198,7 +190,7 @@ function fsStat(path: string) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function fsMove(oldPath: string, newPath: string) {
 | 
			
		||||
   return new Promise((resolve, reject) => {
 | 
			
		||||
   return new Promise<void>((resolve, reject) => {
 | 
			
		||||
      let callback = (err?) => {
 | 
			
		||||
         if (err) reject(err);
 | 
			
		||||
         else resolve();
 | 
			
		||||
@ -232,7 +224,7 @@ function fsExists(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()));
 | 
			
		||||
   });
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										74
									
								
								src/index.ts
									
									
									
									
									
								
							
							
						
						
									
										74
									
								
								src/index.ts
									
									
									
									
									
								
							@ -1,78 +1,28 @@
 | 
			
		||||
export { LoggingFiles } from "./filewriter";
 | 
			
		||||
import { LoggingFiles } from "./filewriter";
 | 
			
		||||
import {
 | 
			
		||||
   LoggingBase as LoggingBaseOriginal,
 | 
			
		||||
   LoggingBaseOptions,
 | 
			
		||||
} from "@hibas123/logging";
 | 
			
		||||
import { Logging as LoggingBase } from "@hibas123/logging";
 | 
			
		||||
import Logging 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;
 | 
			
		||||
        };
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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() {
 | 
			
		||||
LoggingBase.nativeFunctions = {
 | 
			
		||||
   startTimer: () => {
 | 
			
		||||
      if (process.hrtime.bigint) {
 | 
			
		||||
         return process.hrtime.bigint();
 | 
			
		||||
      } else {
 | 
			
		||||
         return process.hrtime();
 | 
			
		||||
      }
 | 
			
		||||
   }
 | 
			
		||||
 | 
			
		||||
   protected getTimeDiff(start) {
 | 
			
		||||
   },
 | 
			
		||||
   endTimer: (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;
 | 
			
		||||
      }
 | 
			
		||||
   }
 | 
			
		||||
}
 | 
			
		||||
   },
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
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;
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										21
									
								
								src/test.ts
									
									
									
									
									
								
							
							
						
						
									
										21
									
								
								src/test.ts
									
									
									
									
									
								
							@ -1,6 +1,7 @@
 | 
			
		||||
import { randomBytes } from "crypto";
 | 
			
		||||
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) {
 | 
			
		||||
   if (fs.existsSync(path)) {
 | 
			
		||||
@ -24,7 +25,7 @@ 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.error("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"
 | 
			
		||||
@ -36,10 +37,10 @@ if (typeof err.stack !== "string") console.log("Stacktrace invalid", err.stack);
 | 
			
		||||
let cus = new LoggingBase({ name: "test" });
 | 
			
		||||
cus.log("Hello from custom Logger");
 | 
			
		||||
 | 
			
		||||
let cus2 = new LoggingBase("test2");
 | 
			
		||||
let cus2 = Logging.getChild("test2");
 | 
			
		||||
cus2.log("Hello from custom Logger 2");
 | 
			
		||||
 | 
			
		||||
let cus22 = new LoggingBase("test2");
 | 
			
		||||
let cus22 = Logging.getChild("test2");
 | 
			
		||||
cus22.log("Hello from custom Logger 22");
 | 
			
		||||
cus2.log("Hello from custom Logger 2");
 | 
			
		||||
cus22.log("Hello from custom Logger 22");
 | 
			
		||||
@ -55,8 +56,10 @@ const BenchmarkLogger = new LoggingBase({
 | 
			
		||||
   console: false,
 | 
			
		||||
   name: "bench",
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
BenchmarkLogger.addAdapter(DefaultLoggingFile);
 | 
			
		||||
 | 
			
		||||
async function benchmark(count: number, message_size: number) {
 | 
			
		||||
   await BenchmarkLogger.waitForSetup();
 | 
			
		||||
   const randData = randomBytes(message_size).toString("hex");
 | 
			
		||||
   const t = process.hrtime();
 | 
			
		||||
   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 NS_PER_SEC = 1e9;
 | 
			
		||||
   await BenchmarkLogger.waitForSetup();
 | 
			
		||||
   const ns = diff[0] * NS_PER_SEC + diff[1];
 | 
			
		||||
   console.log(
 | 
			
		||||
      `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`);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
Logging.waitForSetup().then(async () => {
 | 
			
		||||
   return;
 | 
			
		||||
const benchTimer = Logging.time("benchmark");
 | 
			
		||||
Promise.resolve().then(async () => {
 | 
			
		||||
   console.log("Large data benchmark:");
 | 
			
		||||
   await benchmark(7000, 50000);
 | 
			
		||||
 | 
			
		||||
   console.log("Realdata data benchmark:");
 | 
			
		||||
   await benchmark(100000, 100);
 | 
			
		||||
   benchTimer.end();
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
const timer = Logging.time("timer1", "Test Timer");
 | 
			
		||||
const timer = Logging.time("Test Timer");
 | 
			
		||||
setTimeout(() => timer.end(), 1000);
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user