mirror of
https://git.stamm.me/OpenServer/NodeLogging.git
synced 2024-11-24 19:59:24 +00:00
Adding initialization on first write
This commit is contained in:
parent
347b92c471
commit
19097d2831
5
.editorconfig
Normal file
5
.editorconfig
Normal file
@ -0,0 +1,5 @@
|
||||
charset = utf-8
|
||||
indent_style = space
|
||||
indent_size = 3
|
||||
trim_trailing_whitespace = true
|
||||
end_of_line = lf
|
17
out/index.d.ts
vendored
17
out/index.d.ts
vendored
@ -46,6 +46,7 @@ export interface LoggingBaseOptions {
|
||||
export declare class LoggingBase {
|
||||
private config;
|
||||
private writeLock;
|
||||
private setted_up;
|
||||
private fileStream;
|
||||
private errorStream;
|
||||
private fileSize;
|
||||
@ -54,7 +55,7 @@ export declare class LoggingBase {
|
||||
constructor(options?: Partial<LoggingBaseOptions>);
|
||||
console_out: boolean;
|
||||
waitForSetup(): Promise<void>;
|
||||
private setup();
|
||||
private setup;
|
||||
events: EventEmitter;
|
||||
debug(...message: any[]): void;
|
||||
log(...message: any[]): void;
|
||||
@ -62,12 +63,12 @@ export declare class LoggingBase {
|
||||
logWithCustomColors(type: LoggingTypes, colors: string, ...message: any[]): void;
|
||||
error(error: Error | string): void;
|
||||
errorMessage(...message: any[]): void;
|
||||
private message(type, message, customColors?, caller?);
|
||||
private writeMessageToFile(message, error?);
|
||||
private checkQueue();
|
||||
private writeToLogFile(data);
|
||||
private writeToErrorFile(data);
|
||||
private initializeFile(file, new_file?);
|
||||
private message;
|
||||
private writeMessageToFile;
|
||||
private checkQueue;
|
||||
private writeToLogFile;
|
||||
private writeToErrorFile;
|
||||
private initializeFile;
|
||||
}
|
||||
export declare let Logging: any;
|
||||
export default Logging;
|
||||
@ -75,5 +76,5 @@ export declare enum LoggingTypes {
|
||||
Log = 0,
|
||||
Warning = 1,
|
||||
Error = 2,
|
||||
Debug = 3,
|
||||
Debug = 3
|
||||
}
|
||||
|
11
out/index.js
11
out/index.js
@ -35,6 +35,7 @@ const OriginalErrorStackFunction = Error.prototype.prepareStackTrace;
|
||||
class LoggingBase {
|
||||
constructor(options) {
|
||||
this.writeLock = new lock_1.default();
|
||||
this.setted_up = false;
|
||||
this.fileSize = 0;
|
||||
this.errorSize = 0;
|
||||
this.queue = new Array();
|
||||
@ -55,7 +56,10 @@ class LoggingBase {
|
||||
logfile: "./logs/all.log",
|
||||
errorfile: "./logs/error.log"
|
||||
}, options);
|
||||
this.setup();
|
||||
for (let key in this) {
|
||||
if (typeof this[key] === "function")
|
||||
this[key] = this[key].bind(this);
|
||||
}
|
||||
}
|
||||
get console_out() {
|
||||
return this.config.console_out;
|
||||
@ -67,6 +71,7 @@ class LoggingBase {
|
||||
(await this.writeLock.getLock()).release();
|
||||
}
|
||||
async setup() {
|
||||
this.setted_up = true;
|
||||
let lock = await this.writeLock.getLock();
|
||||
if (this.config.logfile) {
|
||||
let f = await this.initializeFile(this.config.logfile, true);
|
||||
@ -74,7 +79,7 @@ class LoggingBase {
|
||||
this.fileSize = f.size;
|
||||
}
|
||||
if (this.config.errorfile) {
|
||||
let f = await this.initializeFile(this.config.errorfile, true);
|
||||
let f = await this.initializeFile(this.config.errorfile, false);
|
||||
this.errorStream = f.stream;
|
||||
this.errorSize = f.size;
|
||||
}
|
||||
@ -176,6 +181,8 @@ class LoggingBase {
|
||||
return;
|
||||
if (this.queue.length <= 0)
|
||||
return;
|
||||
if (!this.setted_up)
|
||||
return this.setup();
|
||||
let lock = await this.writeLock.getLock();
|
||||
var message = this.queue.shift();
|
||||
message.message += "\n";
|
||||
|
File diff suppressed because one or more lines are too long
4
out/lock.d.ts
vendored
4
out/lock.d.ts
vendored
@ -7,6 +7,6 @@ export default class Lock {
|
||||
private toCome;
|
||||
constructor();
|
||||
getLock(): Promise<Release>;
|
||||
private lock();
|
||||
private release();
|
||||
private lock;
|
||||
private release;
|
||||
}
|
||||
|
@ -1 +1 @@
|
||||
{"version":3,"file":"lock.js","sourceRoot":"","sources":["../src/lock.ts"],"names":[],"mappings":";;AACA;IAOG;QANQ,YAAO,GAAY,K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;IACvB,CAAC;IAOD,KAAK,CAAC,OAAO;QACV,IAAI,CAAC,IAAI,CAAC,OAAO;YAAE,OAAO,EAAE,OAAO,EAAE,IAAI,CAAC,IAAI,EAAE,EAAE,CAAC;aAC9C;YACF,OAAO,IAAI,OAAO,CAAU,CAAC,OAAO,EAAE,EAAE;gBACrC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE;oBACnB,OAAO,CAAC,EAAE,OAAO,EAAE,IAAI,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;gBACrC,CAAC,CAAC,CAAA;YACL,CAAC,CAAC,CAAA;SACJ;IACJ,CAAC;IAEO,IAAI;QACT,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC;QACpB,OAAO,IAAI,CAAC,OAAO,CAAC;IACvB,CAAC;IAEO,KAAK,CAAC,OAAO;QAClB,IAAI,IAAI,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE;YACzB,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,EAAE,CAAC;SACxB;aAAM;YACJ,IAAI,CAAC,OAAO,GAAG,KAAK,CAAC;SACvB;IACJ,CAAC;CACH;AAlCD,uBAkCC"}
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@hibas123/nodelogging",
|
||||
"version": "1.3.12",
|
||||
"version": "1.3.13",
|
||||
"description": "",
|
||||
"main": "out/index.js",
|
||||
"types": "out/index.d.ts",
|
||||
|
10
src/index.ts
10
src/index.ts
@ -59,6 +59,8 @@ export class LoggingBase {
|
||||
private config: LoggingBaseOptions;
|
||||
private writeLock = new Lock();
|
||||
|
||||
private setted_up = false;
|
||||
|
||||
private fileStream: fs.WriteStream;
|
||||
private errorStream: fs.WriteStream;
|
||||
private fileSize: number = 0;
|
||||
@ -83,7 +85,11 @@ export class LoggingBase {
|
||||
logfile: "./logs/all.log",
|
||||
errorfile: "./logs/error.log"
|
||||
}, options);
|
||||
this.setup();
|
||||
|
||||
for (let key in this) {
|
||||
if (typeof this[key] === "function") this[key] = (<any>this[key]).bind(this);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
get console_out() {
|
||||
@ -99,6 +105,7 @@ export class LoggingBase {
|
||||
}
|
||||
|
||||
private async setup() {
|
||||
this.setted_up = true;
|
||||
let lock = await this.writeLock.getLock();
|
||||
if (this.config.logfile) {
|
||||
let f = await this.initializeFile(this.config.logfile, true);
|
||||
@ -213,6 +220,7 @@ export class LoggingBase {
|
||||
try {
|
||||
if (this.writeLock.locked) return;
|
||||
if (this.queue.length <= 0) return;
|
||||
if (!this.setted_up) return this.setup();
|
||||
let lock = await this.writeLock.getLock();
|
||||
var message = this.queue.shift();
|
||||
message.message += "\n";
|
||||
|
Loading…
Reference in New Issue
Block a user