diff --git a/packages/sdk/browser/src/BrowserClient.ts b/packages/sdk/browser/src/BrowserClient.ts index 892ed688d1..898c0f3069 100644 --- a/packages/sdk/browser/src/BrowserClient.ts +++ b/packages/sdk/browser/src/BrowserClient.ts @@ -1,6 +1,7 @@ import { AutoEnvAttributes, base64UrlEncode, + BasicLogger, LDClient as CommonClient, Configuration, createSafeLogger, @@ -98,15 +99,18 @@ export class BrowserClient extends LDClientImpl implements LDClient { // Overrides the default logger from the common implementation. const logger = customLogger ?? - createSafeLogger({ - // eslint-disable-next-line no-console - debug: debug ? console.debug : () => {}, - // eslint-disable-next-line no-console - info: console.info, - // eslint-disable-next-line no-console - warn: console.warn, - // eslint-disable-next-line no-console - error: console.error, + new BasicLogger({ + destination: { + // eslint-disable-next-line no-console + debug: console.debug, + // eslint-disable-next-line no-console + info: console.info, + // eslint-disable-next-line no-console + warn: console.warn, + // eslint-disable-next-line no-console + error: console.error, + }, + level: debug ? 'debug' : 'info', }); // TODO: Use the already-configured baseUri from the SDK config. SDK-560 diff --git a/packages/sdk/browser/src/index.ts b/packages/sdk/browser/src/index.ts index 6fe3a53a7f..7c02521cb6 100644 --- a/packages/sdk/browser/src/index.ts +++ b/packages/sdk/browser/src/index.ts @@ -12,6 +12,8 @@ */ import { AutoEnvAttributes, + BasicLogger, + BasicLoggerOptions, EvaluationSeriesContext, EvaluationSeriesData, Hook, @@ -84,3 +86,54 @@ export function initialize(clientSideId: string, options?: LDOptions): LDClient // AutoEnvAttributes are not supported yet in the browser SDK. return new BrowserClient(clientSideId, AutoEnvAttributes.Disabled, options); } + +/** + * Provides a simple {@link LDLogger} implementation. + * + * This logging implementation uses a simple format that includes only the log level + * and the message text. By default the output is written to `console.error`. + * + * To use the logger created by this function, put it into {@link LDOptions.logger}. If + * you do not set {@link LDOptions.logger} to anything, the SDK uses a default logger + * that will log "info" level and higher priorty messages and it will log messages to + * console.info, console.warn, and console.error. + * + * @param options Configuration for the logger. If no options are specified, the + * logger uses `{ level: 'info' }`. + * + * @example + * This example shows how to use `basicLogger` in your SDK options to enable console + * logging only at `warn` and `error` levels. + * ```javascript + * const ldOptions = { + * logger: basicLogger({ level: 'warn' }), + * }; + * ``` + * + * @example + * This example shows how to use `basicLogger` in your SDK options to cause all + * log output to go to `console.log` + * ```javascript + * const ldOptions = { + * logger: ld.basicLogger({ destination: console.log }), + * }; + * ``` + * + * * @example + * The configuration also allows you to control the destination for each log level. + * ```javascript + * const ldOptions = { + * logger: ld.basicLogger({ + * destination: { + * debug: console.debug, + * info: console.info, + * warn: console.warn, + * error:console.error + * } + * }), + * }; + * ``` + */ +export function basicLogger(options: BasicLoggerOptions): LDLogger { + return new BasicLogger(options); +} diff --git a/packages/shared/sdk-client/src/api/LDOptions.ts b/packages/shared/sdk-client/src/api/LDOptions.ts index 27627011a4..5ce6b647d3 100644 --- a/packages/shared/sdk-client/src/api/LDOptions.ts +++ b/packages/shared/sdk-client/src/api/LDOptions.ts @@ -126,7 +126,10 @@ export interface LDOptions { * @remarks * Set a custom {@link LDLogger} if you want full control of logging behavior. * - * @defaultValue A {@link BasicLogger} which outputs to the console at `info` level. + * @defaultValue The default logging implementation will varybased on platform. For the browser + * the default logger will log "info" level and higher priorty messages and it will log messages to + * console.info, console.warn, and console.error. Other platforms may use a `BasicLogger` instance + * also defaulted to the "info" level. */ logger?: LDLogger;