Skip to content

Commit

Permalink
perfect(LoggerManager): 优化日志处理,开发模式日志不再单独存放、并且会将 Debug 级别日志写入文件
Browse files Browse the repository at this point in the history
  • Loading branch information
Akimio521 committed Dec 5, 2024
1 parent 6f998ca commit 30ab6df
Showing 1 changed file with 3 additions and 17 deletions.
20 changes: 3 additions & 17 deletions app/core/log.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import logging
from logging.handlers import RotatingFileHandler, TimedRotatingFileHandler
from logging.handlers import TimedRotatingFileHandler
from datetime import datetime
from pathlib import Path

Expand Down Expand Up @@ -82,20 +82,6 @@ def __get_log_filname(self) -> str:
return (self.log_dir / f"{current_date}.log").as_posix()


def get_filehandler(
log_dir: Path,
is_dev: bool = False,
encoding: str = "utf-8",
) -> logging.FileHandler:
"""
获取文件处理器
"""
if is_dev:
return logging.FileHandler(log_dir / "dev.log", encoding=encoding)
else:
return TRFileHandler(log_dir, encoding=encoding)


class LoggerManager:
"""
日志管理器
Expand Down Expand Up @@ -125,8 +111,8 @@ def __init__(self) -> None:
console_handler.setFormatter(console_formatter)
self.__logger.addHandler(console_handler)

file_handler = get_filehandler(settings.LOG_DIR, settings.DEBUG)
file_handler.setLevel(logging.INFO)
file_handler = TRFileHandler(log_dir=settings.LOG_DIR, encoding="utf-8")
file_handler.setLevel(level)
file_handler.setFormatter(file_formatter)
self.__logger.addHandler(file_handler)

Expand Down

0 comments on commit 30ab6df

Please sign in to comment.