forked from project-alvarium/alvarium-sdk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add LoggerFactory for structured logging
Fix project-alvarium#10 Signed-off-by: Omar Eissa <[email protected]> Signed-off-by: Omar Eissa <[email protected]>
- Loading branch information
Showing
5 changed files
with
52 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"python.testing.unittestArgs": [ | ||
"-v", | ||
"-s", | ||
"./tests", | ||
"-p", | ||
"test_*.py" | ||
], | ||
"python.testing.pytestEnabled": false, | ||
"python.testing.unittestEnabled": true, | ||
"python.testing.pytestArgs": [ | ||
"tests" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters