- 修复日志滚动问题

This commit is contained in:
jxxghp
2025-08-06 16:32:54 +08:00
parent e2367103a1
commit 099d7874d7
+9 -14
View File
@@ -122,7 +122,7 @@ class NonBlockingFileHandler:
""" """
_instance = None _instance = None
_lock = threading.Lock() _lock = threading.Lock()
_rotating_handlers = {} # 缓存RotatingFileHandler实例 _rotating_handlers = {}
def __new__(cls): def __new__(cls):
if cls._instance is None: if cls._instance is None:
@@ -213,19 +213,16 @@ class NonBlockingFileHandler:
# 获取RotatingFileHandler实例 # 获取RotatingFileHandler实例
handler = NonBlockingFileHandler()._get_rotating_handler(entry.file_path) handler = NonBlockingFileHandler()._get_rotating_handler(entry.file_path)
# 格式化时间戳 # 使用RotatingFileHandler的emit方法,只传递原始消息
timestamp = entry.timestamp.strftime('%Y-%m-%d %H:%M:%S,') + f"{entry.timestamp.microsecond // 1000:03d}"
line = f"{entry.level.upper()}{timestamp} - {entry.message}\n"
# 使用RotatingFileHandler的emit方法
handler.emit(logging.LogRecord( handler.emit(logging.LogRecord(
name='', name='',
level=getattr(logging, entry.level.upper(), logging.INFO), level=getattr(logging, entry.level.upper(), logging.INFO),
pathname='', pathname='',
lineno=0, lineno=0,
msg=line, msg=entry.message,
args=(), args=(),
exc_info=None exc_info=None,
created=entry.timestamp.timestamp()
)) ))
except Exception as e: except Exception as e:
# 如果文件写入失败,至少输出到控制台 # 如果文件写入失败,至少输出到控制台
@@ -276,18 +273,16 @@ class NonBlockingFileHandler:
# 批量写入 # 批量写入
for entry in entries: for entry in entries:
timestamp = entry.timestamp.strftime( # 使用RotatingFileHandler的emit方法,只传递原始消息
'%Y-%m-%d %H:%M:%S,') + f"{entry.timestamp.microsecond // 1000:03d}"
line = f"{entry.level.upper()}{timestamp} - {entry.message}\n"
# 使用RotatingFileHandler的emit方法
handler.emit(logging.LogRecord( handler.emit(logging.LogRecord(
name='', name='',
level=getattr(logging, entry.level.upper(), logging.INFO), level=getattr(logging, entry.level.upper(), logging.INFO),
pathname='', pathname='',
lineno=0, lineno=0,
msg=line, msg=entry.message,
args=(), args=(),
exc_info=None exc_info=None,
created=entry.timestamp.timestamp()
)) ))
except Exception as e: except Exception as e:
print(f"批量写入失败 {file_path}: {e}") print(f"批量写入失败 {file_path}: {e}")