-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog_utils.py
More file actions
63 lines (53 loc) · 1.7 KB
/
log_utils.py
File metadata and controls
63 lines (53 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import logging
from settings import LOG_FILE, LOG_FILE_MODE
# Configure logging
logging.basicConfig(
filename= LOG_FILE,
filemode= LOG_FILE_MODE,
level= logging.INFO,
format='%(asctime)s - %(levelname)s [%(filename)s]: %(message)s',
# datefmt='%Y-%m-%d %H:%M:%S',
# handlers=[
# logging.FileHandler("app.log"), # Log to file
# logging.StreamHandler() # Log to console (stdout)
# ]
)
def log_info(
msg: str,
logger: logging.Logger | None = None,
) -> None:
"""
Logs an informational message and prints it to the console.
Args:
msg (str): The message to log.
logger (logging.Logger | None): Optional logger instance. Defaults to root logger.
"""
if logger is None:
logger = logging.getLogger()
logger.info(msg)
print(msg)
def log_warning(
msg: str,
logger: logging.Logger | None = None,
) -> None:
"""
Logs a warning message and prints it to the console with a 'WARNING:' prefix.
Args:
msg (str): The warning message to log.
logger (logging.Logger | None): Optional logger instance. Defaults to root logger.
"""
if logger is None:
logger = logging.getLogger()
logger.warning(msg)
print(f"WARNING: {msg}")
def log_debug(msg: str, logger: logging.Logger | None = None) -> None:
"""
Logs a debug message and prints it to the console with a 'DEBUG:' prefix.
Args:
msg (str): The debug message to log.
logger (logging.Logger | None): Optional logger instance. Defaults to root logger.
"""
if logger is None:
logger = logging.getLogger()
logger.debug(msg)
print(f"DEBUG: {msg}")