-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
83 additions
and
0 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,33 @@ | ||
import logging | ||
import io | ||
|
||
from typing import Tuple | ||
|
||
default_fmt = "%(asctime)s | %(funcName)s | %(levelname)s | %(message)s" | ||
default_date_fmt = "%Y-%m-%d %H:%M:%S" | ||
|
||
|
||
def get_logger( | ||
fmt: str = default_fmt, datefmt: str = default_date_fmt | ||
) -> Tuple[logging.Logger, io.StringIO]: | ||
""" | ||
returns a logger object and an io stream of the data that is logged | ||
""" | ||
|
||
log = logging.getLogger("root") | ||
log.setLevel(logging.DEBUG) | ||
|
||
log_stringio = io.StringIO() | ||
handler = logging.StreamHandler(log_stringio) | ||
|
||
log_formatter = logging.Formatter(fmt=fmt, datefmt=datefmt) | ||
handler.setFormatter(log_formatter) | ||
log.addHandler(handler) | ||
|
||
# Add console output | ||
console = logging.StreamHandler() | ||
console.setLevel(logging.INFO) | ||
console.setFormatter(log_formatter) | ||
log.addHandler(console) | ||
|
||
return log, log_stringio |
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,50 @@ | ||
import json | ||
import re | ||
|
||
from dataengineeringutils3.logging import get_logger | ||
|
||
|
||
def test_output(): | ||
""" | ||
ensures the log ouput is as expected (including context filter) | ||
""" | ||
|
||
# get the logger and the IO stream | ||
logger, logger_io_stream = get_logger() | ||
|
||
# log and retrieve a message | ||
log_message = "a message!" | ||
logger.info(log_message) | ||
a = logger_io_stream.getvalue().strip() | ||
|
||
# ensure it matches the required pattern | ||
regex = re.compile( | ||
r"^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} \| " | ||
f"test_output | INFO | {log_message}$" | ||
) | ||
|
||
assert regex.match(a) | ||
|
||
|
||
def test_diff_fmt(): | ||
""" | ||
makes sure different formats work correctly | ||
""" | ||
|
||
# get the logger and the IO stream | ||
logger, logger_io_stream = get_logger( | ||
fmt="%(asctime)s | %(module)s %(table)s | %(levelname)s | %(message)s" | ||
) | ||
|
||
# log and retrieve a message | ||
log_message = "a message!" | ||
logger.info(log_message, extra={"table": "a_very_nice_table"}) | ||
a = logger_io_stream.getvalue() | ||
|
||
# ensure it matches the required pattern | ||
regex = re.compile( | ||
r"^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} \| " | ||
f"{__name__.split('.')[1]} a_very_nice_table | INFO | {log_message}$" | ||
) | ||
|
||
assert regex.match(a) |