From 25d3d6649230770d53f00d7878de217850b3bd2f Mon Sep 17 00:00:00 2001 From: d-llama <132324914+d-llama@users.noreply.github.com> Date: Mon, 22 May 2023 22:04:25 -0700 Subject: [PATCH] Add the ability to flush() the LogSink from OptionalLogger and friends (#19) --- package.json | 2 +- src/logger.test.ts | 9 +++++++++ src/logger.ts | 4 ++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 9743b58..d1f7ecc 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/logger.test.ts b/src/logger.test.ts index e789757..56576c8 100644 --- a/src/logger.test.ts +++ b/src/logger.test.ts @@ -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'); diff --git a/src/logger.ts b/src/logger.ts index 93223ff..417b2cd 100644 --- a/src/logger.ts +++ b/src/logger.ts @@ -9,6 +9,7 @@ export interface OptionalLogger { error?(...args: unknown[]): void; info?(...args: unknown[]): void; debug?(...args: unknown[]): void; + flush?(): Promise; } /** @@ -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; constructor(logSink: LogSink, level: LogLevel = 'info', context?: Context) { const impl = @@ -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(); } }