Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions packages/sdk/browser/src/BrowserClient.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
AutoEnvAttributes,
base64UrlEncode,
BasicLogger,
LDClient as CommonClient,
Configuration,
createSafeLogger,
Expand Down Expand Up @@ -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
Expand Down
53 changes: 53 additions & 0 deletions packages/sdk/browser/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*/
import {
AutoEnvAttributes,
BasicLogger,
BasicLoggerOptions,
EvaluationSeriesContext,
EvaluationSeriesData,
Hook,
Expand Down Expand Up @@ -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);
}
5 changes: 4 additions & 1 deletion packages/shared/sdk-client/src/api/LDOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down