feat: log

This commit is contained in:
superobk
2024-04-30 17:59:37 +08:00
parent 884a835cae
commit 54ddb8ee4a
9 changed files with 81 additions and 31 deletions

View File

@ -1,9 +1,10 @@
from dataclasses import dataclass
from injector import Injector, inject
from injector import inject,singleton
import yaml
import sys
import logging
@singleton
class Configuration():
@inject
@ -26,7 +27,7 @@ class Configuration():
bbb:
ccc: "hello world"
"""
def get(self, path: str | list[str], cfg: dict = None):
def get(self, path: str | list[str], cfg: dict = None, default = None):
if isinstance(path, str):
if cfg is None:
cfg = self.cfg
@ -36,9 +37,8 @@ class Configuration():
return None
if length == 1:
return cfg.get(path[0])
return self.get(path[1:], cfg.get(path[0]))
return self.get(path[1:], cfg.get(path[0]), default=default)
class TesouConf():
url: str
@ -54,9 +54,37 @@ class MeloConf():
def __init__(self, config: Configuration) -> None:
self.melotts = config.get("melotts.url")
# 'CRITICAL': CRITICAL,
# 'FATAL': FATAL,
# 'ERROR': ERROR,
# 'WARN': WARNING,
# 'WARNING': WARNING,
# 'INFO': INFO,
# 'DEBUG': DEBUG,
# 'NOTSET': NOTSET,
DEFAULT_LEVEL="WARNING"
DEFAULT_TIME_FORMAT="%Y-%m-%d %H:%M:%S"
@singleton
class LogConf():
level: str
level: int
time_format = "%Y-%m-%d %H:%M:%S"
@inject
def __init__(self,config: Configuration) -> None:
self.level = config.get("log.level")
c = config.get("log.level", default=DEFAULT_LEVEL).upper()
level=logging._nameToLevel.get(c)
if level is None:
self.level = logging.WARNING
else:
self.level = level
self.time_format=config.get("log.time_format", default=DEFAULT_TIME_FORMAT)
@singleton
class EnvConf():
version: str
@inject
def __init__(self, config: Configuration) -> None:
self.version = config.get("env.version")