Skip to content

Commit

Permalink
增加多进程日志支持
Browse files Browse the repository at this point in the history
  • Loading branch information
zhouhao10 committed Jan 2, 2025
1 parent 6fb10ec commit 4868155
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 24 deletions.
5 changes: 3 additions & 2 deletions docs/BasisModule/Trace/Debug.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ Appbuilder-SDK新增滚动日志功能
主要参数:
- console_output: 数据类型bool,默认值True,LOG日志是否在控制台输出
- loglevel: 数据类型str,默认值"DEBUG",LOG日志级别
- file_name: 数据类型str,默认值"tmp.log",LOG日志名称
- log_path: 数据类型str,默认值"/tmp",默认日志存放路径。
- file_name: 数据类型str,默认值为进程id,日志名前缀
- rotate_frequency: 数据类型str,默认值"MIDNIGHT",LOG日志滚动更新时间单位
- "S": 以秒为单位
- "M": 以分钟为单位
Expand All @@ -78,7 +79,7 @@ Appbuilder-SDK新增滚动日志功能
appbuilder.logger.setLogConfig(
console_output = False,
loglevel="DEBUG"
file_name="appbuilder.log",
log_path="/tmp",,
rotate_frequency="MIDNIGHT", # 每日凌晨更新
rotate_interval=1,
max_file_size=100 * 1024 *1024, # 最大日志大小为100MB
Expand Down
73 changes: 51 additions & 22 deletions python/utils/logger_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import logging.config
from threading import current_thread
from typing import Optional

LOGGING_CONFIG = {
"version": 1,
"disable_existing_loggers": False,
Expand Down Expand Up @@ -53,22 +53,22 @@
ERROR_FILE_HEADER = {
"level": "ERROR",
"class": "logging.FileHandler",
"filename": "error.tmp.log",
"filename": "tmp.error.log",
"formatter": "standard",
}

FILE_HEADER = {
"level": "DEBUG",
"class": "logging.FileHandler",
"filename": "tmp.log",
"filename": "tmp.info.log",
"formatter": "standard",
}

ERROR_SET_CONFIG_HEADER = {
'level': 'ERROR',
'formatter': 'standard',
'class': 'appbuilder.SizeAndTimeRotatingFileHandler',
'file_name': 'error.tmp.log',
'file_name': 'tmp.error.log',
'rotate_frequency': 'MIDDNIGHT',
'rotate_interval': 1,
'max_file_size': 5*1024*1024,
Expand All @@ -80,7 +80,7 @@
'level': 'DEBUG',
'formatter': 'standard',
'class': 'appbuilder.SizeAndTimeRotatingFileHandler',
'file_name': 'tmp.log',
'file_name': 'tmp.info.log',
'rotate_frequency': 'MIDDNIGHT',
'rotate_interval': 1,
'max_file_size': 5*1024*1024,
Expand All @@ -99,19 +99,37 @@ def __init__(self, logger, extra, loglevel):
LOGGING_CONFIG["handlers"] = {}
LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"] = []
log_file = os.environ.get("APPBUILDER_LOGFILE", "")
log_path = os.environ.get("APPBUILDER_LOGPATH", "")
loglevel = loglevel.strip().lower()
if loglevel not in ["debug", "info", "warning", "error"]:
raise ValueError("expected APPBUILDER_LOGLEVEL in [debug, info, warning, error], but got %s" % loglevel)
loglevel = loglevel.upper()
if log_file:
ERROR_FILE_HEADER["filename"] = _add_error_to_file_name(log_file)

if log_path:
current_pid = str(os.getpid())
full_log_path = os.path.join(log_path, "log")
if not os.path.exists(full_log_path):
os.makedirs(full_log_path)
info_log_file = os.path.join(log_path, "log", current_pid + ".info.log")
error_log_file = os.path.join(log_path, "log", current_pid + ".error.log")
FILE_HEADER["filename"] = info_log_file
ERROR_FILE_HEADER["filename"] = error_log_file
FILE_HEADER["level"] = loglevel
LOGGING_CONFIG["handlers"]["file"] = FILE_HEADER
LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"].append("file")
if loglevel in ("DEBUG", "INFO", "WARNING"):
LOGGING_CONFIG["handlers"]["error_file"] = ERROR_FILE_HEADER
LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"].append("error_file")
elif log_file:
ERROR_FILE_HEADER["filename"] = self._add_error_to_file_name(log_file)
FILE_HEADER["filename"] = log_file
FILE_HEADER["level"] = loglevel
LOGGING_CONFIG["handlers"]["file"] = FILE_HEADER
LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"].append("file")
if loglevel in ("DEBUG", "INFO", "WARNING"):
LOGGING_CONFIG["handlers"]["error_file"] = ERROR_FILE_HEADER
LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"].append("error_file")

CONSOLE_HEADER["level"] = loglevel
LOGGING_CONFIG["handlers"]["console"] = CONSOLE_HEADER
LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"].append("console")
Expand Down Expand Up @@ -148,6 +166,13 @@ def level(self):
"""
return self.logger.level

@staticmethod
def _add_error_to_file_name(filename):
prefix = "error."
dir_name, base_name = os.path.split(filename)
new_base_name = f"{prefix}{base_name}"
return os.path.join(dir_name, new_base_name)

def setFilename(self, filename):
"""
set filename
Expand All @@ -157,11 +182,11 @@ def setFilename(self, filename):
LOGGING_CONFIG["handlers"]["file"] = FILE_HEADER
LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"].append("file")
if "error_file" not in LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"]:
ERROR_FILE_HEADER["filename"] = _add_error_to_file_name(filename)
ERROR_FILE_HEADER["filename"] = self._add_error_to_file_name(filename)
LOGGING_CONFIG["handlers"]["error_file"] = ERROR_FILE_HEADER
LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"].append("error_file")
FILE_HEADER["filename"] = filename
ERROR_FILE_HEADER["filename"] = _add_error_to_file_name(filename)
ERROR_FILE_HEADER["filename"] = self._add_error_to_file_name(filename)
LOGGING_CONFIG["handlers"]["file"] = FILE_HEADER
LOGGING_CONFIG["handlers"]["error_file"] = ERROR_FILE_HEADER
logging.config.dictConfig(LOGGING_CONFIG)
Expand All @@ -186,16 +211,17 @@ def setLoglevel(self, level):
def setLogConfig(self,
console_output: bool = True,
loglevel: str = "DEBUG",
file_name: str = "tmp.log",
log_path: str = "/tmp",
rotate_frequency: str = "MIDNIGHT",
rotate_interval: int = 1,
max_file_size: Optional[int] = None, # 以B为单位
total_log_size: Optional[int] = None, # 以B为单位
max_log_files: Optional[int] = None
max_log_files: Optional[int] = None,
file_name: Optional[str] = None
):
LOGGING_CONFIG["handlers"] = {}
LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"] = []

# log_level 数据校验
log_level = loglevel.strip().lower()
if log_level not in ["debug", "info", "warning", "error"]:
Expand All @@ -209,7 +235,7 @@ def setLogConfig(self,
LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"].append("console")
else:
LOGGING_CONFIG["loggers"]["appbuilder"]["propagate"] = False

# 参数验证
if not max_file_size or max_file_size <= 0:
max_file_size = sys.maxsize
Expand All @@ -228,8 +254,17 @@ def setLogConfig(self,
SET_CONFIG_HEADER['level'] = loglevel

# 设置文件名称
SET_CONFIG_HEADER['file_name'] = file_name
ERROR_SET_CONFIG_HEADER['file_name'] = _add_error_to_file_name(file_name)
if not file_name:
current_pid = str(os.getpid())
else:
current_pid = file_name
full_log_path = os.path.join(log_path, "log")
if not os.path.exists(full_log_path):
os.makedirs(full_log_path)
info_log_file = os.path.join(log_path, "log", current_pid + ".info.log")
error_log_file = os.path.join(log_path, "log", current_pid + ".error.log")
SET_CONFIG_HEADER["file_name"] = info_log_file
ERROR_SET_CONFIG_HEADER["file_name"] = error_log_file

# 设置滚动时间
SET_CONFIG_HEADER['rotate_frequency'] = rotate_frequency
Expand All @@ -238,7 +273,7 @@ def setLogConfig(self,
ERROR_SET_CONFIG_HEADER['rotate_interval'] = rotate_interval

# 设置最大文件大小

SET_CONFIG_HEADER['max_file_size'] = max_file_size
ERROR_SET_CONFIG_HEADER['max_file_size'] = max_file_size

Expand Down Expand Up @@ -320,11 +355,5 @@ def get_logger(name, level=logging.INFO):
logger.propagate = False
return logger

def _add_error_to_file_name(filename):
prefix = "error."
dir_name, base_name = os.path.split(filename)
new_base_name = f"{prefix}{base_name}"
return os.path.join(dir_name, new_base_name)


logger = _setup_logging()

0 comments on commit 4868155

Please sign in to comment.