add settag for logger

This commit is contained in:
shenjack 2023-12-14 11:16:40 +08:00
parent 960f524f95
commit 2b40562296
Signed by: shenjack
GPG Key ID: 7B1134A979775551
3 changed files with 19 additions and 15 deletions

View File

@ -78,7 +78,7 @@ class Client:
def __init__(self, game: "Game", net_mode="local"):
start_time = time.time_ns()
# logging
self.logger = loggers.get_logger("client")
self.logger = loggers.get_logger("client").set_tag("client")
self.logger.info(tr().client.setup.start())
# config
self.config = ClientOption()
@ -243,7 +243,7 @@ class ClientWindow(Window):
start_time = time.time_ns()
super().__init__(*args, **kwargs)
# logging
self.logger = loggers.get_logger("client")
self.logger = loggers.get_logger("client").set_tag("window")
self.logger.info(tr().window.setup.start())
# value
self.game = game

View File

@ -37,6 +37,9 @@ class ModManager(Options):
find_mod_paths: Dict[str, Path] = {}
loaded_mod_modules: Dict[str, ModInfo] = {}
def init(self, **kwargs) -> bool:
self.logger = config.get_logger_from_old("mod_manager", "client")
def get_mod_module(self, mod_name: str) -> Optional[ModInfo]:
"""
获取指定 mod 的模块
@ -61,7 +64,7 @@ class ModManager(Options):
try:
getattr(mod, event_name)(*args, **kwargs)
except Exception as e:
logger.error(
self.logger.error(
tr()
.mod.event.error()
.format(mod, event_name, e, traceback.format_exc())
@ -73,6 +76,7 @@ class ModManager(Options):
:param mod_path: mod 的路径
:return:
"""
logger = self.logger.set_tag("load")
if not mod_path.exists():
logger.error(tr().mod.load.faild.not_exist().format(mod_path), tag="load")
return None
@ -134,7 +138,7 @@ class ModManager(Options):
):
# 文件夹 mod
mods_path.append(mod)
logger.info(
self.logger.info(
tr().mod.finded().format(len(mods_path), time.time() - start_time), tag="find"
)
return mods_path
@ -154,7 +158,7 @@ class ModManager(Options):
_add_path_to_sys(find_path)
mods = []
start_time = time.time()
logger.info(tr().mod.load.start().format(find_path), tag="load")
self.logger.info(tr().mod.load.start().format(find_path), tag="load")
for path in find_path:
if not path.exists():
path.mkdir(parents=True)
@ -166,7 +170,7 @@ class ModManager(Options):
for path in extra_mod_path:
if (cache := self.load_mod(path)) is not None:
mods.append(cache)
logger.info(tr().mod.load.use_time().format(time.time() - start_time), tag="load")
self.logger.info(tr().mod.load.use_time().format(time.time() - start_time), tag="load")
return mods
def init_mods(self, mods: List[type(ModInfo)]):
@ -180,16 +184,16 @@ class ModManager(Options):
try:
init_mod = mod_class()
self.loaded_mod_modules[init_mod.mod_id] = init_mod
logger.info(
self.logger.info(
tr().mod.init.success().format(init_mod, init_mod.version), tag="init"
)
except Exception as e:
logger.error(
self.logger.error(
tr().mod.init.faild().format(mod_class, e, traceback.format_exc()),
tag="init",
)
continue
logger.info(tr().mod.init.use_time().format(time.time() - start_time), tag="init")
self.logger.info(tr().mod.init.use_time().format(time.time() - start_time), tag="init")
def unload_mod(self, mod_id: str, game: Game) -> Optional[ModInfo]:
"""
@ -202,15 +206,15 @@ class ModManager(Options):
not (mod_class := self.loaded_mod_modules.get(mod_id))
and (mod_class := self.get_mod_module(mod_id)) is None
):
logger.warn(tr().mod.unload.faild.not_find().format(mod_id), tag="unload")
self.logger.warn(tr().mod.unload.faild.not_find().format(mod_id), tag="unload")
return None
try:
mod_class.on_unload(game=game)
self.loaded_mod_modules.pop(mod_class.mod_id)
logger.info(tr().mod.unload.success().format(mod_id), tag="unload")
self.logger.info(tr().mod.unload.success().format(mod_id), tag="unload")
return mod_class
except Exception as e:
logger.error(
self.logger.error(
tr().mod.unload.faild.error().format(mod_id, e, traceback.format_exc()),
tag="unload",
)
@ -228,7 +232,7 @@ class ModManager(Options):
return
mod_class: Optional[ModInfo] = None
if unload.mod_id not in self.find_mod_paths:
logger.warn(
self.logger.warn(
tr().mod.reload.faild.not_find().format(unload.mod_id), tag="reload"
)
paths = self.find_mods_in_path()
@ -243,4 +247,4 @@ class ModManager(Options):
self.init_mods([mod_class])
if mod_id in self.loaded_mod_modules and mod_class is not None:
self.loaded_mod_modules[mod_id].on_load(game=game, old_self=mod_class)
logger.info(tr().mod.reload.success().format(mod_id), tag="reload")
self.logger.info(tr().mod.reload.success().format(mod_id), tag="reload")

View File

@ -28,7 +28,7 @@ class Server:
def __init__(self, net_mode="local"):
start_time = time.time()
# logging
self.logger = loggers.config.get_logger("server")
self.logger = loggers.config.get_logger("server").set_tag("server")
self.logger.info(tr().server.setup.start())
# value
self.process_id = os.getpid()