|
from loguru import logger |
|
import logging |
|
import sys |
|
import os |
|
|
|
def chat_log_filter(record): |
|
return "chat_msg" in record["extra"] |
|
|
|
def not_chat_log_filter(record): |
|
return "chat_msg" not in record["extra"] |
|
|
|
def formatter_with_clip(record): |
|
|
|
|
|
max_len = 12 |
|
record['function_x'] = record['function'].center(max_len) |
|
if len(record['function_x']) > max_len: |
|
record['function_x'] = ".." + record['function_x'][-(max_len-2):] |
|
record['line_x'] = str(record['line']).ljust(3) |
|
return '<green>{time:HH:mm}</green> | <cyan>{function_x}</cyan>:<cyan>{line_x}</cyan> | <level>{message}</level>\n' |
|
|
|
def setup_logging(PATH_LOGGING): |
|
|
|
admin_log_path = os.path.join(PATH_LOGGING, "admin") |
|
os.makedirs(admin_log_path, exist_ok=True) |
|
sensitive_log_path = os.path.join(admin_log_path, "chat_secrets.log") |
|
regular_log_path = os.path.join(admin_log_path, "console_log.log") |
|
logger.remove() |
|
logger.configure( |
|
levels=[dict(name="WARNING", color="<g>")], |
|
) |
|
|
|
logger.add( |
|
sys.stderr, |
|
format=formatter_with_clip, |
|
|
|
filter=(lambda record: not chat_log_filter(record)), |
|
colorize=True, |
|
enqueue=True |
|
) |
|
|
|
logger.add( |
|
sensitive_log_path, |
|
format='<green>{time:MM-DD HH:mm:ss}</green> | <level>{level: <8}</level> | <cyan>{name}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan> - <level>{message}</level>', |
|
rotation="10 MB", |
|
filter=chat_log_filter, |
|
enqueue=True, |
|
) |
|
|
|
logger.add( |
|
regular_log_path, |
|
format='<green>{time:MM-DD HH:mm:ss}</green> | <level>{level: <8}</level> | <cyan>{name}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan> - <level>{message}</level>', |
|
rotation="10 MB", |
|
filter=not_chat_log_filter, |
|
enqueue=True, |
|
) |
|
|
|
logging.getLogger("httpx").setLevel(logging.WARNING) |
|
|
|
logger.warning(f"所有对话记录将自动保存在本地目录{sensitive_log_path}, 请注意自我隐私保护哦!") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|