-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.py
40 lines (34 loc) · 1.16 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
import os
import logging
import colorlog
def setup_logger(
name: str, file: str = "discord.log", file_mode: str = "w", console: bool = True
) -> logging.Logger:
"""Set up a logger with colorlog and file handler."""
logger = logging.getLogger(name)
logger.setLevel(logging.DEBUG)
if console:
stream_handler = colorlog.StreamHandler()
stream_handler.setFormatter(
colorlog.ColoredFormatter(
"%(log_color)s%(message)s",
log_colors={
"DEBUG": "cyan",
"INFO": "green",
"WARNING": "yellow",
"ERROR": "red",
"CRITICAL": "bold_red",
},
)
)
logger.addHandler(stream_handler)
os.makedirs("logs", exist_ok=True)
file_handler = logging.FileHandler(f"logs/{file}", mode=file_mode, encoding="utf-8")
file_handler.setFormatter(
logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
)
logger.addHandler(file_handler)
return logger
def highlight(text):
"""Highlight in magenta."""
return f"\033[95m{text}\033[0m"