-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.py
41 lines (31 loc) · 1.2 KB
/
logger.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
41
import logging
import os
def setup_logger(log_filename):
"""
Setup logger for logging messages to both file and console.
Args:
log_filename (str): Name of the log file.
Returns:
logger (logging.Logger): Configured logger.
"""
# Create logger
logger = logging.getLogger(log_filename)
logger.setLevel(logging.DEBUG) # Log level can be adjusted
# Create a file handler to log to a file
log_path = os.path.join('logs', f"{log_filename}.log")
if not os.path.exists('logs'):
os.makedirs('logs')
file_handler = logging.FileHandler(log_path)
file_handler.setLevel(logging.DEBUG)
# Create a console handler to log to the console
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.INFO)
# Create a formatter for logs
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# Add the formatter to handlers
file_handler.setFormatter(formatter)
console_handler.setFormatter(formatter)
# Add the handlers to the logger
logger.addHandler(file_handler)
logger.addHandler(console_handler)
return logger