Skip to content

Commit 2cf1045

Browse files
authored
Add method to flush the list of logs (#134)
* Add method to flush the list of logs * Fix unit test
1 parent aa64f08 commit 2cf1045

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

lib/logzio-nodejs.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ interface ILogzioLogger extends ILoggerOptions {
2323
log(msg: any, obj?: object): void;
2424
close(): void;
2525
sendAndClose(callback?: (error: Error, bulk: object) => void): void;
26+
flush(callback?: (error: Error, bulk: object) => void): void;
2627
}
2728

2829
export function createLogger(options: ILoggerOptions): ILogzioLogger;

lib/logzio-nodejs.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,12 @@ class LogzioLogger {
130130
}
131131
}
132132

133+
flush(callback) {
134+
this.callback = callback || this._defaultCallback;
135+
this._debug('Flushing messages...');
136+
this._popMsgsAndSend();
137+
}
138+
133139
sendAndClose(callback) {
134140
this.callback = callback || this._defaultCallback;
135141
this._debug('Sending last messages and closing...');

test/logger.test.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,4 +617,53 @@ describe('logger', () => {
617617
logger.close();
618618
});
619619
});
620+
621+
describe('Flush log messages', () => {
622+
623+
afterAll((done) => {
624+
axiosInstance.post.restore();
625+
done();
626+
});
627+
628+
beforeAll((done) => {
629+
sinon
630+
.stub(axiosInstance, 'post')
631+
.resolves({
632+
statusCode: 200,
633+
});
634+
done();
635+
});
636+
637+
it('should send one log at a time', (done) => {
638+
let timesCalled = 0;
639+
const bufferSize = 3;
640+
const logCount = 3;
641+
const expectedTimes = 3;
642+
643+
function assertCalled() {
644+
timesCalled += 1;
645+
646+
if (logCount === timesCalled) {
647+
done();
648+
} else if (timesCalled > expectedTimes) {
649+
throw new Error('called less times than expected');
650+
}
651+
}
652+
653+
const logger = createLogger({
654+
bufferSize,
655+
});
656+
657+
Array(logCount).fill().forEach((item, i) => {
658+
logger.log({
659+
message: `hello there from test #${i}`,
660+
id: i,
661+
});
662+
663+
logger.flush(assertCalled);
664+
});
665+
666+
logger.close();
667+
});
668+
});
620669
});

0 commit comments

Comments
 (0)