Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added flush logs on buffer size condition #233

Merged
merged 2 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading