Files
zsglpt/services/client_log.py

32 lines
976 B
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from __future__ import annotations
from app_logger import LoggerAdapter
from app_security import escape_html
from services.runtime import get_socketio
from services.state import safe_add_log
from services.time_utils import get_beijing_now
def log_to_client(message, user_id=None, account_id=None):
"""发送日志到Web客户端(用户隔离) + 统一输出到 logger。"""
timestamp = get_beijing_now().strftime('%H:%M:%S')
log_data = {
'timestamp': timestamp,
'message': escape_html(str(message)) if message else '',
'account_id': account_id
}
if user_id:
safe_add_log(user_id, log_data)
get_socketio().emit('log', log_data, room=f'user_{user_id}')
ctx = {}
if user_id is not None:
ctx["user_id"] = user_id
if account_id:
ctx["account_id"] = account_id
LoggerAdapter("app", ctx).info(str(message) if message is not None else "")