Skip to content

Commit 3dd5199

Browse files
committed
Add logging configuration presets
1 parent 008496a commit 3dd5199

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

django_utils_lib/logging_utils.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from os import get_terminal_size
2-
from typing import List, Union
2+
from typing import Any, Dict, List, Literal, TypedDict, Union
33

44

55
def build_heading_block(heading: Union[str, List[str]], border_width=2) -> str:
@@ -26,3 +26,40 @@ def build_heading_block(heading: Union[str, List[str]], border_width=2) -> str:
2626
heading_lines.append(heading_delim)
2727

2828
return "\n".join(heading_lines)
29+
30+
31+
LoggingFormatterConfig = TypedDict(
32+
"LoggingFormatterConfig",
33+
{"class": str, "format": str, "datefmt": str, "style": Literal["%", "{", "$"]},
34+
total=False,
35+
)
36+
37+
LoggingFormatterPreset = Literal["with_line_numbers", "no_line_numbers"]
38+
LoggingHandlerPreset = Literal["console", "console_no_line_number"]
39+
40+
41+
class LoggingPresets:
42+
"""
43+
These presets can be useful for working the Python standard `logging` library, as well as Django's
44+
logging configuration, which wraps the standard implementation.
45+
46+
For more details, see:
47+
48+
- [Python Docs - logging.config](https://docs.python.org/3/library/logging.config.html)
49+
- [Django Docs - Logging](https://docs.djangoproject.com/en/5.1/topics/logging/)
50+
"""
51+
52+
formatters: Dict[LoggingFormatterPreset, LoggingFormatterConfig] = {
53+
"with_line_numbers": {
54+
"format": "[%(name)s] [%(asctime)s] %(levelname)s - [%(filename)s:%(lineno)s] %(message)s",
55+
"datefmt": "%Y-%m-%d %H:%M:%S",
56+
},
57+
"no_line_numbers": {
58+
"format": "[%(name)s] [%(asctime)s] %(levelname)s - %(message)s",
59+
"datefmt": "%Y-%m-%d %H:%M:%S",
60+
},
61+
}
62+
handlers: Dict[LoggingHandlerPreset, Dict[str, Any]] = {
63+
"console": {"class": "logging.StreamHandler", "formatter": "with_line_numbers"},
64+
"console_no_line_number": {"class": "logging.StreamHandler", "formatter": "no_line_numbers"},
65+
}

0 commit comments

Comments
 (0)