Skip to content

Commit 2db23ae

Browse files
Fixing logger (#29)
1 parent 3ec80a8 commit 2db23ae

File tree

2 files changed

+38
-35
lines changed

2 files changed

+38
-35
lines changed

optimizely/optimizely.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import logging
21
import sys
32

43
from . import bucketer
@@ -12,6 +11,7 @@
1211
from .helpers import experiment as experiment_helper
1312
from .helpers import validator
1413
from .logger import NoOpLogger as noop_logger
14+
from .logger import SimpleLogger
1515

1616

1717
class Optimizely(object):
@@ -39,15 +39,17 @@ def __init__(self, datafile, event_dispatcher=None, logger=None, error_handler=N
3939
self._validate_inputs(datafile, skip_json_validation)
4040
except exceptions.InvalidInputException as error:
4141
self.is_valid = False
42-
logging.error(str(error))
42+
self.logger = SimpleLogger()
43+
self.logger.log(enums.LogLevels.ERROR, str(error))
4344
return
4445

4546
try:
4647
self.config = project_config.ProjectConfig(datafile, self.logger, self.error_handler)
4748
except:
4849
self.is_valid = False
4950
self.config = None
50-
logging.error(enums.Errors.INVALID_INPUT_ERROR.format('datafile'))
51+
self.logger = SimpleLogger()
52+
self.logger.log(enums.LogLevels.ERROR, enums.Errors.INVALID_INPUT_ERROR.format('datafile'))
5153
return
5254

5355
self.bucketer = bucketer.Bucketer(self.config)
@@ -56,7 +58,8 @@ def __init__(self, datafile, event_dispatcher=None, logger=None, error_handler=N
5658
self.event_builder = event_builder.get_event_builder(self.config, self.bucketer)
5759
except:
5860
self.is_valid = False
59-
logging.error(enums.Errors.UNSUPPORTED_DATAFILE_VERSION)
61+
self.logger = SimpleLogger()
62+
self.logger.log(enums.LogLevels.ERROR, enums.Errors.UNSUPPORTED_DATAFILE_VERSION)
6063

6164
def _validate_inputs(self, datafile, skip_json_validation):
6265
""" Helper method to validate all input parameters.
@@ -128,7 +131,7 @@ def activate(self, experiment_key, user_id, attributes=None):
128131
"""
129132

130133
if not self.is_valid:
131-
logging.error(enums.Errors.INVALID_DATAFILE.format('activate'))
134+
self.logger.log(enums.LogLevels.ERROR, enums.Errors.INVALID_DATAFILE.format('activate'))
132135
return None
133136

134137
experiment = self.config.get_experiment_from_key(experiment_key)
@@ -171,7 +174,7 @@ def track(self, event_key, user_id, attributes=None, event_value=None):
171174
"""
172175

173176
if not self.is_valid:
174-
logging.error(enums.Errors.INVALID_DATAFILE.format('track'))
177+
self.logger.log(enums.LogLevels.ERROR, enums.Errors.INVALID_DATAFILE.format('track'))
175178
return
176179

177180
if attributes and not validator.are_attributes_valid(attributes):
@@ -224,7 +227,7 @@ def get_variation(self, experiment_key, user_id, attributes=None):
224227
"""
225228

226229
if not self.is_valid:
227-
logging.error(enums.Errors.INVALID_DATAFILE.format('get_variation'))
230+
self.logger.log(enums.LogLevels.ERROR, enums.Errors.INVALID_DATAFILE.format('get_variation'))
228231
return None
229232

230233
experiment = self.config.get_experiment_from_key(experiment_key)

tests/test_optimizely.py

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,43 +23,43 @@ def _validate_event_object(self, event_obj, expected_url, expected_params, expec
2323
def test_init__invalid_datafile__logs_error(self):
2424
""" Test that invalid datafile logs error on init. """
2525

26-
with mock.patch('logging.error') as mock_log_error:
26+
with mock.patch('optimizely.logger.SimpleLogger.log') as mock_logging:
2727
optimizely.Optimizely('invalid_datafile')
2828

29-
mock_log_error.assert_called_once_with('Provided "datafile" is in an invalid format.')
29+
mock_logging.assert_called_once_with(enums.LogLevels.ERROR, 'Provided "datafile" is in an invalid format.')
3030

3131
def test_init__invalid_event_dispatcher__logs_error(self):
3232
""" Test that invalid event_dispatcher logs error on init. """
3333

3434
class InvalidDispatcher(object):
3535
pass
3636

37-
with mock.patch('logging.error') as mock_log_error:
37+
with mock.patch('optimizely.logger.SimpleLogger.log') as mock_logging:
3838
optimizely.Optimizely(json.dumps(self.config_dict), event_dispatcher=InvalidDispatcher)
3939

40-
mock_log_error.assert_called_once_with('Provided "event_dispatcher" is in an invalid format.')
40+
mock_logging.assert_called_once_with(enums.LogLevels.ERROR, 'Provided "event_dispatcher" is in an invalid format.')
4141

4242
def test_init__invalid_logger__raises(self):
4343
""" Test that invalid logger logs error on init. """
4444

4545
class InvalidLogger(object):
4646
pass
4747

48-
with mock.patch('logging.error') as mock_log_error:
48+
with mock.patch('optimizely.logger.SimpleLogger.log') as mock_logging:
4949
optimizely.Optimizely(json.dumps(self.config_dict), logger=InvalidLogger)
5050

51-
mock_log_error.assert_called_once_with('Provided "logger" is in an invalid format.')
51+
mock_logging.assert_called_once_with(enums.LogLevels.ERROR, 'Provided "logger" is in an invalid format.')
5252

5353
def test_init__invalid_error_handler__raises(self):
5454
""" Test that invalid error_handler logs error on init. """
5555

5656
class InvalidErrorHandler(object):
5757
pass
5858

59-
with mock.patch('logging.error') as mock_log_error:
59+
with mock.patch('optimizely.logger.SimpleLogger.log') as mock_logging:
6060
optimizely.Optimizely(json.dumps(self.config_dict), error_handler=InvalidErrorHandler)
6161

62-
mock_log_error.assert_called_once_with('Provided "error_handler" is in an invalid format.')
62+
mock_logging.assert_called_once_with(enums.LogLevels.ERROR, 'Provided "error_handler" is in an invalid format.')
6363

6464
def test_skip_json_validation_true(self):
6565
""" Test that on setting skip_json_validation to true, JSON schema validation is not performed. """
@@ -73,23 +73,23 @@ def test_invalid_json_raises_schema_validation_off(self):
7373
""" Test that invalid JSON logs error if schema validation is turned off. """
7474

7575
# Not JSON
76-
with mock.patch('logging.error') as mock_log_error:
76+
with mock.patch('optimizely.logger.SimpleLogger.log') as mock_logging:
7777
optimizely.Optimizely('invalid_json', skip_json_validation=True)
7878

79-
mock_log_error.assert_called_once_with('Provided "datafile" is in an invalid format.')
79+
mock_logging.assert_called_once_with(enums.LogLevels.ERROR, 'Provided "datafile" is in an invalid format.')
8080

8181
# JSON, but missing version
82-
with mock.patch('logging.error') as mock_log_error:
82+
with mock.patch('optimizely.logger.SimpleLogger.log') as mock_logging:
8383
optimizely.Optimizely(json.dumps({'some_field': 'some_value'}), skip_json_validation=True)
8484

85-
mock_log_error.assert_called_once_with(enums.Errors.UNSUPPORTED_DATAFILE_VERSION)
85+
mock_logging.assert_called_once_with(enums.LogLevels.ERROR, enums.Errors.UNSUPPORTED_DATAFILE_VERSION)
8686

8787
# JSON having valid version, but entities have invalid format
88-
with mock.patch('logging.error') as mock_log_error:
88+
with mock.patch('optimizely.logger.SimpleLogger.log') as mock_logging:
8989
optimizely.Optimizely({'version': '2', 'events': 'invalid_value', 'experiments': 'invalid_value'},
9090
skip_json_validation=True)
9191

92-
mock_log_error.assert_called_once_with('Provided "datafile" is in an invalid format.')
92+
mock_logging.assert_called_once_with(enums.LogLevels.ERROR, 'Provided "datafile" is in an invalid format.')
9393

9494
def test_activate(self):
9595
""" Test that activate calls dispatch_event with right params and returns expected variation. """
@@ -213,10 +213,10 @@ def test_activate__invalid_object(self):
213213

214214
opt_obj = optimizely.Optimizely('invalid_file')
215215

216-
with mock.patch('logging.error') as mock_logging_error:
216+
with mock.patch('optimizely.logger.SimpleLogger.log') as mock_logging:
217217
self.assertIsNone(opt_obj.activate('test_experiment', 'test_user'))
218218

219-
mock_logging_error.assert_called_once_with('Datafile has invalid format. Failing "activate".')
219+
mock_logging.assert_called_once_with(enums.LogLevels.ERROR, 'Datafile has invalid format. Failing "activate".')
220220

221221
def test_track__with_attributes(self):
222222
""" Test that track calls dispatch_event with right params when attributes are provided. """
@@ -337,10 +337,10 @@ def test_track__invalid_object(self):
337337

338338
opt_obj = optimizely.Optimizely('invalid_file')
339339

340-
with mock.patch('logging.error') as mock_logging_error:
340+
with mock.patch('optimizely.logger.SimpleLogger.log') as mock_logging:
341341
opt_obj.track('test_event', 'test_user')
342342

343-
mock_logging_error.assert_called_once_with('Datafile has invalid format. Failing "track".')
343+
mock_logging.assert_called_once_with(enums.LogLevels.ERROR, 'Datafile has invalid format. Failing "track".')
344344

345345
def test_get_variation__audience_match_and_experiment_running(self):
346346
""" Test that get variation retrieves expected variation
@@ -409,10 +409,10 @@ def test_get_variation__invalid_object(self):
409409

410410
opt_obj = optimizely.Optimizely('invalid_file')
411411

412-
with mock.patch('logging.error') as mock_logging_error:
412+
with mock.patch('optimizely.logger.SimpleLogger.log') as mock_logging:
413413
self.assertIsNone(opt_obj.get_variation('test_experiment', 'test_user'))
414414

415-
mock_logging_error.assert_called_once_with('Datafile has invalid format. Failing "get_variation".')
415+
mock_logging.assert_called_once_with(enums.LogLevels.ERROR, 'Datafile has invalid format. Failing "get_variation".')
416416

417417
def test_custom_logger(self):
418418
""" Test creating Optimizely object with a custom logger. """
@@ -450,10 +450,10 @@ def _validate_event_object(self, event_obj, expected_url, expected_params, expec
450450
def test_init__invalid_datafile__raises(self):
451451
""" Test that invalid datafile raises Exception on init. """
452452

453-
with mock.patch('logging.error') as mock_log_error:
453+
with mock.patch('optimizely.logger.SimpleLogger.log') as mock_logging:
454454
optimizely.Optimizely('invalid_datafile')
455455

456-
mock_log_error.assert_called_once_with('Provided "datafile" is in an invalid format.')
456+
mock_logging.assert_called_once_with(enums.LogLevels.ERROR, 'Provided "datafile" is in an invalid format.')
457457

458458
def test_activate(self):
459459
""" Test that activate calls dispatch_event with right params and returns expected variation. """
@@ -598,10 +598,10 @@ def test_activate__invalid_object(self):
598598

599599
opt_obj = optimizely.Optimizely('invalid_file')
600600

601-
with mock.patch('logging.error') as mock_logging_error:
601+
with mock.patch('optimizely.logger.SimpleLogger.log') as mock_logging:
602602
self.assertIsNone(opt_obj.activate('test_experiment', 'test_user'))
603603

604-
mock_logging_error.assert_called_once_with('Datafile has invalid format. Failing "activate".')
604+
mock_logging.assert_called_once_with(enums.LogLevels.ERROR, 'Datafile has invalid format. Failing "activate".')
605605

606606
def test_track__with_attributes(self):
607607
""" Test that track calls dispatch_event with right params when attributes are provided. """
@@ -755,20 +755,20 @@ def test_track__invalid_object(self):
755755

756756
opt_obj = optimizely.Optimizely('invalid_file')
757757

758-
with mock.patch('logging.error') as mock_logging_error:
758+
with mock.patch('optimizely.logger.SimpleLogger.log') as mock_logging:
759759
opt_obj.track('test_event', 'test_user')
760760

761-
mock_logging_error.assert_called_once_with('Datafile has invalid format. Failing "track".')
761+
mock_logging.assert_called_once_with(enums.LogLevels.ERROR, 'Datafile has invalid format. Failing "track".')
762762

763763
def test_get_variation__invalid_object(self):
764764
""" Test that get_variation logs error if Optimizely object is not created correctly. """
765765

766766
opt_obj = optimizely.Optimizely('invalid_file')
767767

768-
with mock.patch('logging.error') as mock_logging_error:
768+
with mock.patch('optimizely.logger.SimpleLogger.log') as mock_logging:
769769
self.assertIsNone(opt_obj.get_variation('test_experiment', 'test_user'))
770770

771-
mock_logging_error.assert_called_once_with('Datafile has invalid format. Failing "get_variation".')
771+
mock_logging.assert_called_once_with(enums.LogLevels.ERROR, 'Datafile has invalid format. Failing "get_variation".')
772772

773773

774774
class OptimizelyWithExceptionTest(base.BaseTestV1):

0 commit comments

Comments
 (0)