From db4c045876d88f63370e63f03039600677fb90a6 Mon Sep 17 00:00:00 2001 From: James Stevenson Date: Sun, 7 Sep 2025 19:49:06 -0400 Subject: [PATCH 1/3] feat: better logging --- .../{{cookiecutter.project_slug}}/logging.py | 16 ------------ .../{{cookiecutter.project_slug}}/utils.py | 25 +++++++++++++++++++ 2 files changed, 25 insertions(+), 16 deletions(-) delete mode 100644 python/{{cookiecutter.project_slug}}/src/{{cookiecutter.project_slug}}/logging.py create mode 100644 python/{{cookiecutter.project_slug}}/src/{{cookiecutter.project_slug}}/utils.py diff --git a/python/{{cookiecutter.project_slug}}/src/{{cookiecutter.project_slug}}/logging.py b/python/{{cookiecutter.project_slug}}/src/{{cookiecutter.project_slug}}/logging.py deleted file mode 100644 index 5fe9733..0000000 --- a/python/{{cookiecutter.project_slug}}/src/{{cookiecutter.project_slug}}/logging.py +++ /dev/null @@ -1,16 +0,0 @@ -"""Configure application logging.""" - -import logging - - -def initialize_logs(log_level: int = logging.DEBUG) -> None: - """Configure logging. - - :param log_level: app log level to set - """ - logging.basicConfig( - filename=f"{__package__}.log", - format="[%(asctime)s] - %(name)s - %(levelname)s : %(message)s", - ) - logger = logging.getLogger(__package__) - logger.setLevel(log_level) diff --git a/python/{{cookiecutter.project_slug}}/src/{{cookiecutter.project_slug}}/utils.py b/python/{{cookiecutter.project_slug}}/src/{{cookiecutter.project_slug}}/utils.py new file mode 100644 index 0000000..0da9d90 --- /dev/null +++ b/python/{{cookiecutter.project_slug}}/src/{{cookiecutter.project_slug}}/utils.py @@ -0,0 +1,25 @@ +"""Configure useful utilities.""" + +import logging +from logging.handlers import RotatingFileHandler + + +def initialize_logs(log_level: int = logging.DEBUG) -> None: + """Configure logging. + + :param log_level: app log level to set + """ + if log_level not in {10, 20, 30, 40, 50}: + msg = f"Unrecognized log level: {log_level}" + raise ValueError(msg) + root = logging.getLogger() + if root.handlers: + return + + root.setLevel(log_level) + formatter = logging.Formatter( + "[%(asctime)s] - %(name)s - %(levelname)s : %(message)s" + ) + fh = RotatingFileHandler(f"{__package__}.log", maxBytes=5_000_000, backupCount=3) + fh.setFormatter(formatter) + root.addHandler(fh) From 3e250163d0d91f46e66a0e607ed1e95b80ae71f6 Mon Sep 17 00:00:00 2001 From: James Stevenson Date: Mon, 8 Sep 2025 09:02:18 -0400 Subject: [PATCH 2/3] fix hook --- python/hooks/post_gen_project.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/hooks/post_gen_project.py b/python/hooks/post_gen_project.py index 60e5fa0..dfef79e 100644 --- a/python/hooks/post_gen_project.py +++ b/python/hooks/post_gen_project.py @@ -20,4 +20,4 @@ Path("src/{{ cookiecutter.project_slug }}/config.py").unlink() if (not {{ cookiecutter.add_fastapi }}) and (not {{ cookiecutter.add_cli }}): - Path("./src/{{ cookiecutter.project_slug }}/logging.py").unlink() + Path("./src/{{ cookiecutter.project_slug }}/utils.py").unlink() From 52f547712899d1db2e4ac3957f21ea81a7422f33 Mon Sep 17 00:00:00 2001 From: James Stevenson Date: Mon, 8 Sep 2025 21:34:11 -0400 Subject: [PATCH 3/3] dont guard log level --- .../src/{{cookiecutter.project_slug}}/utils.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/python/{{cookiecutter.project_slug}}/src/{{cookiecutter.project_slug}}/utils.py b/python/{{cookiecutter.project_slug}}/src/{{cookiecutter.project_slug}}/utils.py index 0da9d90..2adec92 100644 --- a/python/{{cookiecutter.project_slug}}/src/{{cookiecutter.project_slug}}/utils.py +++ b/python/{{cookiecutter.project_slug}}/src/{{cookiecutter.project_slug}}/utils.py @@ -9,9 +9,6 @@ def initialize_logs(log_level: int = logging.DEBUG) -> None: :param log_level: app log level to set """ - if log_level not in {10, 20, 30, 40, 50}: - msg = f"Unrecognized log level: {log_level}" - raise ValueError(msg) root = logging.getLogger() if root.handlers: return