Adding initialization on first write

This commit is contained in:
Fabian Stamm 2018-09-28 10:05:10 +02:00
parent 347b92c471
commit 19097d2831
8 changed files with 37 additions and 16 deletions

5
.editorconfig Normal file
View 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
View File

@ -46,6 +46,7 @@ export interface LoggingBaseOptions {
export declare class LoggingBase { export declare class LoggingBase {
private config; private config;
private writeLock; private writeLock;
private setted_up;
private fileStream; private fileStream;
private errorStream; private errorStream;
private fileSize; private fileSize;
@ -54,7 +55,7 @@ export declare class LoggingBase {
constructor(options?: Partial<LoggingBaseOptions>); constructor(options?: Partial<LoggingBaseOptions>);
console_out: boolean; console_out: boolean;
waitForSetup(): Promise<void>; waitForSetup(): Promise<void>;
private setup(); private setup;
events: EventEmitter; events: EventEmitter;
debug(...message: any[]): void; debug(...message: any[]): void;
log(...message: any[]): void; log(...message: any[]): void;
@ -62,12 +63,12 @@ export declare class LoggingBase {
logWithCustomColors(type: LoggingTypes, colors: string, ...message: any[]): void; logWithCustomColors(type: LoggingTypes, colors: string, ...message: any[]): void;
error(error: Error | string): void; error(error: Error | string): void;
errorMessage(...message: any[]): void; errorMessage(...message: any[]): void;
private message(type, message, customColors?, caller?); private message;
private writeMessageToFile(message, error?); private writeMessageToFile;
private checkQueue(); private checkQueue;
private writeToLogFile(data); private writeToLogFile;
private writeToErrorFile(data); private writeToErrorFile;
private initializeFile(file, new_file?); private initializeFile;
} }
export declare let Logging: any; export declare let Logging: any;
export default Logging; export default Logging;
@ -75,5 +76,5 @@ export declare enum LoggingTypes {
Log = 0, Log = 0,
Warning = 1, Warning = 1,
Error = 2, Error = 2,
Debug = 3, Debug = 3
} }

View File

@ -35,6 +35,7 @@ const OriginalErrorStackFunction = Error.prototype.prepareStackTrace;
class LoggingBase { class LoggingBase {
constructor(options) { constructor(options) {
this.writeLock = new lock_1.default(); this.writeLock = new lock_1.default();
this.setted_up = false;
this.fileSize = 0; this.fileSize = 0;
this.errorSize = 0; this.errorSize = 0;
this.queue = new Array(); this.queue = new Array();
@ -55,7 +56,10 @@ class LoggingBase {
logfile: "./logs/all.log", logfile: "./logs/all.log",
errorfile: "./logs/error.log" errorfile: "./logs/error.log"
}, options); }, options);
this.setup(); for (let key in this) {
if (typeof this[key] === "function")
this[key] = this[key].bind(this);
}
} }
get console_out() { get console_out() {
return this.config.console_out; return this.config.console_out;
@ -67,6 +71,7 @@ class LoggingBase {
(await this.writeLock.getLock()).release(); (await this.writeLock.getLock()).release();
} }
async setup() { async setup() {
this.setted_up = true;
let lock = await this.writeLock.getLock(); let lock = await this.writeLock.getLock();
if (this.config.logfile) { if (this.config.logfile) {
let f = await this.initializeFile(this.config.logfile, true); let f = await this.initializeFile(this.config.logfile, true);
@ -74,7 +79,7 @@ class LoggingBase {
this.fileSize = f.size; this.fileSize = f.size;
} }
if (this.config.errorfile) { 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.errorStream = f.stream;
this.errorSize = f.size; this.errorSize = f.size;
} }
@ -176,6 +181,8 @@ class LoggingBase {
return; return;
if (this.queue.length <= 0) if (this.queue.length <= 0)
return; return;
if (!this.setted_up)
return this.setup();
let lock = await this.writeLock.getLock(); let lock = await this.writeLock.getLock();
var message = this.queue.shift(); var message = this.queue.shift();
message.message += "\n"; message.message += "\n";

File diff suppressed because one or more lines are too long

4
out/lock.d.ts vendored
View File

@ -7,6 +7,6 @@ export default class Lock {
private toCome; private toCome;
constructor(); constructor();
getLock(): Promise<Release>; getLock(): Promise<Release>;
private lock(); private lock;
private release(); private release;
} }

View File

@ -1 +1 @@
{"version":3,"file":"lock.js","sourceRoot":"","sources":["../src/lock.ts"],"names":[],"mappings":";;AACA;IAOG;QANQ,YAAO,GAAY,KAAK,CAAC;QAIzB,WAAM,GAAmB,EAAE,CAAC;QAGjC,IAAI,CAAC,OAAO,GAAG,IAAI,C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,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"}

View File

@ -1,6 +1,6 @@
{ {
"name": "@hibas123/nodelogging", "name": "@hibas123/nodelogging",
"version": "1.3.12", "version": "1.3.13",
"description": "", "description": "",
"main": "out/index.js", "main": "out/index.js",
"types": "out/index.d.ts", "types": "out/index.d.ts",

View File

@ -59,6 +59,8 @@ export class LoggingBase {
private config: LoggingBaseOptions; private config: LoggingBaseOptions;
private writeLock = new Lock(); private writeLock = new Lock();
private setted_up = false;
private fileStream: fs.WriteStream; private fileStream: fs.WriteStream;
private errorStream: fs.WriteStream; private errorStream: fs.WriteStream;
private fileSize: number = 0; private fileSize: number = 0;
@ -83,7 +85,11 @@ export class LoggingBase {
logfile: "./logs/all.log", logfile: "./logs/all.log",
errorfile: "./logs/error.log" errorfile: "./logs/error.log"
}, options); }, options);
this.setup();
for (let key in this) {
if (typeof this[key] === "function") this[key] = (<any>this[key]).bind(this);
}
} }
get console_out() { get console_out() {
@ -99,6 +105,7 @@ export class LoggingBase {
} }
private async setup() { private async setup() {
this.setted_up = true;
let lock = await this.writeLock.getLock(); let lock = await this.writeLock.getLock();
if (this.config.logfile) { if (this.config.logfile) {
let f = await this.initializeFile(this.config.logfile, true); let f = await this.initializeFile(this.config.logfile, true);
@ -213,6 +220,7 @@ export class LoggingBase {
try { try {
if (this.writeLock.locked) return; if (this.writeLock.locked) return;
if (this.queue.length <= 0) return; if (this.queue.length <= 0) return;
if (!this.setted_up) return this.setup();
let lock = await this.writeLock.getLock(); let lock = await this.writeLock.getLock();
var message = this.queue.shift(); var message = this.queue.shift();
message.message += "\n"; message.message += "\n";