-
Notifications
You must be signed in to change notification settings - Fork 1
/
launcher.py
83 lines (65 loc) · 2.26 KB
/
launcher.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
from __future__ import annotations
import asyncio
import contextlib
import logging
from logging.handlers import RotatingFileHandler
import click
import discord
from decouple import config
from bot import CustomBot
log_path = (config("LOG_PATH", cast=str))
try:
import uvloop # type: ignore
except ImportError:
pass
else:
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
class RemoveNoise(logging.Filter):
def __init__(self):
super().__init__(name='discord.state')
def filter(self, record: logging.LogRecord) -> bool:
return (
record.levelname != 'WARNING'
or 'referencing an unknown' not in record.msg
)
@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())
handler = RotatingFileHandler(filename=f"{log_path}/root.log", encoding='utf-8', mode='w', maxBytes=max_bytes,
backupCount=5)
handler.setLevel(logging.DEBUG)
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)
handler = RotatingFileHandler(filename=f"{log_path}/error.log", encoding='utf-8', mode='w', maxBytes=max_bytes,
backupCount=5)
handler.setLevel(logging.ERROR)
handler.setFormatter(fmt)
log.addHandler(handler)
yield
finally:
# __exit__
handlers = log.handlers[:]
for hdlr in handlers:
hdlr.close()
log.removeHandler(hdlr)
async def run_bot():
logging.getLogger()
async with CustomBot() as bot:
await bot.start()
@click.group(invoke_without_command=True, options_metavar='[options]')
@click.pass_context
def main(ctx):
if ctx.invoked_subcommand is None:
with setup_logging():
asyncio.run(run_bot())
if __name__ == '__main__':
main()