-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlogging_util.py
40 lines (32 loc) · 1.02 KB
/
logging_util.py
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
import logging
import coloredlogs
def setup_logger(name):
"""
Setup a logger with the specified name.
:param name: Name of the logger.
:return: Configured logger.
"""
logger = logging.getLogger(name)
logger.setLevel(logging.DEBUG)
# Create handlers
console_handler = logging.StreamHandler()
file_handler = logging.FileHandler(f"{name}.log")
# Set log level for handlers
console_handler.setLevel(logging.DEBUG)
file_handler.setLevel(logging.DEBUG)
# Create formatters and add them to handlers
formatter = logging.Formatter(
"%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
console_handler.setFormatter(formatter)
file_handler.setFormatter(formatter)
# Add handlers to the logger
logger.addHandler(console_handler)
logger.addHandler(file_handler)
# Setup colored logs
coloredlogs.install(
level="DEBUG",
logger=logger,
fmt="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
)
return logger