diff --git a/python/hooks/post_gen_project.py b/python/hooks/post_gen_project.py index 3b8fa97..4c8e3d5 100644 --- a/python/hooks/post_gen_project.py +++ b/python/hooks/post_gen_project.py @@ -20,5 +20,5 @@ Path("src/{{ cookiecutter.project_slug }}/models.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() Path("src/{{ cookiecutter.project_slug }}/config.py").unlink() 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 511a035..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.INFO) -> 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..a38ca73 --- /dev/null +++ b/python/{{cookiecutter.project_slug}}/src/{{cookiecutter.project_slug}}/utils.py @@ -0,0 +1,22 @@ +"""Configure useful utilities.""" + +import logging +from logging.handlers import RotatingFileHandler + + +def initialize_logs(log_level: int = logging.INFO) -> None: + """Configure logging. + + :param log_level: app log level to set + """ + 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)