From eebf3dec68f86d3bdbcab91ebe89c8481272e27f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=AE=E5=8F=AE=E5=BD=93?= <604054726@qq.com> Date: Thu, 22 Feb 2024 23:54:25 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix=20=E6=97=A5=E5=BF=97=E6=81=A2=E5=A4=8D?= =?UTF-8?q?=E6=88=90=E6=96=87=E4=BB=B6=E5=90=8D=E6=96=B9=E5=BC=8F/?= =?UTF-8?q?=E6=8F=92=E4=BB=B6=E8=B0=83=E7=94=A8=E5=86=85=E7=BD=AE=E6=A8=A1?= =?UTF-8?q?=E5=9D=97=E7=9A=84=E6=97=A5=E5=BF=97=E5=B0=86=E4=BC=9A=E6=98=BE?= =?UTF-8?q?=E7=A4=BA=E5=9C=A8=E6=8F=92=E4=BB=B6=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/log.py | 67 ++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 47 insertions(+), 20 deletions(-) diff --git a/app/log.py b/app/log.py index 4df32220..1cab8a0a 100644 --- a/app/log.py +++ b/app/log.py @@ -40,6 +40,32 @@ class LoggerManager: # 默认日志文件 _default_log_file = "moviepilot.log" + def __get_caller(self): + """ + 获取调用者的文件名称与插件名称(如果是插件调用内置的模块, 也能写入到插件日志文件中) + """ + # 调用者文件名称 + caller_name = None + # 调用者插件名称 + plugin_name = None + for i in inspect.stack(): + filepath = Path(i.filename) + parts = filepath.parts + if not caller_name and parts and parts[-1].endswith(".py") and parts[-1] != "log.py": + # 设定调用者文件名称 + if parts[-1] == "__init__.py": + caller_name = parts[-2] + else: + caller_name = parts[-1] + if "app" in parts: + if not plugin_name and "plugins" in parts: + # 设定调用者插件名称 + plugin_name = parts[parts.index("plugins") + 1] + if "main.py" in parts: + # 已经到达程序的入口 + break + return caller_name or "log.py", plugin_name + @staticmethod def __setup_logger(log_file: str): """ @@ -66,7 +92,7 @@ class LoggerManager: # 终端日志 console_handler = logging.StreamHandler() console_handler.setLevel(logging.DEBUG) - console_formatter = CustomFormatter("%(leveltext)s%(name)s - %(message)s") + console_formatter = CustomFormatter(f"%(leveltext)s%(message)s") console_handler.setFormatter(console_formatter) _logger.addHandler(console_handler) @@ -77,23 +103,23 @@ class LoggerManager: backupCount=3, encoding='utf-8') file_handler.setLevel(logging.INFO) - file_formater = CustomFormatter("【%(levelname)s】%(asctime)s - %(name)s - %(message)s") + file_formater = CustomFormatter(f"【%(levelname)s】%(asctime)s - %(message)s") file_handler.setFormatter(file_formater) _logger.addHandler(file_handler) return _logger - def logger(self, path: str) -> logging.Logger: + def logger(self, method, msg, *args, **kwargs) -> logging.Logger: """ 获取模块的logger :param path: 当前运行程序路径 """ - filepath = Path(path) + # 获取调用者文件名和插件名 + caller_name, plugin_name = self.__get_caller() # 区分插件日志 - if "plugins" in filepath.parts: + if plugin_name: # 使用插件日志文件 - plugin_name = filepath.parts[filepath.parts.index("plugins") + 1] logfile = Path("plugins") / f"{plugin_name}.log" else: # 使用默认日志文件 @@ -104,44 +130,45 @@ class LoggerManager: if not _logger: _logger = self.__setup_logger(logfile) self._loggers[logfile] = _logger - return _logger + if hasattr(_logger, method): + method = getattr(_logger, method) + method(f"{caller_name} - {msg}", *args, **kwargs) def info(self, msg, *args, **kwargs): """ - 重载info方法,按模块区分输出 + 重载info方法 """ - self.logger(inspect.stack()[1].filename).info(msg, *args, **kwargs) + self.logger("info", msg, *args, **kwargs) def debug(self, msg, *args, **kwargs): """ - 重载debug方法,按模块区分输出 + 重载debug方法 """ - self.logger(inspect.stack()[1].filename).debug(msg, *args, **kwargs) + self.logger("debug", msg, *args, **kwargs) def warning(self, msg, *args, **kwargs): """ - 重载warning方法,按模块区分输出 + 重载warning方法 """ - self.logger(inspect.stack()[1].filename).warning(msg, *args, **kwargs) + self.logger("warning", msg, *args, **kwargs) def warn(self, msg, *args, **kwargs): """ - 重载warn方法,按模块区分输出 + 重载warn方法 """ - self.warning(msg, *args, **kwargs) + self.logger("warning", msg, *args, **kwargs) def error(self, msg, *args, **kwargs): """ - 重载error方法,按模块区分输出 + 重载error方法 """ - self.logger(inspect.stack()[1].filename).error(msg, *args, **kwargs) + self.logger("error", msg, *args, **kwargs) def critical(self, msg, *args, **kwargs): """ - 重载critical方法,按模块区分输出 + 重载critical方法 """ - self.logger(inspect.stack()[1].filename).critical(msg, *args, **kwargs) - + self.logger("critical", msg, *args, **kwargs) # 初始化公共日志 logger = LoggerManager() From 7524379af6a94bf20675df3d7858e52ad5d7ccc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=AE=E5=8F=AE=E5=BD=93?= <604054726@qq.com> Date: Fri, 23 Feb 2024 01:15:11 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=9D=A1=E4=BB=B6?= =?UTF-8?q?=E5=87=8F=E5=B0=91=E5=BE=AA=E7=8E=AF=E6=AC=A1=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/log.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/app/log.py b/app/log.py index 1cab8a0a..0e09735a 100644 --- a/app/log.py +++ b/app/log.py @@ -48,10 +48,10 @@ class LoggerManager: caller_name = None # 调用者插件名称 plugin_name = None - for i in inspect.stack(): + for i in inspect.stack()[3:]: filepath = Path(i.filename) parts = filepath.parts - if not caller_name and parts and parts[-1].endswith(".py") and parts[-1] != "log.py": + if not caller_name: # 设定调用者文件名称 if parts[-1] == "__init__.py": caller_name = parts[-2] @@ -61,9 +61,13 @@ class LoggerManager: if not plugin_name and "plugins" in parts: # 设定调用者插件名称 plugin_name = parts[parts.index("plugins") + 1] + break if "main.py" in parts: # 已经到达程序的入口 break + elif len(parts) != 1: + # 已经超出程序范围 + break return caller_name or "log.py", plugin_name @staticmethod