make logger not bad

This commit is contained in:
xeals 2023-03-27 23:07:10 +11:00
parent 5f9ae3e145
commit 693029fd21
Signed by: xeals
GPG Key ID: A498C7AF27EC6B5C

37
main.py
View File

@ -12,15 +12,28 @@ import sys
import typing as t import typing as t
class LogFormatter(logging.Formatter):
format = "%(name)s [%(levelname)s] %(message)s"
FORMATS = {
logging.DEBUG: f"\x1b[38;20m{format}\x1b[0m",
logging.INFO: f"\x1b[34;20m{format}\x1b[0m",
logging.WARNING: f"\x1b[33;20m{format}\x1b[0m",
logging.ERROR: f"\x1b[31;20m{format}\x1b[0m",
logging.CRITICAL: f"\x1b[31;1m{format}\x1b[0m",
}
def format(self, record):
fmt = self.FORMATS.get(record.levelno)
formatter = logging.Formatter(fmt)
return formatter.format(record)
APP = "hhirlstats" APP = "hhirlstats"
LOG = logging.getLogger(APP)
_ch = logging.StreamHandler()
def error(*args, **kwargs): _ch.setFormatter(LogFormatter())
logging.getLogger(APP).error(*args, **kwargs) LOG.addHandler(_ch)
def debug(*args, **kwargs):
logging.getLogger(APP).debug(*args, **kwargs)
class safelist(list): class safelist(list):
@ -125,7 +138,8 @@ def parse_log(game: str, log: str, into: sqlite3.Connection):
(game, turn, player, mon), (game, turn, player, mon),
) )
case _: case _:
debug(f"unhandled message {chunks[0]}") # LOG.debug(f"unhandled message {chunks[0]}")
pass
@dataclass(frozen=True) @dataclass(frozen=True)
@ -177,7 +191,7 @@ def main():
parser.add_argument("replay", nargs="+", help="replay ID or URL") parser.add_argument("replay", nargs="+", help="replay ID or URL")
args = parser.parse_args(sys.argv[1:]) args = parser.parse_args(sys.argv[1:])
logging.basicConfig(level=logging.DEBUG if args.verbose else logging.INFO) LOG.setLevel(logging.DEBUG if args.verbose else logging.INFO)
try: try:
db = sqlite3.connect("data.db") db = sqlite3.connect("data.db")
@ -187,9 +201,10 @@ def main():
try: try:
replay = fetch(r, cache=args.cache) replay = fetch(r, cache=args.cache)
except Exception as e: except Exception as e:
error(f"bad replay {r}") LOG.error(f"bad replay {r}")
continue continue
LOG.info(f"indexing game {replay.id}")
db.execute( db.execute(
""" """
INSERT INTO games(id, p1, p2, format, uploadtime) INSERT INTO games(id, p1, p2, format, uploadtime)