mirror of
https://github.com/Genshin-bots/gsuid_core.git
synced 2025-05-12 06:55:49 +08:00
✨ configure log level (close #5)
This commit is contained in:
parent
224ac1596f
commit
ccccddd1aa
@ -4,23 +4,27 @@ from typing import Dict, List, Union, Literal, overload
|
|||||||
|
|
||||||
CONFIG_PATH = Path(__file__).parent / 'config.json'
|
CONFIG_PATH = Path(__file__).parent / 'config.json'
|
||||||
|
|
||||||
CONIFG_DEFAULT = {
|
CONFIG_DEFAULT = {
|
||||||
'HOST': 'localhost',
|
'HOST': 'localhost',
|
||||||
'PORT': '8765',
|
'PORT': '8765',
|
||||||
'masters': [],
|
'masters': [],
|
||||||
'superusers': [],
|
'superusers': [],
|
||||||
'sv': {},
|
'sv': {},
|
||||||
|
'log': {
|
||||||
|
'level': 'INFO',
|
||||||
|
# ...
|
||||||
|
},
|
||||||
}
|
}
|
||||||
STR_CONFIG = Literal['HOST', 'PORT']
|
STR_CONFIG = Literal['HOST', 'PORT']
|
||||||
LIST_CONFIG = Literal['superusers', 'masters']
|
LIST_CONFIG = Literal['superusers', 'masters']
|
||||||
DICT_CONFIG = Literal['sv']
|
DICT_CONFIG = Literal['sv', 'log']
|
||||||
|
|
||||||
|
|
||||||
class CoreConfig:
|
class CoreConfig:
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
if not CONFIG_PATH.exists():
|
if not CONFIG_PATH.exists():
|
||||||
with open(CONFIG_PATH, 'w', encoding='UTF-8') as file:
|
with open(CONFIG_PATH, 'w', encoding='UTF-8') as file:
|
||||||
json.dump(CONIFG_DEFAULT, file, indent=4, ensure_ascii=False)
|
json.dump(CONFIG_DEFAULT, file, indent=4, ensure_ascii=False)
|
||||||
|
|
||||||
self.update_config()
|
self.update_config()
|
||||||
|
|
||||||
@ -33,9 +37,9 @@ class CoreConfig:
|
|||||||
with open(CONFIG_PATH, 'r', encoding='UTF-8') as f:
|
with open(CONFIG_PATH, 'r', encoding='UTF-8') as f:
|
||||||
self.config = json.load(f)
|
self.config = json.load(f)
|
||||||
# 对没有的值,添加默认值
|
# 对没有的值,添加默认值
|
||||||
for key in CONIFG_DEFAULT:
|
for key in CONFIG_DEFAULT:
|
||||||
if key not in self.config:
|
if key not in self.config:
|
||||||
self.config[key] = CONIFG_DEFAULT[key]
|
self.config[key] = CONFIG_DEFAULT[key]
|
||||||
|
|
||||||
# 重新写回
|
# 重新写回
|
||||||
self.write_config()
|
self.write_config()
|
||||||
@ -55,7 +59,7 @@ class CoreConfig:
|
|||||||
def get_config(self, key: str) -> Union[str, Dict, List]:
|
def get_config(self, key: str) -> Union[str, Dict, List]:
|
||||||
if key in self.config:
|
if key in self.config:
|
||||||
return self.config[key]
|
return self.config[key]
|
||||||
elif key in CONIFG_DEFAULT:
|
elif key in CONFIG_DEFAULT:
|
||||||
self.update_config()
|
self.update_config()
|
||||||
return self.config[key]
|
return self.config[key]
|
||||||
else:
|
else:
|
||||||
@ -74,7 +78,7 @@ class CoreConfig:
|
|||||||
...
|
...
|
||||||
|
|
||||||
def set_config(self, key: str, value: Union[str, List, Dict]) -> bool:
|
def set_config(self, key: str, value: Union[str, List, Dict]) -> bool:
|
||||||
if key in CONIFG_DEFAULT:
|
if key in CONFIG_DEFAULT:
|
||||||
# 设置值
|
# 设置值
|
||||||
self.config[key] = value
|
self.config[key] = value
|
||||||
# 重新写回
|
# 重新写回
|
||||||
|
@ -5,6 +5,8 @@ from typing import TYPE_CHECKING
|
|||||||
|
|
||||||
import loguru
|
import loguru
|
||||||
|
|
||||||
|
from gsuid_core.config import core_config
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
# avoid sphinx autodoc resolve annotation failed
|
# avoid sphinx autodoc resolve annotation failed
|
||||||
# because loguru module do not have `Logger` class actually
|
# because loguru module do not have `Logger` class actually
|
||||||
@ -39,15 +41,15 @@ FORMAT = (
|
|||||||
"{message}"
|
"{message}"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
LEVEL: str = core_config.get_config("log").get("level", "INFO")
|
||||||
|
|
||||||
logger.remove()
|
logger.remove()
|
||||||
logger_id = logger.add(
|
logger_id = logger.add(sys.stdout, level=LEVEL, diagnose=False, format=FORMAT)
|
||||||
sys.stdout, level=20, diagnose=False, format=FORMAT # INFO
|
|
||||||
)
|
|
||||||
|
|
||||||
logger.add(
|
logger.add(
|
||||||
"logs/{time:YYYY-MM-DD}.log",
|
"logs/{time:YYYY-MM-DD}.log",
|
||||||
rotation=datetime.time(),
|
rotation=datetime.time(),
|
||||||
level=20,
|
level=LEVEL,
|
||||||
diagnose=False,
|
diagnose=False,
|
||||||
format=FORMAT,
|
format=FORMAT,
|
||||||
)
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user