diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..54251d1 Binary files /dev/null and b/.DS_Store differ diff --git a/README.md b/README.md index a24e64c..c08c995 100644 --- a/README.md +++ b/README.md @@ -167,6 +167,7 @@ logger.info('Warning', extra={'extra_key':'extra_value'}) #### Dynamic Extra Fields If you prefer, you can add extra fields to your logs dynamically, and not pre-defining them in the configuration. This way, you can allow different logs to have different extra fields. + See the following code example: ```python @@ -291,7 +292,7 @@ LOGGING = { Please note that if you are using `python 3.8`, it is preferred to use the `logging.config.dictConfig` method, as mentioned in [python's documentation](https://docs.python.org/3/library/logging.config.html#configuration-file-format). ## Release Notes -- 4.0.3 +- 4.1.0 - Add ability to dynamically attach extra fields to the logs. - Import opentelemetry logging dependency only if trace context is enabled and dependency is installed manually. - Updated `opentelemetry-instrumentation-logging==0.39b0` diff --git a/setup.py b/setup.py index 63b3731..5157705 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ from setuptools import setup, find_packages setup( name="logzio-python-handler", - version='4.0.3', + version='4.1.0', description="Logging handler to send logs to your Logz.io account with bulk SSL", keywords="logging handler logz.io bulk https", author="roiravhon", diff --git a/tests/test_add_context.py b/tests/test_add_context.py index 8ffffdb..d176857 100644 --- a/tests/test_add_context.py +++ b/tests/test_add_context.py @@ -28,7 +28,7 @@ class TestAddContext(TestCase): self.retries_no = 4 self.retry_timeout = 2 self.add_context = True - logging_configuration = { + self.logging_configuration = { "version": 1, "formatters": { "logzio": { @@ -59,14 +59,14 @@ class TestAddContext(TestCase): } } - logging.config.dictConfig(logging_configuration) + logging.config.dictConfig(self.logging_configuration) self.logger = logging.getLogger('test') for curr_file in _find("logzio-failures-*.txt", "."): os.remove(curr_file) def test_add_context(self): - + # Logging configuration of add_context default to True log_message = "this log should have a trace context" self.logger.info(log_message) time.sleep(self.logs_drain_timeout * 2) @@ -80,3 +80,22 @@ class TestAddContext(TestCase): self.assertTrue('otelServiceName' in log_dict) except AssertionError as err: print(err) + + def test_ignore_context(self): + # Set add_context to False and reconfigure the logger as it defaults to True + self.logging_configuration["handlers"]["LogzioHandler"]["add_context"] = False + logging.config.dictConfig(self.logging_configuration) + self.logger = logging.getLogger('test') + log_message = "this log should not have a trace context" + self.logger.info(log_message) + time.sleep(self.logs_drain_timeout * 2) + logs_list = self.logzio_listener.logs_list + for current_log in logs_list: + if log_message in current_log: + log_dict = json.loads(current_log) + try: + self.assertFalse('otelSpanID' in log_dict) + self.assertFalse('otelTraceID' in log_dict) + self.assertFalse('otelServiceName' in log_dict) + except AssertionError as err: + print(err) diff --git a/tests/test_extra_fields.py b/tests/test_extra_fields.py index 4a332a7..33e9c5b 100644 --- a/tests/test_extra_fields.py +++ b/tests/test_extra_fields.py @@ -77,7 +77,55 @@ class TestExtraFieldsFilter(TestCase): if log_message in current_log: log_dict = json.loads(current_log) try: - self.assertEqual(extra_fields, {**extra_fields,**log_dict}) + self.assertEqual(extra_fields, {**extra_fields, **log_dict}) + except AssertionError as err: + print(err) + + def test_remove_extra_fields(self): + extra_fields = {"foo": "bar"} + + self.logger.addFilter(ExtraFieldsLogFilter(extra=extra_fields)) + log_message = "this log should have a additional fields" + self.logger.info(log_message) + + self.logger.removeFilter(ExtraFieldsLogFilter(extra=extra_fields)) + unfiltered_log_message = "this log shouldn't have a additional fields" + self.logger.info(unfiltered_log_message) + + time.sleep(self.logs_drain_timeout * 2) + logs_list = self.logzio_listener.logs_list + for current_log in logs_list: + if unfiltered_log_message in current_log: + log_dict = json.loads(current_log) + try: + self.assertNotEqual(extra_fields, {**extra_fields, **log_dict}) + except AssertionError as err: + print(err) + + def test_add_multiple_extra_fields(self): + extra_fields = {"foo": "bar"} + self.logger.addFilter(ExtraFieldsLogFilter(extra=extra_fields)) + log_message = "this log should have additional fields" + self.logger.info(log_message) + + extra_fields = {"counter":1} + self.logger.addFilter(ExtraFieldsLogFilter(extra=extra_fields)) + filtered_log_message = "this log should have multiple additional fields" + self.logger.info(filtered_log_message) + + time.sleep(self.logs_drain_timeout * 2) + logs_list = self.logzio_listener.logs_list + for current_log in logs_list: + if log_message in current_log: + log_dict = json.loads(current_log) + try: + self.assertEqual(extra_fields, {**extra_fields, **log_dict}) + except AssertionError as err: + print(err) + elif filtered_log_message in current_log: + log_dict = json.loads(current_log) + try: + self.assertEqual(extra_fields, {**extra_fields, **log_dict}) except AssertionError as err: print(err)