mirror of
				https://git.hibas.dev/OpenServer/NodeLogging.git
				synced 2025-11-03 22:40:45 +00:00 
			
		
		
		
	Adding config option to set console output name for additional Logging objects.
This commit is contained in:
		
							
								
								
									
										27
									
								
								src/index.ts
									
									
									
									
									
								
							
							
						
						
									
										27
									
								
								src/index.ts
									
									
									
									
									
								
							@ -37,6 +37,10 @@ const maxFileSize = 500000000;
 | 
			
		||||
const OriginalErrorStackFunction = (<any>Error.prototype).prepareStackTrace
 | 
			
		||||
 | 
			
		||||
export interface LoggingBaseOptions {
 | 
			
		||||
   /**
 | 
			
		||||
    * Name will be prefixed on Console output
 | 
			
		||||
    */
 | 
			
		||||
   name: string,
 | 
			
		||||
   logfile: string;
 | 
			
		||||
   errorfile: string;
 | 
			
		||||
   console_out: boolean;
 | 
			
		||||
@ -56,6 +60,7 @@ export class LoggingBase {
 | 
			
		||||
   constructor(options?: Partial<LoggingBaseOptions>) {
 | 
			
		||||
      if (!options) options = {};
 | 
			
		||||
      this.config = Object.assign(<LoggingBaseOptions>{
 | 
			
		||||
         name: undefined,
 | 
			
		||||
         console_out: true,
 | 
			
		||||
         logfile: "./logs/all.log",
 | 
			
		||||
         errorfile: "./logs/error.log"
 | 
			
		||||
@ -161,7 +166,11 @@ export class LoggingBase {
 | 
			
		||||
      let prefix = `[${LoggingTypes[type]}][${file.file}:${file.line}][${date}]: `;
 | 
			
		||||
      let message_lines = mb.split("\n").map(line => prefix + line);
 | 
			
		||||
 | 
			
		||||
      if (this.config.console_out) message_lines.forEach(line => console.log(consoleLogFormat + line + Colors.Reset));
 | 
			
		||||
      if (this.config.console_out) {
 | 
			
		||||
         let prefix = "";
 | 
			
		||||
         if (this.config.name) prefix = `[${this.config.name}]`;
 | 
			
		||||
         message_lines.forEach(line => console.log(consoleLogFormat + prefix + line + Colors.Reset));
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      let m = message_lines.join("\n");
 | 
			
		||||
      let index = m.indexOf("\x1b");
 | 
			
		||||
@ -249,22 +258,6 @@ export class LoggingBase {
 | 
			
		||||
         }
 | 
			
		||||
 | 
			
		||||
         return { stream: fs.createWriteStream(file, { flags: "a" }), size: size };
 | 
			
		||||
 | 
			
		||||
         // if (await fsExists(this.logFileLocation + "error.log")) {
 | 
			
		||||
         //    let stats = await fsStat(this.logFileLocation + "error.log")
 | 
			
		||||
         //    if (stats.size > maxFileSize) {
 | 
			
		||||
         //       if (await fsExists(this.logFileLocation + "error.log.old"))
 | 
			
		||||
         //          await fsUnlink(this.logFileLocation + "error.log.old");
 | 
			
		||||
         //       await fsMove(this.logFileLocation + "error.log", this.logFileLocation + "error.log.old")
 | 
			
		||||
         //    } else {
 | 
			
		||||
         //       this.errorSize = stats.size;
 | 
			
		||||
         //    }
 | 
			
		||||
         // }
 | 
			
		||||
 | 
			
		||||
         // this.fileStream = fs.createWriteStream(this.logFileLocation + "all.log", { flags: "a" });
 | 
			
		||||
         // this.errorStream = fs.createWriteStream(this.logFileLocation + "error.log", { flags: "a" });
 | 
			
		||||
         // this.writing = false;
 | 
			
		||||
         // this.checkQueue();
 | 
			
		||||
      } catch (e) {
 | 
			
		||||
         console.log(e);
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user