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

added logging module #32

Merged
merged 9 commits into from
Jan 6, 2022
Merged
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
33 changes: 33 additions & 0 deletions dataengineeringutils3/logging.py
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
49 changes: 49 additions & 0 deletions tests/test_logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
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)