forked from Zero6992/chatGPT-discord-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.py
65 lines (56 loc) · 2.14 KB
/
log.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import os
import logging
import logging.handlers
class CustomFormatter(logging.Formatter):
LEVEL_COLORS = [
(logging.DEBUG, '\x1b[40;1m'),
(logging.INFO, '\x1b[34;1m'),
(logging.WARNING, '\x1b[33;1m'),
(logging.ERROR, '\x1b[31m'),
(logging.CRITICAL, '\x1b[41m'),
]
FORMATS = {
level: logging.Formatter(
f'\x1b[30;1m%(asctime)s\x1b[0m {color}%(levelname)-8s\x1b[0m \x1b[35m%(name)s\x1b[0m -> %(message)s',
'%Y-%m-%d %H:%M:%S'
)
for level, color in LEVEL_COLORS
}
def format(self, record):
formatter = self.FORMATS.get(record.levelno)
if formatter is None:
formatter = self.FORMATS[logging.DEBUG]
# Override the traceback to always print in red
if record.exc_info:
text = formatter.formatException(record.exc_info)
record.exc_text = f'\x1b[31m{text}\x1b[0m'
output = formatter.format(record)
# Remove the cache layer
record.exc_text = None
return output
def setup_logger(module_name:str) -> logging.Logger:
# create logger
library, _, _ = module_name.partition('.py')
logger = logging.getLogger(library)
logger.setLevel(logging.INFO)
# create console handler
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.INFO)
console_handler.setFormatter(CustomFormatter())
# Add console handler to logger
logger.addHandler(console_handler)
if os.getenv("LOGGING")=="True": #Check if logging is enabled
# specify that the log file path is the same as `main.py` file path
grandparent_dir = os.path.abspath(__file__ + "/../../")
log_name='chatgpt_discord_bot.log'
log_path = os.path.join(grandparent_dir, log_name)
# create local log handler
log_handler = logging.handlers.RotatingFileHandler(
filename=log_path,
encoding='utf-8',
maxBytes=32 * 1024 * 1024, # 32 MiB
backupCount=2, # Rotate through 5 files
)
log_handler.setFormatter(CustomFormatter())
logger.addHandler(log_handler)
return logger