add chat logging

This commit is contained in:
Anthony Sottile 2020-05-20 13:47:03 -07:00
parent 3a67fe29ea
commit 53caebaf4e
2 changed files with 23 additions and 5 deletions

3
.gitignore vendored
View File

@ -1,4 +1,5 @@
/db.db
/.mypy_cache /.mypy_cache
/config.json /config.json
/db.db
/logs
/venv /venv

25
bot.py
View File

@ -597,11 +597,27 @@ def _shutdown(
closing_task.add_done_callback(cancel_tasks) closing_task.add_done_callback(cancel_tasks)
UNCOLOR_RE = re.compile(r'\033\[[^m]*m')
class LogWriter:
def __init__(self) -> None:
self.date = str(datetime.date.today())
def write_message(self, msg: str) -> None:
print(msg)
uncolored_msg = UNCOLOR_RE.sub('', msg)
os.makedirs('logs', exist_ok=True)
with open(os.path.join('logs', f'{self.date}.log'), 'a+') as f:
f.write(f'{uncolored_msg}\n')
async def handle_response( async def handle_response(
config: Config, config: Config,
match: Match[str], match: Match[str],
handler: Callable[[Match[str]], Response], handler: Callable[[Match[str]], Response],
writer: asyncio.StreamWriter, writer: asyncio.StreamWriter,
log_writer: LogWriter,
*, *,
quiet: bool, quiet: bool,
) -> None: ) -> None:
@ -617,7 +633,7 @@ async def handle_response(
send_match = SEND_MSG_RE.match(res) send_match = SEND_MSG_RE.match(res)
if send_match: if send_match:
color = '\033[1m\033[3m\033[38;5;21m' color = '\033[1m\033[3m\033[38;5;21m'
print( log_writer.write_message(
f'{dt_str()}' f'{dt_str()}'
f'<{color}{config.username}\033[m> ' f'<{color}{config.username}\033[m> '
f'{send_match[1]}', f'{send_match[1]}',
@ -626,6 +642,7 @@ async def handle_response(
async def amain(config: Config, *, quiet: bool) -> None: async def amain(config: Config, *, quiet: bool) -> None:
log_writer = LogWriter()
reader, writer = await asyncio.open_connection(HOST, PORT, ssl=True) reader, writer = await asyncio.open_connection(HOST, PORT, ssl=True)
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
@ -658,7 +675,7 @@ async def amain(config: Config, *, quiet: bool) -> None:
color_start = f'\033[1m\033[38;2;{r};{g};{b}m' color_start = f'\033[1m\033[38;2;{r};{g};{b}m'
if msg_match[3].startswith('\x01ACTION '): if msg_match[3].startswith('\x01ACTION '):
print( log_writer.write_message(
f'{dt_str()}' f'{dt_str()}'
f'{_badges(info["badges"])}' f'{_badges(info["badges"])}'
f'{color_start}\033[3m * {info["display-name"]}\033[22m ' f'{color_start}\033[3m * {info["display-name"]}\033[22m '
@ -670,7 +687,7 @@ async def amain(config: Config, *, quiet: bool) -> None:
else: else:
msg_s = msg_match[3] msg_s = msg_match[3]
print( log_writer.write_message(
f'{dt_str()}' f'{dt_str()}'
f'{_badges(info["badges"])}' f'{_badges(info["badges"])}'
f'<{color_start}{info["display-name"]}\033[m> ' f'<{color_start}{info["display-name"]}\033[m> '
@ -681,7 +698,7 @@ async def amain(config: Config, *, quiet: bool) -> None:
match = pattern.match(msg) match = pattern.match(msg)
if match: if match:
coro = handle_response( coro = handle_response(
config, match, handler, writer, quiet=quiet, config, match, handler, writer, log_writer, quiet=quiet,
) )
loop.create_task(coro) loop.create_task(coro)
break break