Skip to content

Commit

Permalink
Add LoggerFactory for structured logging
Browse files Browse the repository at this point in the history
Fix project-alvarium#10
Signed-off-by: Omar Eissa <[email protected]>
  • Loading branch information
o-eissa committed Jan 21, 2024
1 parent 8f9e800 commit 2791d24
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 13 deletions.
27 changes: 26 additions & 1 deletion src/alvarium/streams/factories.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import logging
import socket
from .exceptions import StreamException
from .mock import MockProvider
from .mqtt import MQTTStreamProvider
Expand All @@ -12,4 +14,27 @@ def get_provider(self, info: StreamInfo):
if info.type == StreamType.MQTT:
return MQTTStreamProvider(info.config)
else:
raise StreamException(f"{info.type} is not yet implemented.")
raise StreamException(f"{info.type} is not yet implemented.")

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
5 changes: 3 additions & 2 deletions tests/example/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import os
import sys

from alvarium.streams.factories import LoggerFactory

PROJECT_PATH = os.getcwd()
SOURCE_PATH = os.path.join(
PROJECT_PATH,"src"
Expand All @@ -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()
Expand Down
16 changes: 6 additions & 10 deletions tests/test_sdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from alvarium.annotators.factories import AnnotatorFactory
from alvarium.sdk import Sdk
from alvarium.default import DefaultSdk
from alvarium.streams.factories import LoggerFactory

class TestSdk(unittest.TestCase):

Expand All @@ -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()
Expand All @@ -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'
Expand All @@ -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'
Expand All @@ -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'
Expand All @@ -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)

Expand Down

0 comments on commit 2791d24

Please sign in to comment.