Skip to content

Commit

Permalink
修bug:日志文件大小检查和文件轮转功能失效
Browse files Browse the repository at this point in the history
  • Loading branch information
hiroi-sora committed Oct 9, 2024
1 parent 17ce07a commit c558b00
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions UmiOCR-data/py_src/imports/umi_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,14 @@
import sys
import json
import logging
from threading import Lock
from datetime import datetime
from logging.handlers import RotatingFileHandler
from logging import LogRecord

# 日志目录路径
Logs_Dir = "./logs"
Logs_Dir = os.path.abspath(Logs_Dir)


# 覆盖过滤器
class _CoverFilter(logging.Filter):
Expand Down Expand Up @@ -112,6 +115,9 @@ def _record_to_dict(self, record: LogRecord):

# 发送日志
def emit(self, record: LogRecord):
# 检查文件大小并进行轮转
if self.shouldRollover(record):
self.doRollover()
# 日志信息转字典
try:
log_dict = self._record_to_dict(record)
Expand All @@ -128,12 +134,6 @@ def emit(self, record: LogRecord):

# 日志记录器 管理类
class _LogManager:

# 日志目录路径
_log_dir = "./logs"
# 确保线程安全的锁
_lock = Lock()

@staticmethod # 控制台处理器
def _get_console_handler():
# 显式规定输出到 stderr ,避免干涉命令行使用
Expand All @@ -147,12 +147,12 @@ def _get_console_handler():
@staticmethod # json处理器,输出到本地文件及UI
def _get_json_handler():
# 确保日志目录存在
if not os.path.exists(_LogManager._log_dir):
os.makedirs(_LogManager._log_dir)
if not os.path.exists(Logs_Dir):
os.makedirs(Logs_Dir)
# 获取当前日期
current_date = datetime.now().strftime("%Y-%m-%d")
# 构造错误日志文件路径
log_file = os.path.join(_LogManager._log_dir, f"{current_date}.jsonl.txt")
log_file = os.path.join(Logs_Dir, f"log_{current_date}.jsonl.txt")
# 创建json处理器
json_handler = _JsonRotatingFileHandler(
log_file,
Expand Down

0 comments on commit c558b00

Please sign in to comment.