-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolor_logger.py
More file actions
100 lines (84 loc) · 3.67 KB
/
color_logger.py
File metadata and controls
100 lines (84 loc) · 3.67 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# color_logger.py
import logging
from logging.handlers import TimedRotatingFileHandler
from colorama import Fore, Style, init
import os
from datetime import datetime
# Initialize colorama
init(autoreset=True)
# Define custom log levels
SUCCESS_LEVEL_NUM = 25
GET_LEVEL_NUM = 15
UPDATE_LEVEL_NUM = 35
REMOVE_LEVEL_NUM = 45
# Add custom log levels
logging.addLevelName(SUCCESS_LEVEL_NUM, "SUCC")
logging.addLevelName(GET_LEVEL_NUM, "GET")
logging.addLevelName(UPDATE_LEVEL_NUM, "UPDATE")
logging.addLevelName(REMOVE_LEVEL_NUM, "REMOVE")
class ColorLogger:
def __init__(self, name=__name__, log_to_file=False, log_dir=os.path.join(os.path.dirname(os.path.abspath(__file__)), "logs")):
self.logger = logging.getLogger(name)
self.logger.setLevel(logging.DEBUG)
# Console handler
console_handler = logging.StreamHandler()
console_handler.setFormatter(self.CustomFormatter())
self.logger.addHandler(console_handler)
# File handler
if log_to_file:
if not os.path.exists(log_dir):
os.makedirs(log_dir)
log_filename = os.path.join(log_dir, f"{datetime.now().strftime('%Y-%m-%d')}.log")
file_handler = TimedRotatingFileHandler(log_filename, when='midnight', interval=1, backupCount=7)
file_handler.suffix = "%Y-%m-%d"
file_handler.setFormatter(logging.Formatter("%(asctime)s - %(levelname)s - %(message)s"))
self.logger.addHandler(file_handler)
# Add custom logging methods
self.logger.succ = self.succ
self.logger.get = self.get
self.logger.update = self.update
self.logger.remove = self.remove
# Define custom logging functions
def succ(self, message, *args, **kws):
if self.logger.isEnabledFor(SUCCESS_LEVEL_NUM):
self.logger._log(SUCCESS_LEVEL_NUM, message, args, **kws)
def get(self, message, *args, **kws):
if self.logger.isEnabledFor(GET_LEVEL_NUM):
self.logger._log(GET_LEVEL_NUM, message, args, **kws)
def update(self, message, *args, **kws):
if self.logger.isEnabledFor(UPDATE_LEVEL_NUM):
self.logger._log(UPDATE_LEVEL_NUM, message, args, **kws)
def remove(self, message, *args, **kws):
if self.logger.isEnabledFor(REMOVE_LEVEL_NUM):
self.logger._log(REMOVE_LEVEL_NUM, message, args, **kws)
class CustomFormatter(logging.Formatter):
# Define format string
format_str = "%(levelname)s: %(message)s"
# Define colors for different levels
LEVEL_COLORS = {
logging.DEBUG: Fore.CYAN,
logging.INFO: Fore.BLUE,
logging.WARNING: Fore.YELLOW,
logging.ERROR: Fore.RED,
logging.CRITICAL: Fore.MAGENTA,
SUCCESS_LEVEL_NUM: Fore.GREEN,
GET_LEVEL_NUM: Fore.WHITE,
UPDATE_LEVEL_NUM: Fore.GREEN,
REMOVE_LEVEL_NUM: Fore.RED,
}
def format(self, record):
levelname = record.levelname
level_color = self.LEVEL_COLORS.get(record.levelno, "")
format_str = f"[{level_color}%(levelname)s{Style.RESET_ALL}] \t%(message)s"
formatter = logging.Formatter(format_str)
return formatter.format(record)
# Example usage within the module itself, can be removed
if __name__ == "__main__":
logger = ColorLogger().logger
logger.debug("This is a debug message.")
logger.info("This is an info message.")
logger.warning("This is a warning message.")
logger.error("This is an error message.")
logger.critical("This is a critical message.")
logger.succ("This is a success message.")
logger.get("This is a get message.")