option logger update
This commit is contained in:
parent
19ca8e7d86
commit
e6cb2fad4e
@ -11,7 +11,7 @@ github: @shenjackyuanjie
|
|||||||
gitee: @shenjackyuanjie
|
gitee: @shenjackyuanjie
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from typing import Any, Dict, Callable, Union, Tuple, List, Type
|
from typing import Any, Dict, Callable, Union, Tuple, List
|
||||||
|
|
||||||
from libs.MCDR.version import Version
|
from libs.MCDR.version import Version
|
||||||
|
|
||||||
@ -19,11 +19,23 @@ game_version = Version("0.6.3")
|
|||||||
__version__ = game_version
|
__version__ = game_version
|
||||||
|
|
||||||
|
|
||||||
|
class OptionNameNotDefined(Exception):
|
||||||
|
"""向初始化的 option 里添加了一个不存在于选项里的选项"""
|
||||||
|
|
||||||
|
|
||||||
class Options:
|
class Options:
|
||||||
"""
|
"""
|
||||||
Difficult Rocket 的游戏配置的存储基类
|
Difficult Rocket 的游戏配置的存储基类
|
||||||
"""
|
"""
|
||||||
__options: Dict[str, Union[Callable, object]] = {}
|
__options: Dict[str, Union[Callable, object]] = {}
|
||||||
|
cached_options: Dict[str, Union[str, Any]] = {}
|
||||||
|
|
||||||
|
def __init__(self, **kwargs):
|
||||||
|
for option, value in kwargs.items():
|
||||||
|
if option not in self.option():
|
||||||
|
raise OptionNameNotDefined(f"option: {option} with value: {value} is not defined")
|
||||||
|
setattr(self, option, value)
|
||||||
|
self.flush_option()
|
||||||
|
|
||||||
def option(self) -> Dict[str, Any]:
|
def option(self) -> Dict[str, Any]:
|
||||||
"""
|
"""
|
||||||
@ -44,8 +56,16 @@ class Options:
|
|||||||
values[option] = getattr(self, option)
|
values[option] = getattr(self, option)
|
||||||
return values
|
return values
|
||||||
|
|
||||||
|
def flush_option(self) -> Dict[str, Any]:
|
||||||
|
"""
|
||||||
|
刷新缓存 options 的内容
|
||||||
|
:return: 刷新过的 options
|
||||||
|
"""
|
||||||
|
self.cached_options = self.option()
|
||||||
|
return self.cached_options
|
||||||
|
|
||||||
def option_with_len(self) -> List[Union[List[Tuple[str, Any, Any]], int, Any]]:
|
def option_with_len(self) -> List[Union[List[Tuple[str, Any, Any]], int, Any]]:
|
||||||
options = self.option()
|
options = self.flush_option()
|
||||||
max_len_key = 1
|
max_len_key = 1
|
||||||
max_len_value = 1
|
max_len_value = 1
|
||||||
max_len_value_t = 1
|
max_len_value_t = 1
|
||||||
@ -96,7 +116,8 @@ class _DR_runtime(Options):
|
|||||||
# game options
|
# game options
|
||||||
_language = 'zh-CN'
|
_language = 'zh-CN'
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self, **kwargs):
|
||||||
|
super().__init__(**kwargs)
|
||||||
self.__options = {'language': self.language}
|
self.__options = {'language': self.language}
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@ -36,7 +36,6 @@ logger = logger.get_logger(name)
|
|||||||
直接获取一个配置好的logger
|
直接获取一个配置好的logger
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
color_reset_suffix = "\033[0m"
|
color_reset_suffix = "\033[0m"
|
||||||
""" 只是用来重置颜色的后缀 """
|
""" 只是用来重置颜色的后缀 """
|
||||||
|
|
||||||
@ -80,28 +79,28 @@ class LoggingLevel(enum.IntEnum):
|
|||||||
|
|
||||||
|
|
||||||
level_name_map = {
|
level_name_map = {
|
||||||
LoggingLevel.ALL: 'ALL', # NOTSET
|
LoggingLevel.ALL: 'ALL', # NOTSET
|
||||||
LoggingLevel.TRACE: 'TRACE',
|
LoggingLevel.TRACE: 'TRACE',
|
||||||
LoggingLevel.FINE: 'FINE',
|
LoggingLevel.FINE: 'FINE',
|
||||||
LoggingLevel.DEBUG: 'DEBUG',
|
LoggingLevel.DEBUG: 'DEBUG',
|
||||||
LoggingLevel.INFO: 'INFO',
|
LoggingLevel.INFO: 'INFO',
|
||||||
LoggingLevel.WARNING: 'WARNING', # WARN
|
LoggingLevel.WARNING: 'WARNING', # WARN
|
||||||
LoggingLevel.ERROR: 'ERROR',
|
LoggingLevel.ERROR: 'ERROR',
|
||||||
LoggingLevel.FATAL: 'FATAL'
|
LoggingLevel.FATAL: 'FATAL'
|
||||||
}
|
}
|
||||||
|
|
||||||
name_level_map = {
|
name_level_map = {
|
||||||
'NOTSET': LoggingLevel.ALL,
|
'NOTSET': LoggingLevel.ALL,
|
||||||
'ALL': LoggingLevel.ALL,
|
'ALL': LoggingLevel.ALL,
|
||||||
'TRACE': LoggingLevel.TRACE,
|
'TRACE': LoggingLevel.TRACE,
|
||||||
'FINE': LoggingLevel.FINE,
|
'FINE': LoggingLevel.FINE,
|
||||||
'DEBUG': LoggingLevel.DEBUG,
|
'DEBUG': LoggingLevel.DEBUG,
|
||||||
'INFO': LoggingLevel.INFO,
|
'INFO': LoggingLevel.INFO,
|
||||||
'WARNING': LoggingLevel.WARNING,
|
'WARNING': LoggingLevel.WARNING,
|
||||||
'WARN': LoggingLevel.WARNING,
|
'WARN': LoggingLevel.WARNING,
|
||||||
'ERROR': LoggingLevel.ERROR,
|
'ERROR': LoggingLevel.ERROR,
|
||||||
'CRITICAL': LoggingLevel.FATAL,
|
'CRITICAL': LoggingLevel.FATAL,
|
||||||
'FATAL': LoggingLevel.FATAL
|
'FATAL': LoggingLevel.FATAL
|
||||||
}
|
}
|
||||||
|
|
||||||
# class LoggerConfig(enum.Enum):
|
# class LoggerConfig(enum.Enum):
|
||||||
@ -112,73 +111,73 @@ name_level_map = {
|
|||||||
|
|
||||||
|
|
||||||
logger_configs = {
|
logger_configs = {
|
||||||
'Logger': {
|
'Logger': {
|
||||||
'root': {
|
'root': {
|
||||||
'level': LoggingLevel.DEBUG,
|
'level': LoggingLevel.DEBUG,
|
||||||
'color': 'main_color',
|
'color': 'main_color',
|
||||||
'file': 'main_log_file',
|
'file': 'main_log_file',
|
||||||
},
|
},
|
||||||
'client': {
|
'client': {
|
||||||
'level': LoggingLevel.TRACE,
|
'level': LoggingLevel.TRACE,
|
||||||
'color': 'main_color',
|
'color': 'main_color',
|
||||||
'file': 'main_log_file',
|
'file': 'main_log_file',
|
||||||
},
|
},
|
||||||
'server': {
|
'server': {
|
||||||
'level': LoggingLevel.TRACE,
|
'level': LoggingLevel.TRACE,
|
||||||
'color': 'DiGua_color',
|
'color': 'DiGua_color',
|
||||||
'file': 'main_log_file',
|
'file': 'main_log_file',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
'Color': {
|
'Color': {
|
||||||
'main_color': {
|
'main_color': {
|
||||||
'file_time': '\033[38;2;201;222;56m',
|
'file_time': '\033[38;2;201;222;56m',
|
||||||
'main_time': '\033[38;2;201;222;56m',
|
'main_time': '\033[38;2;201;222;56m',
|
||||||
'file_name': '\033[38;2;0;255;180m',
|
'file_name': '\033[38;2;0;255;180m',
|
||||||
'code_line': '\033[38;2;0;255;180m',
|
'code_line': '\033[38;2;0;255;180m',
|
||||||
'logger': '\033[0m',
|
'logger': '\033[0m',
|
||||||
LoggingLevel.TRACE: {'info': '\033[38;2;138;173;244m', 'message': '\033[38;2;138;173;244m'},
|
LoggingLevel.TRACE: {'info': '\033[38;2;138;173;244m', 'message': '\033[38;2;138;173;244m'},
|
||||||
LoggingLevel.FINE: {'info': '\033[35;48;2;44;44;54m', 'message': '\033[35m'},
|
LoggingLevel.FINE: {'info': '\033[35;48;2;44;44;54m', 'message': '\033[35m'},
|
||||||
LoggingLevel.DEBUG: {'info': '\033[38;2;133;138;149m', 'message': '\033[38;2;133;138;149m'},
|
LoggingLevel.DEBUG: {'info': '\033[38;2;133;138;149m', 'message': '\033[38;2;133;138;149m'},
|
||||||
LoggingLevel.INFO: {'info': '\033[0m', 'message': '\033[0m'},
|
LoggingLevel.INFO: {'info': '\033[0m', 'message': '\033[0m'},
|
||||||
LoggingLevel.WARNING: {'info': '\033[33m', 'message': '\033[33m'},
|
LoggingLevel.WARNING: {'info': '\033[33m', 'message': '\033[33m'},
|
||||||
LoggingLevel.ERROR: {'info': '\033[31m', 'message': '\033[31m'},
|
LoggingLevel.ERROR: {'info': '\033[31m', 'message': '\033[31m'},
|
||||||
LoggingLevel.FATAL: {'info': '\033[38;2;255;255;0;48;2;120;10;10m', 'message': '\033[38;2;255;255;0;48;2;120;10;10m', 'logger': '\033[38;2;245;189;230m'}
|
LoggingLevel.FATAL: {'info': '\033[38;2;255;255;0;48;2;120;10;10m', 'message': '\033[38;2;255;255;0;48;2;120;10;10m', 'logger': '\033[38;2;245;189;230m'}
|
||||||
},
|
},
|
||||||
'DiGua_color': {
|
'DiGua_color': {
|
||||||
# catppuccin Macchiato
|
# catppuccin Macchiato
|
||||||
'file_time': '\033[38;2;238;212;159m',
|
'file_time': '\033[38;2;238;212;159m',
|
||||||
'main_time': '\033[38;2;202;211;245m',
|
'main_time': '\033[38;2;202;211;245m',
|
||||||
'file_name': '\033[38;2;139;213;202m',
|
'file_name': '\033[38;2;139;213;202m',
|
||||||
'code_line': '\033[38;2;166;218;149m',
|
'code_line': '\033[38;2;166;218;149m',
|
||||||
'logger': '\033[0m',
|
'logger': '\033[0m',
|
||||||
LoggingLevel.TRACE: {'info': '\033[38;2;138;173;244m', 'message': '\033[38;2;138;173;244m'},
|
LoggingLevel.TRACE: {'info': '\033[38;2;138;173;244m', 'message': '\033[38;2;138;173;244m'},
|
||||||
LoggingLevel.FINE: {'info': '\033[38;2;198;160;246m', 'message': '\033[38;2;198;160;246m'},
|
LoggingLevel.FINE: {'info': '\033[38;2;198;160;246m', 'message': '\033[38;2;198;160;246m'},
|
||||||
LoggingLevel.DEBUG: {'info': '\033[38;2;133;138;149m', 'message': '\033[38;2;133;138;149m'},
|
LoggingLevel.DEBUG: {'info': '\033[38;2;133;138;149m', 'message': '\033[38;2;133;138;149m'},
|
||||||
LoggingLevel.INFO: {'info': '\033[0m', 'message': '\033[0m'},
|
LoggingLevel.INFO: {'info': '\033[0m', 'message': '\033[0m'},
|
||||||
LoggingLevel.WARNING: {'info': '\033[38;2;245;169;127m', 'message': '\033[38;2;245;169;127m'},
|
LoggingLevel.WARNING: {'info': '\033[38;2;245;169;127m', 'message': '\033[38;2;245;169;127m'},
|
||||||
LoggingLevel.ERROR: {'info': '\033[38;2;237;135;150m', 'message': '\033[38;2;237;135;150m'},
|
LoggingLevel.ERROR: {'info': '\033[38;2;237;135;150m', 'message': '\033[38;2;237;135;150m'},
|
||||||
LoggingLevel.FATAL: {'info': '\033[38;2;255;255;0;48;2;120;10;10m', 'message': '\033[38;2;255;255;0;48;2;120;10;10m', 'logger': '\033[38;2;245;189;230m'}
|
LoggingLevel.FATAL: {'info': '\033[38;2;255;255;0;48;2;120;10;10m', 'message': '\033[38;2;255;255;0;48;2;120;10;10m', 'logger': '\033[38;2;245;189;230m'}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
'File': {
|
'File': {
|
||||||
'main_log_file': {
|
'main_log_file': {
|
||||||
'mode': 'a',
|
'mode': 'a',
|
||||||
'encoding': 'utf-8',
|
'encoding': 'utf-8',
|
||||||
'level': LoggingLevel.TRACE,
|
'level': LoggingLevel.TRACE,
|
||||||
'file_name': './logs/{file_time}_logs.md',
|
'file_name': './logs/{file_time}_logs.md',
|
||||||
'cache_len': 10,
|
'cache_len': 10,
|
||||||
'cache_time': 1
|
'cache_time': 1
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
'Formatter': {
|
'Formatter': {
|
||||||
'MESSAGE': {
|
'MESSAGE': {
|
||||||
'format': '[{main_time}] [{logger_name}] {level} | {file_name}:{code_line} | {message}'
|
'format': '[{main_time}] [{logger_name}] {level} | {file_name}:{code_line} | {message}'
|
||||||
},
|
},
|
||||||
'file_name': 'no frame',
|
'file_name': 'no frame',
|
||||||
'code_line': 'no frame',
|
'code_line': 'no frame',
|
||||||
'file_time': {'strftime': '%Y-%m-%d %H-%M'},
|
'file_time': {'strftime': '%Y-%m-%d %H-%M'},
|
||||||
'main_time': {'strftime': '%Y-%m-%d %H-%M-%S:%%S'}, # %%S 三位毫秒
|
'main_time': {'strftime': '%Y-%m-%d %H-%M-%S:%%S'}, # %%S 三位毫秒
|
||||||
...: ...
|
...: ...
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -625,6 +624,82 @@ class Logger:
|
|||||||
return self.make_log(*values, level=LoggingLevel.FATAL, marker=marker, sep=sep, end=end, flush=flush, frame=frame)
|
return self.make_log(*values, level=LoggingLevel.FATAL, marker=marker, sep=sep, end=end, flush=flush, frame=frame)
|
||||||
|
|
||||||
|
|
||||||
|
_loggerClass = Logger
|
||||||
|
|
||||||
|
|
||||||
|
class RootLogger(Logger):
|
||||||
|
""" 默认的 logger """
|
||||||
|
def __init__(self, level: int = LoggingLevel.WARNING, *args, **kwargs):
|
||||||
|
super().__init__(level=level, *args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
root_logger = RootLogger()
|
||||||
|
|
||||||
|
|
||||||
|
def trace(*values: object,
|
||||||
|
marker: Optional[str] = None,
|
||||||
|
sep: Optional[str] = ' ',
|
||||||
|
end: Optional[str] = '\n',
|
||||||
|
flush: Optional[bool] = False,
|
||||||
|
frame: Optional[FrameType] = None) -> Optional[str]:
|
||||||
|
...
|
||||||
|
|
||||||
|
|
||||||
|
def fine(*values: object,
|
||||||
|
marker: Optional[str] = None,
|
||||||
|
sep: Optional[str] = ' ',
|
||||||
|
end: Optional[str] = '\n',
|
||||||
|
flush: Optional[bool] = False,
|
||||||
|
frame: Optional[FrameType] = None) -> Optional[str]:
|
||||||
|
...
|
||||||
|
|
||||||
|
|
||||||
|
def debug(*values: object,
|
||||||
|
marker: Optional[str] = None,
|
||||||
|
sep: Optional[str] = ' ',
|
||||||
|
end: Optional[str] = '\n',
|
||||||
|
flush: Optional[bool] = False,
|
||||||
|
frame: Optional[FrameType] = None) -> Optional[str]:
|
||||||
|
...
|
||||||
|
|
||||||
|
|
||||||
|
def info(*values: object,
|
||||||
|
marker: Optional[str] = None,
|
||||||
|
sep: Optional[str] = ' ',
|
||||||
|
end: Optional[str] = '\n',
|
||||||
|
flush: Optional[bool] = False,
|
||||||
|
frame: Optional[FrameType] = None) -> Optional[str]:
|
||||||
|
...
|
||||||
|
|
||||||
|
|
||||||
|
def warning(*values: object,
|
||||||
|
marker: Optional[str] = None,
|
||||||
|
sep: Optional[str] = ' ',
|
||||||
|
end: Optional[str] = '\n',
|
||||||
|
flush: Optional[bool] = False,
|
||||||
|
frame: Optional[FrameType] = None) -> Optional[str]:
|
||||||
|
...
|
||||||
|
|
||||||
|
|
||||||
|
def error(*values: object,
|
||||||
|
marker: Optional[str] = None,
|
||||||
|
sep: Optional[str] = ' ',
|
||||||
|
end: Optional[str] = '\n',
|
||||||
|
flush: Optional[bool] = False,
|
||||||
|
frame: Optional[FrameType] = None) -> Optional[str]:
|
||||||
|
...
|
||||||
|
|
||||||
|
|
||||||
|
def fatal(*values: object,
|
||||||
|
marker: Optional[str] = None,
|
||||||
|
sep: Optional[str] = ' ',
|
||||||
|
end: Optional[str] = '\n',
|
||||||
|
flush: Optional[bool] = False,
|
||||||
|
frame: Optional[FrameType] = None) -> Optional[str]:
|
||||||
|
...
|
||||||
|
|
||||||
|
|
||||||
def get_key_from_dict(a_dict: Dict, key: Any, default: Any = None) -> Optional[Any]:
|
def get_key_from_dict(a_dict: Dict, key: Any, default: Any = None) -> Optional[Any]:
|
||||||
if default is None:
|
if default is None:
|
||||||
return a_dict[key]
|
return a_dict[key]
|
||||||
@ -666,11 +741,11 @@ def gen_file_conf(file_name: str,
|
|||||||
:param file_cache_time: 文件缓存时间
|
:param file_cache_time: 文件缓存时间
|
||||||
:return: 生成的配置
|
:return: 生成的配置
|
||||||
"""
|
"""
|
||||||
return {'file_name': file_name,
|
return {'file_name': file_name,
|
||||||
'level': file_level,
|
'level': file_level,
|
||||||
'mode': file_mode,
|
'mode': file_mode,
|
||||||
'encoding': file_encoding,
|
'encoding': file_encoding,
|
||||||
'cache_len': file_cache_len,
|
'cache_len': file_cache_len,
|
||||||
'cache_time': file_cache_time}
|
'cache_time': file_cache_time}
|
||||||
|
|
||||||
|
|
||||||
@ -710,11 +785,11 @@ def add_file_config(conf_name: str,
|
|||||||
:param file_cache_time: 文件缓存时间
|
:param file_cache_time: 文件缓存时间
|
||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
logger_configs['File'][conf_name] = {'file_name': file_name,
|
logger_configs['File'][conf_name] = {'file_name': file_name,
|
||||||
'level': file_level,
|
'level': file_level,
|
||||||
'mode': file_mode,
|
'mode': file_mode,
|
||||||
'encoding': file_encoding,
|
'encoding': file_encoding,
|
||||||
'cache_len': file_cache_len,
|
'cache_len': file_cache_len,
|
||||||
'cache_time': file_cache_time}
|
'cache_time': file_cache_time}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user