This repository has been archived by the owner on Oct 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.py
91 lines (69 loc) · 2.59 KB
/
main.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
84
85
86
87
88
89
90
91
# Future
from __future__ import annotations
# Standard Library
import asyncio
import contextlib
import logging
import logging.handlers
import os
import sys
from collections.abc import Generator
# Packages
import setproctitle
# My stuff
from core import bot, config
RESET = "\u001b[0m"
BOLD = "\u001b[1m"
UNDERLINE = "\u001b[4m"
REVERSE = "\u001b[7m"
BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = (f"\u001b[{30 + i}m" for i in range(8))
@contextlib.contextmanager
def logger() -> Generator[None, None, None]:
loggers: dict[str, logging.Logger] = {
# "discord": logging.getLogger("discord"),
# "bot": logging.getLogger("bot"),
# "extensions": logging.getLogger("extensions"),
# "utilities": logging.getLogger("utilities"),
"slate": logging.getLogger("slate"),
}
for name, log in loggers.items():
file_handler = logging.handlers.RotatingFileHandler(filename=f"logs/{name}.log", mode="w", backupCount=5, encoding="utf-8", maxBytes=2 ** 22)
log.addHandler(file_handler)
stream_handler = logging.StreamHandler()
log.addHandler(stream_handler)
if os.path.isfile(f"logs/{name}.log"):
file_handler.doRollover()
file_formatter = logging.Formatter(fmt="%(asctime)s [%(name) 30s] [%(filename) 20s] [%(levelname) 7s] %(message)s", datefmt="%I:%M:%S %p %d/%m/%Y")
file_handler.setFormatter(file_formatter)
stream_formatter = logging.Formatter(
fmt=f"{CYAN}%(asctime)s{RESET} {YELLOW}[%(name) 30s]{RESET} {GREEN}[%(filename) 20s]{RESET} {BOLD}{REVERSE}{MAGENTA}[%(levelname) 7s]{RESET} %(message)s\n",
datefmt="%I:%M:%S %p %d/%m/%Y",
)
stream_handler.setFormatter(stream_formatter)
# loggers["discord"].setLevel(logging.INFO)
# loggers["bot"].setLevel(logging.DEBUG)
# loggers["extensions"].setLevel(logging.DEBUG)
# loggers["utilities"].setLevel(logging.DEBUG)
loggers["slate"].setLevel(logging.DEBUG)
try:
yield
finally:
[log.handlers[0].close() for log in loggers.values()]
if __name__ == "__main__":
os.environ["JISHAKU_NO_UNDERSCORE"] = "True"
os.environ["JISHAKU_HIDE"] = "True"
os.environ["JISHAKU_NO_DM_TRACEBACK"] = "True"
setproctitle.setproctitle("Life")
try:
# Packages
import uvloop
if sys.platform != "win32":
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
except ImportError:
uvloop = None
else:
del uvloop
with logger():
life = bot.Life()
life.ipc.start()
life.run(config.TOKEN)