!403 增加pedt debug模块

Merge pull request !403 from xujian/master
This commit is contained in:
xujian 2024-06-17 06:21:02 +00:00 committed by Gitee
commit 8727f84f85
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
1 changed files with 54 additions and 0 deletions

54
data/Pedt/debug.py Executable file
View File

@ -0,0 +1,54 @@
import logging
import os
class Debug:
_logger = None
DEBUG_LOG=True
@classmethod
def setup_logger(cls, log_file,level=logging.INFO,console=False):
if cls._logger is not None:
return
os.makedirs(os.path.dirname(log_file), exist_ok=True)
cls._logger = logging.getLogger('Pedt')
cls._logger.setLevel(level)
file_handler = logging.FileHandler(log_file)
file_handler.setLevel(level)
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
file_handler.setFormatter(formatter)
cls._logger.addHandler(file_handler)
if console:
console_handler = logging.StreamHandler()
console_handler.setLevel(level)
console_handler.setFormatter(formatter)
cls._logger.addHandler(console_handler)
@classmethod
def debug(cls, *args):
if cls.DEBUG_LOG:
# stack = inspect.stack()
# frame = stack[1].frame
# function_name = frame.f_code.co_name
# line = frame.f_lineno
message = ' '.join(map(str, args))
cls._logger.debug(message)
@classmethod
def info(cls, *args):
if cls.DEBUG_LOG:
message = ' '.join(map(str, args))
cls._logger.info(message)
@classmethod
def warn(cls, *args):
if cls.DEBUG_LOG:
message = ' '.join(map(str, args))
cls._logger.warn(message)
@classmethod
def error(cls, *args):
if cls.DEBUG_LOG:
message = ' '.join(map(str, args))
cls._logger.error(message)