like `logging.TimedRotatingFileHandler`, but this class support multi-process
| 252 | |
| 253 | |
| 254 | class DailyRotatingFileHandler(BaseRotatingHandler): |
| 255 | """ |
| 256 | like `logging.TimedRotatingFileHandler`, but this class support multi-process |
| 257 | """ |
| 258 | |
| 259 | def __init__( |
| 260 | self, |
| 261 | filename, |
| 262 | backupCount=0, |
| 263 | encoding="utf-8", |
| 264 | delay=False, |
| 265 | utc=False, |
| 266 | **kwargs, |
| 267 | ): |
| 268 | """ |
| 269 | 初始化 RotatingFileHandler 对象。 |
| 270 | |
| 271 | Args: |
| 272 | filename (str): 日志文件的路径,可以是相对路径或绝对路径。 |
| 273 | backupCount (int, optional, default=0): 保存的备份文件数量,默认为 0,表示不保存备份文件。 |
| 274 | encoding (str, optional, default='utf-8'): 编码格式,默认为 'utf-8'。 |
| 275 | delay (bool, optional, default=False): 是否延迟写入,默认为 False,表示立即写入。 |
| 276 | utc (bool, optional, default=False): 是否使用 UTC 时区,默认为 False,表示不使用 UTC 时区。 |
| 277 | kwargs (dict, optional): 其他参数将被传递给 BaseRotatingHandler 类的 init 方法。 |
| 278 | |
| 279 | Raises: |
| 280 | TypeError: 如果 filename 不是 str 类型。 |
| 281 | ValueError: 如果 backupCount 小于等于 0。 |
| 282 | """ |
| 283 | self.backup_count = backupCount |
| 284 | self.utc = utc |
| 285 | self.suffix = "%Y-%m-%d" |
| 286 | self.base_log_path = Path(filename) |
| 287 | self.base_filename = self.base_log_path.name |
| 288 | self.current_filename = self._compute_fn() |
| 289 | self.current_log_path = self.base_log_path.with_name(self.current_filename) |
| 290 | BaseRotatingHandler.__init__(self, filename, "a", encoding, delay) |
| 291 | |
| 292 | def shouldRollover(self, record): |
| 293 | """ |
| 294 | check scroll through the log |
| 295 | """ |
| 296 | if self.current_filename != self._compute_fn(): |
| 297 | return True |
| 298 | return False |
| 299 | |
| 300 | def doRollover(self): |
| 301 | """ |
| 302 | scroll log |
| 303 | """ |
| 304 | if self.stream: |
| 305 | self.stream.close() |
| 306 | self.stream = None |
| 307 | |
| 308 | self.current_filename = self._compute_fn() |
| 309 | self.current_log_path = self.base_log_path.with_name(self.current_filename) |
| 310 | |
| 311 | if not self.delay: |
no outgoing calls