From ee24b9d9d99dd1f2524deb9a245717e86ae92c28 Mon Sep 17 00:00:00 2001 From: Omar Eissa Date: Sun, 21 Jan 2024 09:40:02 +0200 Subject: [PATCH] Add LoggerFactory for structured logging Fix #10 Signed-off-by: Omar Eissa Signed-off-by: Omar Eissa --- src/alvarium/logging/factories.py | 26 ++++++++++++++++++++++++++ tests/example/__main__.py | 5 +++-- tests/test_sdk.py | 16 ++++++---------- 3 files changed, 35 insertions(+), 12 deletions(-) create mode 100644 src/alvarium/logging/factories.py diff --git a/src/alvarium/logging/factories.py b/src/alvarium/logging/factories.py new file mode 100644 index 0000000..72b9751 --- /dev/null +++ b/src/alvarium/logging/factories.py @@ -0,0 +1,26 @@ +import logging +import socket + + +class LoggerFactory: + """A factory that returns a specific implementation of a Logger""" + + def get_logger(self, name: str = __name__, level: int = logging.DEBUG) -> logging.Logger: + class LowerCaseLevelNameFormatter(logging.Formatter): + def format(self, record): + record.levelname = record.levelname.lower() + return super().format(record) + + logger = logging.getLogger(name) + logger.propagate = False + logging.basicConfig(level=level) + + handler = logging.StreamHandler() + formatter = LowerCaseLevelNameFormatter('{"timestamp":"%(asctime)s","log-level": "%(levelname)s","message":"%(message)s","hostname":"%(hostname)s", "application":"%(filename)s", "line-number":"%(filename)s:%(lineno)d"}', + datefmt='%Y-%m-%dT%H:%M:%SZ', + validate=True, + defaults={'hostname': socket.gethostname()} ) + handler.setFormatter(formatter) + logger.addHandler(handler) + + return logger \ No newline at end of file diff --git a/tests/example/__main__.py b/tests/example/__main__.py index a2e52b8..01d866e 100644 --- a/tests/example/__main__.py +++ b/tests/example/__main__.py @@ -2,6 +2,8 @@ import os import sys +from alvarium.logging.factories import LoggerFactory + PROJECT_PATH = os.getcwd() SOURCE_PATH = os.path.join( PROJECT_PATH,"src" @@ -26,8 +28,7 @@ sdk_info = SdkInfo.from_json(json.dumps(config["sdk"])) # construct logger -logger = logging.getLogger(__name__) -logging.basicConfig(level = logging.DEBUG) +logger = LoggerFactory().get_logger(name=__name__, level=logging.DEBUG) # construct annotators annotator_factory = AnnotatorFactory() diff --git a/tests/test_sdk.py b/tests/test_sdk.py index 32f88f6..a3d87a5 100644 --- a/tests/test_sdk.py +++ b/tests/test_sdk.py @@ -4,6 +4,7 @@ from alvarium.annotators.factories import AnnotatorFactory from alvarium.sdk import Sdk from alvarium.default import DefaultSdk +from alvarium.logging.factories import LoggerFactory class TestSdk(unittest.TestCase): @@ -17,8 +18,7 @@ def test_default_sdk_instantiate_should_not_raise(self): annotator_factory = AnnotatorFactory() annotators = [annotator_factory.get_annotator(kind=annotation_type, sdk_info=sdk_info) for annotation_type in sdk_info.annotators] - logger = logging.getLogger(__name__) - logging.basicConfig(level = logging.DEBUG) + logger = LoggerFactory().get_logger(name=__name__, level=logging.DEBUG) sdk: Sdk = DefaultSdk(annotators=annotators,config=sdk_info,logger=logger) sdk.close() @@ -28,8 +28,7 @@ def test_sdk_should_create(self): annotator_factory = AnnotatorFactory() annotators = [annotator_factory.get_annotator(kind=annotation_type, sdk_info=sdk_info) for annotation_type in sdk_info.annotators] - logger = logging.getLogger(__name__) - logging.basicConfig(level = logging.DEBUG) + logger = LoggerFactory().get_logger(name=__name__, level=logging.DEBUG) sdk = DefaultSdk(annotators=annotators, config=sdk_info, logger=logger) test_data = b'test' @@ -41,8 +40,7 @@ def test_sdk_should_mutate(self): annotator_factory = AnnotatorFactory() annotators = [annotator_factory.get_annotator(kind=annotation_type, sdk_info=sdk_info) for annotation_type in sdk_info.annotators] - logger = logging.getLogger(__name__) - logging.basicConfig(level = logging.DEBUG) + logger = LoggerFactory().get_logger(name=__name__, level=logging.DEBUG) sdk = DefaultSdk(annotators=annotators, config=sdk_info, logger=logger) old_data = b'old data' @@ -56,8 +54,7 @@ def test_sdk_should_transit(self) -> None: annotator_factory = AnnotatorFactory() annotators = [annotator_factory.get_annotator(kind=annotation_type, sdk_info=sdk_info) for annotation_type in sdk_info.annotators] - logger = logging.getLogger(__name__) - logging.basicConfig(level = logging.DEBUG) + logger = LoggerFactory().get_logger(name=__name__, level=logging.DEBUG) sdk = DefaultSdk(annotators=annotators, config=sdk_info, logger=logger) test_data = b'test' @@ -69,8 +66,7 @@ def test_sdk_should_create_published_annotations(self) -> None: annotator_factory = AnnotatorFactory() annotators = [annotator_factory.get_annotator(kind=annotation_type, sdk_info=sdk_info) for annotation_type in sdk_info.annotators] - logger = logging.getLogger(__name__) - logging.basicConfig(level = logging.DEBUG) + logger = LoggerFactory().get_logger(name=__name__, level=logging.DEBUG) sdk = DefaultSdk(annotators=annotators, config=sdk_info, logger=logger)