Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add LoggerFactory for structured logging #11

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/alvarium/logging/factories.py
Original file line number Diff line number Diff line change
@@ -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
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.logging.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.logging.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