Skip to content

Commit

Permalink
Added flush logs on buffer size condition
Browse files Browse the repository at this point in the history
PR-URL: #233
  • Loading branch information
RohovAlex committed Oct 17, 2023
1 parent 3bc8a52 commit 0d3148d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
1 change: 1 addition & 0 deletions metalog.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export class Logger extends EventEmitter {
flushTimer: NodeJS.Timer;
lock: boolean;
buffer: Array<Buffer>;
bufferLength: number;
file: string;
toFile: Record<string, boolean>;
fsEnabled: boolean;
Expand Down
7 changes: 5 additions & 2 deletions metalog.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ class Logger extends events.EventEmitter {
this.flushTimer = null;
this.lock = false;
this.buffer = [];
this.bufferLength = 0;
this.file = '';
this.toFile = logTypes(toFile);
this.fsEnabled = toFile.length !== 0;
Expand Down Expand Up @@ -246,8 +247,7 @@ class Logger extends events.EventEmitter {
});
}, nextReopen);
if (this.keepDays) await this.rotate();
const options = { flags: 'a', bufferSize: this.writeBuffer };
this.stream = this.createStream(this.file, options);
this.stream = this.createStream(this.file, { flags: 'a' });
this.flushTimer = setInterval(() => {
this.flush();
}, this.writeInterval);
Expand Down Expand Up @@ -371,6 +371,8 @@ class Logger extends events.EventEmitter {
: this.formatFile(type, indent, ...args);
const buffer = Buffer.from(line + '\n');
this.buffer.push(buffer);
this.bufferLength += buffer.length;
if (this.bufferLength >= this.writeBuffer) this.flush();
}
}

Expand All @@ -392,6 +394,7 @@ class Logger extends events.EventEmitter {
this.lock = true;
const buffer = Buffer.concat(this.buffer);
this.buffer.length = 0;
this.bufferLength = 0;
this.stream.write(buffer, () => {
this.lock = false;
this.emit('unlocked');
Expand Down

0 comments on commit 0d3148d

Please sign in to comment.