-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogging_configuration.py
More file actions
54 lines (42 loc) · 2.01 KB
/
logging_configuration.py
File metadata and controls
54 lines (42 loc) · 2.01 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
import logging, colorlog
import os
def configure_logging(log_file_name: str, logger_name: str = None):
"""
Configure a logger with console and file output.
Args:
log_file_name: Name of the log file (e.g., "compression_log.txt")
logger_name: Optional name for the logger. If None, uses log_file_name without extension.
Returns:
Configured logger instance
"""
if logger_name is None:
logger_name = log_file_name.replace(".txt", "")
logger = colorlog.getLogger(logger_name) # Create a logger object
logger.handlers.clear() # Avoid duplicate handlers on reload
logger.setLevel(logging.DEBUG)
logger.propagate = False
# Configure logging to output messages to the console with color formatting
handler = colorlog.StreamHandler()
handler.setLevel(logging.DEBUG)
handler.setFormatter(colorlog.ColoredFormatter(
"%(log_color)s%(levelname)s: %(message)s", # Customize the log message format
log_colors=
{ # Customize the colors for different log levels
"DEBUG": "blue",
"INFO": "green",
"WARNING": "yellow",
"ERROR": "red",
"CRITICAL": "red,bg_white",
}
))
os.makedirs("Logs", exist_ok=True) # Ensure Logs directory exists for file output
file_handler = logging.FileHandler(os.path.join("Logs", log_file_name),
mode="w", encoding="utf-8", delay=False) # Create a file handler to write log messages to a text file
file_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(logging.Formatter("%(asctime)s %(levelname)s: %(message)s", datefmt="%Y-%m-%d %H:%M:%S"))
logger.addHandler(handler)
logger.addHandler(file_handler)
return logger
# Create separate loggers for compression and decompression
compression_logger = configure_logging("compression_log.txt", "compression")
decompression_logger = configure_logging("decompression_log.txt", "decompression")