Skip to content

Commit db4c045

Browse files
committed
feat: better logging
1 parent 9a3ce7f commit db4c045

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

python/{{cookiecutter.project_slug}}/src/{{cookiecutter.project_slug}}/logging.py

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""Configure useful utilities."""
2+
3+
import logging
4+
from logging.handlers import RotatingFileHandler
5+
6+
7+
def initialize_logs(log_level: int = logging.DEBUG) -> None:
8+
"""Configure logging.
9+
10+
:param log_level: app log level to set
11+
"""
12+
if log_level not in {10, 20, 30, 40, 50}:
13+
msg = f"Unrecognized log level: {log_level}"
14+
raise ValueError(msg)
15+
root = logging.getLogger()
16+
if root.handlers:
17+
return
18+
19+
root.setLevel(log_level)
20+
formatter = logging.Formatter(
21+
"[%(asctime)s] - %(name)s - %(levelname)s : %(message)s"
22+
)
23+
fh = RotatingFileHandler(f"{__package__}.log", maxBytes=5_000_000, backupCount=3)
24+
fh.setFormatter(formatter)
25+
root.addHandler(fh)

0 commit comments

Comments
 (0)