@@ -64,7 +64,9 @@ def get_console_handler():
6464class TimestampedFileHandler (logging .Handler ):
6565 """基于时间戳的文件处理器,避免重命名操作"""
6666
67- def __init__ (self , log_dir , max_bytes = 10 * 1024 * 1024 , backup_count = 5 , encoding = 'utf-8' , compress = True , compress_level = 6 ):
67+ def __init__ (
68+ self , log_dir , max_bytes = 10 * 1024 * 1024 , backup_count = 5 , encoding = "utf-8" , compress = True , compress_level = 6
69+ ):
6870 super ().__init__ ()
6971 self .log_dir = Path (log_dir )
7072 self .log_dir .mkdir (exist_ok = True )
@@ -74,7 +76,7 @@ def __init__(self, log_dir, max_bytes=10*1024*1024, backup_count=5, encoding='ut
7476 self .compress = compress
7577 self .compress_level = compress_level
7678 self ._lock = threading .Lock ()
77-
79+
7880 # 当前活跃的日志文件
7981 self .current_file = None
8082 self .current_stream = None
@@ -84,7 +86,7 @@ def _init_current_file(self):
8486 """初始化当前日志文件"""
8587 timestamp = datetime .now ().strftime ("%Y%m%d_%H%M%S" )
8688 self .current_file = self .log_dir / f"app_{ timestamp } .log.jsonl"
87- self .current_stream = open (self .current_file , 'a' , encoding = self .encoding )
89+ self .current_stream = open (self .current_file , "a" , encoding = self .encoding )
8890
8991 def _should_rollover (self ):
9092 """检查是否需要轮转"""
@@ -96,14 +98,14 @@ def _do_rollover(self):
9698 """执行轮转:关闭当前文件,创建新文件"""
9799 if self .current_stream :
98100 self .current_stream .close ()
99-
101+
100102 # 压缩旧文件
101103 if self .compress and self .current_file :
102104 threading .Thread (target = self ._compress_file , args = (self .current_file ,), daemon = True ).start ()
103-
105+
104106 # 清理旧文件
105107 self ._cleanup_old_files ()
106-
108+
107109 # 创建新文件
108110 self ._init_current_file ()
109111
0 commit comments