From 0451b254892c126ac79feee5f22e4ecfb869c62f Mon Sep 17 00:00:00 2001 From: Ryan Lamb <4955475+kinyoklion@users.noreply.github.com> Date: Wed, 13 Nov 2024 10:23:53 -0800 Subject: [PATCH] feat: Add support for getting the logger from server-side SDKs. --- .../sdk-server/__tests__/LDClientImpl.test.ts | 13 +++++++++++++ packages/shared/sdk-server/src/LDClientImpl.ts | 4 ++++ packages/shared/sdk-server/src/api/LDClient.ts | 8 ++++++++ 3 files changed, 25 insertions(+) diff --git a/packages/shared/sdk-server/__tests__/LDClientImpl.test.ts b/packages/shared/sdk-server/__tests__/LDClientImpl.test.ts index 34d97823d..8cd437241 100644 --- a/packages/shared/sdk-server/__tests__/LDClientImpl.test.ts +++ b/packages/shared/sdk-server/__tests__/LDClientImpl.test.ts @@ -259,4 +259,17 @@ describe('LDClientImpl', () => { await client.waitForInitialization({ timeout: Number.MAX_SAFE_INTEGER }); expect(logger.getCount(LogLevel.Warn)).toBe(0); }); + + it('provides access to the underlying logger', async () => { + const logger = new TestLogger(); + client = createClient({ logger, offline: true }); + client.logger?.error('this is an error'); + expect(logger.getCount(LogLevel.Error)).toBe(1); + logger.expectMessages([ + { + level: LogLevel.Error, + matches: /this is an error/, + }, + ]); + }); }); diff --git a/packages/shared/sdk-server/src/LDClientImpl.ts b/packages/shared/sdk-server/src/LDClientImpl.ts index bdc4df775..08a25dba2 100644 --- a/packages/shared/sdk-server/src/LDClientImpl.ts +++ b/packages/shared/sdk-server/src/LDClientImpl.ts @@ -128,6 +128,10 @@ export default class LDClientImpl implements LDClient { private _hookRunner: HookRunner; + public get logger(): LDLogger | undefined { + return this._logger; + } + /** * Intended for use by platform specific client implementations. * diff --git a/packages/shared/sdk-server/src/api/LDClient.ts b/packages/shared/sdk-server/src/api/LDClient.ts index 221d762f8..0d6d1d7c2 100644 --- a/packages/shared/sdk-server/src/api/LDClient.ts +++ b/packages/shared/sdk-server/src/api/LDClient.ts @@ -3,6 +3,7 @@ import { LDEvaluationDetail, LDEvaluationDetailTyped, LDFlagValue, + LDLogger, } from '@launchdarkly/js-sdk-common'; import { LDMigrationOpEvent, LDMigrationVariation } from './data'; @@ -461,4 +462,11 @@ export interface LDClient { * @param Hook The hook to add. */ addHook?(hook: Hook): void; + + /** + * Get the logger used by this LDClient instance. + * + * For all platforms that support logging the logger should be present. + */ + get logger(): LDLogger | undefined; }