Skip to content

Commit

Permalink
fix logger so we use the top logger in optimizely client
Browse files Browse the repository at this point in the history
  • Loading branch information
Mat001 committed Dec 1, 2021
1 parent ab40d9e commit 795b41a
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 63 deletions.
2 changes: 1 addition & 1 deletion optimizely/optimizely.py
Original file line number Diff line number Diff line change
Expand Up @@ -968,7 +968,7 @@ def create_user_context(self, user_id, attributes=None):
self.logger.error(enums.Errors.INVALID_INPUT.format('attributes'))
return None

return OptimizelyUserContext(self, user_id, attributes)
return OptimizelyUserContext(self, self.logger, user_id, attributes)

def _decide(self, user_context, key, decide_options=None):
"""
Expand Down
20 changes: 10 additions & 10 deletions optimizely/optimizely_user_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import copy
import threading

from . import logger
from .decision.optimizely_decision_message import OptimizelyDecisionMessage
from .helpers import enums

Expand All @@ -26,11 +25,12 @@ class OptimizelyUserContext(object):
Representation of an Optimizely User Context using which APIs are to be called.
"""

def __init__(self, optimizely_client, user_id, user_attributes=None):
def __init__(self, optimizely_client, logger, user_id, user_attributes=None):
""" Create an instance of the Optimizely User Context.
Args:
optimizely_client: client used when calling decisions for this user context
logger: logger for logging
user_id: user id of this user context
user_attributes: user attributes to use for this user context
Expand All @@ -39,6 +39,7 @@ def __init__(self, optimizely_client, user_id, user_attributes=None):
"""

self.client = optimizely_client
self.logger = logger
self.user_id = user_id

if not isinstance(user_attributes, dict):
Expand All @@ -47,7 +48,6 @@ def __init__(self, optimizely_client, user_id, user_attributes=None):
self._user_attributes = user_attributes.copy() if user_attributes else {}
self.lock = threading.Lock()
self.forced_decisions_map = {}
self.log = logger.SimpleLogger(min_level=enums.LogLevels.INFO)

# decision context
class OptimizelyDecisionContext(object):
Expand All @@ -74,7 +74,7 @@ def _clone(self):
if not self.client:
return None

user_context = OptimizelyUserContext(self.client, self.user_id, self.get_user_attributes())
user_context = OptimizelyUserContext(self.client, self.logger, self.user_id, self.get_user_attributes())

with self.lock:
if self.forced_decisions_map:
Expand Down Expand Up @@ -164,7 +164,7 @@ def set_forced_decision(self, decision_context, decision):
True if the forced decision has been set successfully.
"""
if not self.client.is_valid or not self.client.config_manager.get_config():
self.log.logger.error(OptimizelyDecisionMessage.SDK_NOT_READY)
self.logger.error(OptimizelyDecisionMessage.SDK_NOT_READY)
return False

with self.lock:
Expand All @@ -183,7 +183,7 @@ def get_forced_decision(self, decision_context):
A forced_decision or None if forced decisions are not set for the parameters.
"""
if not self.client.is_valid or not self.client.config_manager.get_config():
self.log.logger.error(OptimizelyDecisionMessage.SDK_NOT_READY)
self.logger.error(OptimizelyDecisionMessage.SDK_NOT_READY)
return None

forced_decision = self.find_forced_decision(decision_context)
Expand All @@ -201,7 +201,7 @@ def remove_forced_decision(self, decision_context):
Returns: true if the forced decision has been removed successfully.
"""
if not self.client.is_valid or not self.client.config_manager.get_config():
self.log.logger.error(OptimizelyDecisionMessage.SDK_NOT_READY)
self.logger.error(OptimizelyDecisionMessage.SDK_NOT_READY)
return False

with self.lock:
Expand All @@ -219,7 +219,7 @@ def remove_all_forced_decisions(self):
True if forced decisions have been removed successfully.
"""
if not self.client.is_valid or not self.client.config_manager.get_config():
self.log.logger.error(OptimizelyDecisionMessage.SDK_NOT_READY)
self.logger.error(OptimizelyDecisionMessage.SDK_NOT_READY)
return False

with self.lock:
Expand Down Expand Up @@ -281,7 +281,7 @@ def find_validated_forced_decision(self, decision_context):
self.user_id)

reasons.append(user_has_forced_decision)
self.log.logger.debug(user_has_forced_decision)
self.logger.debug(user_has_forced_decision)

return variation, reasons

Expand All @@ -296,6 +296,6 @@ def find_validated_forced_decision(self, decision_context):
.USER_HAS_FORCED_DECISION_WITHOUT_RULE_SPECIFIED_BUT_INVALID.format(flag_key, self.user_id)

reasons.append(user_has_forced_decision_but_invalid)
self.log.logger.debug(user_has_forced_decision_but_invalid)
self.logger.debug(user_has_forced_decision_but_invalid)

return None, reasons
Loading

0 comments on commit 795b41a

Please sign in to comment.