forked from fsr5-fhaachen/DC-Universe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
63 lines (47 loc) · 1.7 KB
/
run.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
import os
import logging
import contextlib
from dotenv import load_dotenv
from logging.handlers import RotatingFileHandler
from bot import DCUniverseBot
load_dotenv()
@contextlib.contextmanager
def setup_logging():
try:
# __enter__
if os.getenv('LOGGING').lower() == 'true':
if os.getenv('DEBUG').lower() == 'true':
logging.getLogger('discord').setLevel(logging.DEBUG)
logging.getLogger('discord.http').setLevel(logging.DEBUG)
else:
logging.getLogger('discord').setLevel(logging.INFO)
logging.getLogger('discord.http').setLevel(logging.WARNING)
log = logging.getLogger()
log.setLevel(logging.INFO)
handler = RotatingFileHandler(
filename='logs/log.log', encoding='utf-8', mode='w', maxBytes=32 * 1024 * 1024, backupCount=5)
formatter = logging.Formatter(
'[{asctime}] [{levelname:<7}] {name}: {message}', '%Y-%m-%d %H:%M:%S', style='{')
handler.setFormatter(formatter)
log.addHandler(handler)
if os.getenv('DEBUG').lower() == 'true':
log.info('Logger successfully set up in debug mode')
else:
log.info('Logger successfully set up in normal mode')
yield
finally:
# __exit__
if os.getenv('LOGGING').lower() == 'true':
handlers = log.handlers[:]
for handler in handlers:
handler.close()
log.removeHandler(handler)
def run_bot():
# run bot
bot = DCUniverseBot()
bot.run()
def main():
with setup_logging():
run_bot()
if __name__ == '__main__':
main()