Skip to content

Commit

Permalink
Add the ability to flush() the LogSink from OptionalLogger and friends (
Browse files Browse the repository at this point in the history
  • Loading branch information
darkgnotic committed May 23, 2023
1 parent 9f3df39 commit 25d3d66
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@rocicorp/logger",
"description": "Logging utilities",
"version": "5.0.0",
"version": "5.1.0",
"repository": "github:rocicorp/logger",
"license": "Apache-2.0",
"engines": {
Expand Down
9 changes: 9 additions & 0 deletions src/logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,15 @@ test('tee logger flush', async () => {
expect(l3.flushCount).to.equal(1);
});

test('optional logger flush', async () => {
const l1 = new TestLogSinkWithFlush();
const logger = new OptionalLoggerImpl(l1);

expect(l1.flushCount).to.equal(0);
await logger.flush();
expect(l1.flushCount).to.equal(1);
});

test('Console logger calls JSON stringify on complex arguments', () => {
const jsonStringifySpy = sinon.spy(JSON, 'stringify');
const mockDebug = mockConsoleMethod('debug');
Expand Down
4 changes: 4 additions & 0 deletions src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface OptionalLogger {
error?(...args: unknown[]): void;
info?(...args: unknown[]): void;
debug?(...args: unknown[]): void;
flush?(): Promise<void>;
}

/**
Expand Down Expand Up @@ -59,6 +60,7 @@ export class OptionalLoggerImpl implements OptionalLogger {
readonly debug?: (...args: unknown[]) => void = undefined;
readonly info?: (...args: unknown[]) => void = undefined;
readonly error?: (...args: unknown[]) => void = undefined;
readonly flush: () => Promise<void>;

constructor(logSink: LogSink, level: LogLevel = 'info', context?: Context) {
const impl =
Expand All @@ -78,6 +80,8 @@ export class OptionalLoggerImpl implements OptionalLogger {
this.error = impl('error');
}
/* eslint-enable @typescript-eslint/ban-ts-comment, no-fallthrough */

this.flush = () => logSink.flush?.() ?? Promise.resolve();
}
}

Expand Down

0 comments on commit 25d3d66

Please sign in to comment.