添加日志文件过期刷新,防止日志丢失

This commit is contained in:
tursom 2024-03-01 15:01:34 +08:00
parent c7eea9c6d9
commit e4b7e419e7
2 changed files with 9 additions and 3 deletions

View File

@ -2,3 +2,4 @@ aiohttp~=3.9.0
Brotli~=1.1.0
yarl~=1.9.3
redis~=5.0.2
PyYAML~=6.0.1

View File

@ -4,7 +4,6 @@ import http.cookies
import json
import logging
import os
import random
import threading
from typing import *
@ -33,6 +32,8 @@ session: Optional[aiohttp.ClientSession] = None
async def main():
flush_files()
init_session()
try:
for room_id in TEST_ROOM_IDS:
@ -63,6 +64,12 @@ async def main():
await session.close()
def flush_files():
for room_id in log_files:
log_files[room_id].flush()
threading.Timer(1, flush_files).start()
def subscribe_redis():
p = r.pubsub()
p.psubscribe(**{f"__keyspace@0__:{room_list_key}": room_changed_handler})
@ -114,9 +121,7 @@ def init_session():
class JsonlLogger(blivedm.BaseHandler):
def _global_callback(self, client: ws_base.WebSocketClientBase, message: dict):
if client.room_id not in log_files:
# call mkdirs
os.makedirs("logs", exist_ok=True)
log_files[client.room_id] = open(f"logs/{client.room_id}.jsonl", "a", encoding="utf-8")
log_files[client.room_id].write(f"{json.dumps(message, separators=(',', ':'))}\n")