Skip to content

Commit f762aea

Browse files
committed
feat: Use enhanced logger support for browser SDK.
1 parent e551293 commit f762aea

File tree

3 files changed

+70
-10
lines changed

3 files changed

+70
-10
lines changed

packages/sdk/browser/src/BrowserClient.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
AutoEnvAttributes,
33
base64UrlEncode,
4+
BasicLogger,
45
LDClient as CommonClient,
56
Configuration,
67
createSafeLogger,
@@ -98,15 +99,18 @@ export class BrowserClient extends LDClientImpl implements LDClient {
9899
// Overrides the default logger from the common implementation.
99100
const logger =
100101
customLogger ??
101-
createSafeLogger({
102-
// eslint-disable-next-line no-console
103-
debug: debug ? console.debug : () => {},
104-
// eslint-disable-next-line no-console
105-
info: console.info,
106-
// eslint-disable-next-line no-console
107-
warn: console.warn,
108-
// eslint-disable-next-line no-console
109-
error: console.error,
102+
new BasicLogger({
103+
destination: {
104+
// eslint-disable-next-line no-console
105+
debug: console.debug,
106+
// eslint-disable-next-line no-console
107+
info: console.info,
108+
// eslint-disable-next-line no-console
109+
warn: console.warn,
110+
// eslint-disable-next-line no-console
111+
error: console.error,
112+
},
113+
level: debug ? 'debug' : 'info',
110114
});
111115

112116
// TODO: Use the already-configured baseUri from the SDK config. SDK-560

packages/sdk/browser/src/index.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
*/
1313
import {
1414
AutoEnvAttributes,
15+
BasicLogger,
16+
BasicLoggerOptions,
1517
EvaluationSeriesContext,
1618
EvaluationSeriesData,
1719
Hook,
@@ -84,3 +86,54 @@ export function initialize(clientSideId: string, options?: LDOptions): LDClient
8486
// AutoEnvAttributes are not supported yet in the browser SDK.
8587
return new BrowserClient(clientSideId, AutoEnvAttributes.Disabled, options);
8688
}
89+
90+
/**
91+
* Provides a simple {@link LDLogger} implementation.
92+
*
93+
* This logging implementation uses a simple format that includes only the log level
94+
* and the message text. By default the output is written to `console.error`.
95+
*
96+
* To use the logger created by this function, put it into {@link LDOptions.logger}. If
97+
* you do not set {@link LDOptions.logger} to anything, the SDK uses a default logger
98+
* that will log "info" level and higher priorty messages and it will log messages to
99+
* console.info, console.warn, and console.error.
100+
*
101+
* @param options Configuration for the logger. If no options are specified, the
102+
* logger uses `{ level: 'info' }`.
103+
*
104+
* @example
105+
* This example shows how to use `basicLogger` in your SDK options to enable console
106+
* logging only at `warn` and `error` levels.
107+
* ```javascript
108+
* const ldOptions = {
109+
* logger: basicLogger({ level: 'warn' }),
110+
* };
111+
* ```
112+
*
113+
* @example
114+
* This example shows how to use `basicLogger` in your SDK options to cause all
115+
* log output to go to `console.log`
116+
* ```javascript
117+
* const ldOptions = {
118+
* logger: ld.basicLogger({ destination: console.log }),
119+
* };
120+
* ```
121+
*
122+
* * @example
123+
* The configuration also allows you to control the destination for each log level.
124+
* ```javascript
125+
* const ldOptions = {
126+
* logger: ld.basicLogger({
127+
* destination: {
128+
* debug: console.debug,
129+
* info: console.info,
130+
* warn: console.warn,
131+
* error:console.error
132+
* }
133+
* }),
134+
* };
135+
* ```
136+
*/
137+
export function basicLogger(options: BasicLoggerOptions): LDLogger {
138+
return new BasicLogger(options);
139+
}

packages/shared/sdk-client/src/api/LDOptions.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,10 @@ export interface LDOptions {
126126
* @remarks
127127
* Set a custom {@link LDLogger} if you want full control of logging behavior.
128128
*
129-
* @defaultValue A {@link BasicLogger} which outputs to the console at `info` level.
129+
* @defaultValue The default logging implementation will varybased on platform. For the browser
130+
* the default logger will log "info" level and higher priorty messages and it will log messages to
131+
* console.info, console.warn, and console.error. Other platforms may use a `BasicLogger` instance
132+
* also defaulted to the "info" level.
130133
*/
131134
logger?: LDLogger;
132135

0 commit comments

Comments
 (0)