Skip to content

Commit

Permalink
实现:UI全局设置中修改日志文件保存级别 (hiroi-sora#584)
Browse files Browse the repository at this point in the history
  • Loading branch information
hiroi-sora committed Oct 9, 2024
1 parent 6a3d088 commit 4cae5e0
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 15 deletions.
45 changes: 38 additions & 7 deletions UmiOCR-data/py_src/imports/umi_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,23 @@
from logging.handlers import RotatingFileHandler
from logging import LogRecord

# 日志目录路径
# 保存的日志级别,可在UI修改
Save_Log_Level: int = logging.WARNING

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

# 日志级别,对应的int值由小到大
_Log_Levels = {
"DEBUG": logging.DEBUG,
"INFO": logging.INFO,
"WARNING": logging.WARNING,
"ERROR": logging.ERROR,
"CRITICAL": logging.CRITICAL,
"NONE": logging.CRITICAL + 10, # 最大,表示不记录日志
}


# 覆盖过滤器
class _CoverFilter(logging.Filter):
Expand Down Expand Up @@ -124,12 +137,14 @@ def emit(self, record: LogRecord):
except Exception:
self.handleError(record)
# 输出到日志文件
try:
with open(self.baseFilename, "a", encoding=self.encoding) as f:
json.dump(log_dict, f, ensure_ascii=False)
f.write("\n")
except Exception:
self.handleError(record)
if record.levelno >= Save_Log_Level:
try:
with open(self.baseFilename, "a", encoding=self.encoding) as f:
json.dump(log_dict, f, ensure_ascii=False)
f.write("\n")
except Exception:
self.handleError(record)
# TODO: 输出到UI界面


# 日志记录器 管理类
Expand Down Expand Up @@ -215,3 +230,19 @@ def qt_message_handler(mode: QtMsgType, context: QMessageLogContext, msg: str):
logger.critical(msg, extra=extra)

return qt_message_handler


# 更改保存的日志级别,成功返回T
def change_save_log_level(levelname):
global Save_Log_Level
if levelname in _Log_Levels.keys():
Save_Log_Level = _Log_Levels[levelname]
logger.info(f"设置保存日志级别: {levelname}")
return True
logger.error(f"设置保存日志级别 {levelname} 失败。")
return False


# 打开日志保存目录
def open_logs_dir():
os.startfile(Logs_Dir)
21 changes: 15 additions & 6 deletions UmiOCR-data/py_src/utils/global_configs_connector.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
# 全局设置连接器

import os
from PySide2.QtCore import QObject, Slot

from . import app_opengl
from .i18n_configs import I18n
from ..platform import Platform
from .pre_configs import getErrorStr
from ..server import web_server
from ..server.cmd_server import CmdActuator

import os
from PySide2.QtCore import QObject, Slot, Signal
from umi_log import change_save_log_level, open_logs_dir


class GlobalConfigsConnector(QObject):
Expand Down Expand Up @@ -45,6 +46,16 @@ def getOpengl(self):
def setOpengl(self, opt):
app_opengl.setOpengl(opt)

# 修改日志级别,成功返回T
@Slot(str, result=bool)
def change_save_log_level(self, levelname):
return change_save_log_level(levelname)

# 打开日志保存目录
@Slot()
def open_logs_dir(self):
open_logs_dir()

# 启动web服务器,传入qml对象及回调函数名。
@Slot("QVariant", str, str, result=int)
def runUmiWeb(self, qmlObj, callback, host):
Expand All @@ -69,9 +80,7 @@ def checkAccess(self):
if not os.access(cwd, os.R_OK):
err += "在当前路径不具有可读权限。\nDo not have read permission on the current path."
if not os.access(cwd, os.W_OK):
err += (
"在当前路径不具有可写权限。\nDo not have write permission on the current path."
)
err += "在当前路径不具有可写权限。\nDo not have write permission on the current path."
if err:
err = cwd + "\n" + err
return err
38 changes: 36 additions & 2 deletions UmiOCR-data/qt_res/qml/Configs/GlobalConfigs.qml
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ Configs {
"title": qsTr("隐藏托盘图标"),
"toolTip": qsTr("若要弹出位于后台的软件窗口,请在文件管理器中重复启动软件。\n若要彻底退出软件,请从任务管理器中结束进程。"),
"default": false,
"advanced": true,
"onChanged": changeHideTrayIcon,
},
"simpleNotificationType": utilsDicts.getSimpleNotificationType(true),
Expand Down Expand Up @@ -203,7 +202,6 @@ Configs {
"server": {
"title": qsTr("服务"),
"type": "group",
"advanced": true,

"enable": {
"title": qsTr("允许HTTP服务"),
Expand Down Expand Up @@ -239,6 +237,42 @@ Configs {
},
},

// 服务
"logs": {
"title": qsTr("日志"),
"type": "group",

"btns": {
"title": qsTr("操作"),
"btnsList": [
{
"text":qsTr("打开日志保存目录"),
"onClicked": globalConfigConn.open_logs_dir,
"textColorKey":"specialTextColor",
},
],
},
"saveLogLevel": {
"title": qsTr("保存的日志级别"),
"default": "WARNING",
"optionsList": [
["NONE", "Close"],
["DEBUG","DEBUG"],
["INFO","INFO"],
["WARNING","WARNING"],
["ERROR","ERROR"],
["CRITICAL","CRITICAL"],
],
"onChanged": (val, old)=>{
// 调整级别,如果修改失败则不动UI
if(!globalConfigConn.change_save_log_level(val))
return true
if(val === "NONE")
qmlapp.popup.simple(qsTr("已禁用日志文件保存"), "")
},
},
},

// OCR接口全局设定
"ocr": undefined,

Expand Down

0 comments on commit 4cae5e0

Please sign in to comment.