Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
yinjiaqi authored and yinjiaqi committed Dec 23, 2024
1 parent 85c524f commit 44c5a25
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 8 deletions.
8 changes: 4 additions & 4 deletions docs/BasisModule/Trace/Debug.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ os.Setenv("APPBUILDER_LOGLEVEL", "/tmp/appbuilder.log")
参数说明:
- rolling (bool, optional): 是否启用滚动日志. 默认为True.
- update_interval (int, optional): 更新日志文件的间隔时间. 默认为1.
- update_time (str, optional): 更新日志文件的时间间隔单位. 默认为空字符串.
- update_time (str, optional): 更新日志文件的时间间隔单位. 默认为'midnight',每日凌晨更新.
- 可选值:
- 'S' - Seconds
- 'M' - Minutes
Expand All @@ -80,8 +80,8 @@ appbuilder.logger.setLoglevel("DEBUG")
这里设置:
rolling=Ture - 启用滚动日志(运行此段代码,默认使用滚动日志)
filename="appbuilder.log" - 此优先级最高会覆盖之前的设置,若未传参则使用之前已经设置的日志文件,若之前未设置则使用默认的"tmp.log"日志文件
update_interval = 30 - 更新日志文件的间隔数量级
update_time = 'S' - 更新日志文件的间隔单位为秒级
update_interval = 1 - 更新日志文件的间隔数量级
update_time = 'midnight' - 更新日志文件的间隔单位为秒级,每日凌晨滚动日志
"""
appbuilder.logger.setLogConfig(filename="appbuilder.log",update_interval=30, update_time='S')
appbuilder.logger.setLogConfig(filename="appbuilder.log",update_interval=1, update_time='midnight')
```
63 changes: 59 additions & 4 deletions python/utils/logger_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,30 @@
'encoding': 'utf-8',
}

TIME_HANDLERS_FILE_ERROR = {
'class': 'logging.handlers.TimedRotatingFileHandler',
'formatter': 'standard',
'level': 'ERROR',
'filename': "",
'when': 'midnight', # 可选项: 'S', 'M', 'H', 'D', 'W0'-'W6', 'midnight'
'interval': 1, # 每1天滚动一次
'backupCount': 5, # 保留5个备份
'encoding': 'utf-8',
}

SIMPLE_HANDLERS_FILE = {
"level": "INFO",
"class": "logging.FileHandler",
"filename": "",
"formatter": "standard",
}

SIMPLE_HANDLERS_FILE_ERROR = {
"level": "ERROR",
"class": "logging.FileHandler",
"filename": "",
"formatter": "standard",
}

class LoggerWithLoggerId(logging.LoggerAdapter):
"""
Expand All @@ -81,12 +98,15 @@ def __init__(self, logger, extra, loglevel):
log_file = os.environ.get("APPBUILDER_LOGFILE", "")
if log_file:
LOGGING_CONFIG["loggers"]["appbuilder"]["handler"] = ["console"] # 默认使用console
SIMPLE_HANDLERS_FILE["filename"] = log_file
TIME_HANDLERS_FILE['filename'] = log_file

SIMPLE_HANDLERS_FILE["level"] = loglevel
TIME_HANDLERS_FILE['level'] = loglevel
SIMPLE_HANDLERS_FILE["filename"] = log_file
SIMPLE_HANDLERS_FILE_ERROR["filename"] = f"error.{log_file}"
LOGGING_CONFIG["handlers"]["file"] = SIMPLE_HANDLERS_FILE
LOGGING_CONFIG["handlers"]["error_file"] = SIMPLE_HANDLERS_FILE_ERROR
LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"].append("file")
LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"].append("error_file")
LOGGING_CONFIG['handlers']['console']['level'] = loglevel
LOGGING_CONFIG['loggers']['appbuilder']['level'] = loglevel
logging.config.dictConfig(LOGGING_CONFIG)
Expand Down Expand Up @@ -125,13 +145,15 @@ def setLogConfig(
self,
rolling:bool=True,
update_interval:int=1,
update_time:str='',
update_time:str='midnight',
backup_count:int=0,
filename:str=''
):
# 配置控制台输出
if "file" not in LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"]:
LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"].append("file")
if "error_file" not in LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"]:
LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"].append("error_file")

# 确定备份数量
if backup_count <= 0 or not isinstance(backup_count, int):
Expand All @@ -149,37 +171,69 @@ def setLogConfig(

# 设置filename
if not filename:

filename = (SIMPLE_HANDLERS_FILE.get("filename") or
TIME_HANDLERS_FILE.get("filename") or
"tmp.log")


# 创建处理器
if rolling:
if update_time:
TIME_HANDLERS_FILE['when'] = update_time
TIME_HANDLERS_FILE_ERROR['when'] = update_time
TIME_HANDLERS_FILE['interval'] = update_interval
TIME_HANDLERS_FILE_ERROR['interval'] = update_interval
TIME_HANDLERS_FILE['backupCount'] = backup_count
TIME_HANDLERS_FILE_ERROR['backupCount'] = backup_count
TIME_HANDLERS_FILE['filename'] = filename
TIME_HANDLERS_FILE_ERROR['filename'] = f"error.{filename}"
if 'file' in LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"]:
LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"].remove("file")
if 'error_file' in LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"]:
LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"].remove("error_file")
if not "timed_file" in LOGGING_CONFIG["handlers"]:
LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"].append('timed_file')
if not "error_timed_file" in LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"]:
LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"].append('error_timed_file')
LOGGING_CONFIG["handlers"]["timed_file"] = TIME_HANDLERS_FILE
LOGGING_CONFIG["handlers"]["error_timed_file"] = TIME_HANDLERS_FILE_ERROR
LOGGING_CONFIG["handlers"]["timed_file"]["level"] = LOGGING_CONFIG['loggers']['appbuilder']['level']
else:
SIMPLE_HANDLERS_FILE["filename"] = filename
SIMPLE_HANDLERS_FILE_ERROR["filename"] = f"error.{filename}"
LOGGING_CONFIG["handlers"]["file"] = SIMPLE_HANDLERS_FILE
LOGGING_CONFIG["handlers"]["error_file"] = SIMPLE_HANDLERS_FILE_ERROR
if "timed_file" in LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"]:
LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"].remove("timed_file")
if "error_timed_file" in LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"]:
LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"].remove("error_timed_file")
if "file" not in LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"]:
LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"].append("file")
if "error_file" not in LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"]:
LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"].append("error_file")
LOGGING_CONFIG["handlers"]["file"]["level"] = LOGGING_CONFIG['loggers']['appbuilder']['level']
logging.config.dictConfig(LOGGING_CONFIG)

logging.config.dictConfig(LOGGING_CONFIG)

def setFilename(self, filename):
"""
set filename
设置此函数会同时设置日志文件和错误日志文件的文件名。同时取消了日志文件的滚动功能。
"""
if "timed_file" in LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"]:
LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"].remove("timed_file")
if "error_timed_file" in LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"]:
LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"].remove("error_timed_file")
if "file" not in LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"]:
LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"].append("file")
if "error_file" not in LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"]:
LOGGING_CONFIG["loggers"]["appbuilder"]["handlers"].append("error_file")
SIMPLE_HANDLERS_FILE["filename"] = filename
SIMPLE_HANDLERS_FILE_ERROR["filename"] = f"error.{filename}"
LOGGING_CONFIG["handlers"]["file"] = SIMPLE_HANDLERS_FILE
LOGGING_CONFIG["handlers"]["error_file"] = SIMPLE_HANDLERS_FILE_ERROR
logging.config.dictConfig(LOGGING_CONFIG)

def setLoglevel(self, level):
Expand All @@ -194,6 +248,7 @@ def setLoglevel(self, level):
LOGGING_CONFIG['handlers']['console']['level'] = log_level
LOGGING_CONFIG['loggers']['appbuilder']['level'] = log_level
SIMPLE_HANDLERS_FILE["level"] = log_level
TIME_HANDLERS_FILE['level'] = log_level
logging.config.dictConfig(LOGGING_CONFIG)

def process(self, msg, kwargs):
Expand Down

0 comments on commit 44c5a25

Please sign in to comment.