-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Ensure client logger is always wrapped in a safe logger. #599
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ import { | |
LDLogger, | ||
NumberWithMinimum, | ||
OptionMessages, | ||
SafeLogger, | ||
ServiceEndpoints, | ||
TypeValidators, | ||
} from '@launchdarkly/js-sdk-common'; | ||
|
@@ -21,9 +22,6 @@ export interface LDClientInternalOptions extends internal.LDInternalOptions { | |
|
||
export interface Configuration { | ||
readonly logger: LDLogger; | ||
readonly baseUri: string; | ||
readonly eventsUri: string; | ||
readonly streamUri: string; | ||
readonly maxCachedContexts: number; | ||
readonly capacity: number; | ||
readonly diagnosticRecordingInterval: number; | ||
|
@@ -61,12 +59,20 @@ const DEFAULT_STREAM: string = 'https://clientstream.launchdarkly.com'; | |
|
||
export { DEFAULT_POLLING, DEFAULT_STREAM }; | ||
|
||
function ensureSafeLogger(logger?: LDLogger): LDLogger { | ||
if (logger instanceof SafeLogger) { | ||
return logger; | ||
} | ||
// Even if logger is not defined this will produce a valid logger. | ||
return createSafeLogger(logger); | ||
} | ||
|
||
export default class ConfigurationImpl implements Configuration { | ||
public readonly logger: LDLogger = createSafeLogger(); | ||
|
||
public readonly baseUri = DEFAULT_POLLING; | ||
public readonly eventsUri = ServiceEndpoints.DEFAULT_EVENTS; | ||
public readonly streamUri = DEFAULT_STREAM; | ||
private readonly baseUri = DEFAULT_POLLING; | ||
private readonly eventsUri = ServiceEndpoints.DEFAULT_EVENTS; | ||
private readonly streamUri = DEFAULT_STREAM; | ||
|
||
public readonly maxCachedContexts = 5; | ||
|
||
|
@@ -116,6 +122,7 @@ export default class ConfigurationImpl implements Configuration { | |
[index: string]: any; | ||
|
||
constructor(pristineOptions: LDOptions = {}, internalOptions: LDClientInternalOptions = {}) { | ||
this.logger = ensureSafeLogger(pristineOptions.logger); | ||
const errors = this.validateTypesAndNames(pristineOptions); | ||
errors.forEach((e: string) => this.logger.warn(e)); | ||
|
||
|
@@ -161,6 +168,8 @@ export default class ConfigurationImpl implements Configuration { | |
} else { | ||
errors.push(OptionMessages.wrongOptionType(k, validator.getType(), typeof v)); | ||
} | ||
} else if (k === 'logger') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need a logger before doing any of these other config things, so it is assigned earlier. Then we still let the user know if their logger is itself broken. |
||
// Logger already assigned. | ||
} else { | ||
// if an option is explicitly null, coerce to undefined | ||
this[k] = v ?? undefined; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change isn't related to the logger, but code should ALWAYS use the service endpoints, so we shouldn't be exposing these.