添加日志文件过期刷新,防止日志丢失
This commit is contained in:
@@ -2,3 +2,4 @@ aiohttp~=3.9.0
|
|||||||
Brotli~=1.1.0
|
Brotli~=1.1.0
|
||||||
yarl~=1.9.3
|
yarl~=1.9.3
|
||||||
redis~=5.0.2
|
redis~=5.0.2
|
||||||
|
PyYAML~=6.0.1
|
||||||
|
|||||||
11
sample.py
11
sample.py
@@ -4,7 +4,6 @@ import http.cookies
|
|||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import random
|
|
||||||
import threading
|
import threading
|
||||||
from typing import *
|
from typing import *
|
||||||
|
|
||||||
@@ -33,6 +32,8 @@ session: Optional[aiohttp.ClientSession] = None
|
|||||||
|
|
||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
|
flush_files()
|
||||||
|
|
||||||
init_session()
|
init_session()
|
||||||
try:
|
try:
|
||||||
for room_id in TEST_ROOM_IDS:
|
for room_id in TEST_ROOM_IDS:
|
||||||
@@ -63,6 +64,12 @@ async def main():
|
|||||||
await session.close()
|
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():
|
def subscribe_redis():
|
||||||
p = r.pubsub()
|
p = r.pubsub()
|
||||||
p.psubscribe(**{f"__keyspace@0__:{room_list_key}": room_changed_handler})
|
p.psubscribe(**{f"__keyspace@0__:{room_list_key}": room_changed_handler})
|
||||||
@@ -114,9 +121,7 @@ def init_session():
|
|||||||
class JsonlLogger(blivedm.BaseHandler):
|
class JsonlLogger(blivedm.BaseHandler):
|
||||||
def _global_callback(self, client: ws_base.WebSocketClientBase, message: dict):
|
def _global_callback(self, client: ws_base.WebSocketClientBase, message: dict):
|
||||||
if client.room_id not in log_files:
|
if client.room_id not in log_files:
|
||||||
# call mkdirs
|
|
||||||
os.makedirs("logs", exist_ok=True)
|
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] = open(f"logs/{client.room_id}.jsonl", "a", encoding="utf-8")
|
||||||
|
|
||||||
log_files[client.room_id].write(f"{json.dumps(message, separators=(',', ':'))}\n")
|
log_files[client.room_id].write(f"{json.dumps(message, separators=(',', ':'))}\n")
|
||||||
|
|||||||
Reference in New Issue
Block a user