From a7c0a58c9a4444e2b61b6e556dd388473b3c7064 Mon Sep 17 00:00:00 2001 From: Richie Cahill Date: Fri, 31 Oct 2025 20:19:10 -0400 Subject: [PATCH] playing with logging --- python/testing/__init__.py | 1 + python/testing/logging/__init__.py | 1 + python/testing/logging/bar.py | 11 ++++++++ python/testing/logging/configure_logger.py | 16 +++++++++++ python/testing/logging/foo.py | 18 ++++++++++++ python/testing/logging/main.py | 33 ++++++++++++++++++++++ 6 files changed, 80 insertions(+) create mode 100644 python/testing/__init__.py create mode 100644 python/testing/logging/__init__.py create mode 100644 python/testing/logging/bar.py create mode 100644 python/testing/logging/configure_logger.py create mode 100644 python/testing/logging/foo.py create mode 100644 python/testing/logging/main.py diff --git a/python/testing/__init__.py b/python/testing/__init__.py new file mode 100644 index 0000000..525291c --- /dev/null +++ b/python/testing/__init__.py @@ -0,0 +1 @@ +"""init.""" diff --git a/python/testing/logging/__init__.py b/python/testing/logging/__init__.py new file mode 100644 index 0000000..525291c --- /dev/null +++ b/python/testing/logging/__init__.py @@ -0,0 +1 @@ +"""init.""" diff --git a/python/testing/logging/bar.py b/python/testing/logging/bar.py new file mode 100644 index 0000000..00c33d4 --- /dev/null +++ b/python/testing/logging/bar.py @@ -0,0 +1,11 @@ +"""Bar.""" + +import logging + +logger = logging.getLogger(__name__) + + +def bar() -> None: + """Bar.""" + logger.debug(f"bar {__name__}") + logger.debug("bar") diff --git a/python/testing/logging/configure_logger.py b/python/testing/logging/configure_logger.py new file mode 100644 index 0000000..3932a58 --- /dev/null +++ b/python/testing/logging/configure_logger.py @@ -0,0 +1,16 @@ +import logging +import sys + + +def configure_logger(level: str = "INFO", test: str = None) -> None: + """Configure the logger. + Args: + level (str, optional): The logging level. Defaults to "INFO". + """ + logging.basicConfig( + level=level, + datefmt="%Y-%m-%dT%H:%M:%S%z", + format="%(asctime)s %(levelname)s %(filename)s:%(lineno)d - %(message)s" + f" {test}", + handlers=[logging.StreamHandler(sys.stdout)], + ) diff --git a/python/testing/logging/foo.py b/python/testing/logging/foo.py new file mode 100644 index 0000000..e8a7663 --- /dev/null +++ b/python/testing/logging/foo.py @@ -0,0 +1,18 @@ +"""foo""" + +import logging + +from python.testing.logging.bar import bar +from python.testing.logging.configure_logger import configure_logger + +logger = logging.getLogger(__name__) + + +def foo() -> None: + """Foo.""" + + configure_logger("DEBUG", "FOO") + logger.debug(f"foo {__name__}") + logger.debug("foo") + + bar() diff --git a/python/testing/logging/main.py b/python/testing/logging/main.py new file mode 100644 index 0000000..a723bbe --- /dev/null +++ b/python/testing/logging/main.py @@ -0,0 +1,33 @@ +"""main.""" + +import logging + +from python.testing.logging.configure_logger import configure_logger +from python.testing.logging.bar import bar +from python.testing.logging.foo import foo + +logger = logging.getLogger(__name__) + + +def main() -> None: + """Main.""" + configure_logger("DEBUG") + # handler = logging.StreamHandler() + + # Create and attach a formatter + # formatter = logging.Formatter( + # "%(asctime)s %(levelname)s %(filename)s:%(lineno)d - %(message)s FOO" + # ) + # handler.setFormatter(formatter) + + # Attach handler to logger + # foo_logger = logging.getLogger("python.testing.logging.foo") + # foo_logger.addHandler(handler) + # foo_logger.propagate = True + logger.debug("main") + foo() + bar() + + +if __name__ == "__main__": + main()