-
Notifications
You must be signed in to change notification settings - Fork 1
/
flexi.py
57 lines (46 loc) · 1.46 KB
/
flexi.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
from src.bot import bot
from logging.handlers import RotatingFileHandler
import logging
import discord
import contextlib
version = "0.1"
class RemoveNoise(logging.Filter):
def __init__(self):
super().__init__(name="discord.state")
def filter(self, record: logging.LogRecord) -> bool:
if record.levelname == "WARNING" and "referencing an unknown" in record.msg:
return False
return True
@contextlib.contextmanager
def setup_logging():
log = logging.getLogger()
try:
discord.utils.setup_logging()
# __enter__
max_bytes = 32 * 1024 * 1024 # 32 MiB
logging.getLogger("discord").setLevel(logging.INFO)
logging.getLogger("discord.http").setLevel(logging.WARNING)
logging.getLogger("discord.state").addFilter(RemoveNoise())
log.setLevel(logging.INFO)
handler = RotatingFileHandler(
filename="flexi.log",
encoding="utf-8",
mode="w",
maxBytes=max_bytes,
backupCount=5,
)
dt_fmt = "%Y-%m-%d %H:%M:%S"
fmt = logging.Formatter(
"[{asctime}] [{levelname:<7}] {name}: {message}", dt_fmt, style="{"
)
handler.setFormatter(fmt)
log.addHandler(handler)
yield
finally:
# __exit__
handlers = log.handlers[:]
for hdlr in handlers:
hdlr.close()
log.removeHandler(hdlr)
with setup_logging():
bot.run(version)