def __init__(self, logger_name): self.logger = logging.getLogger(logger_name) logging.root.setLevel(logging.NOTSET) c = Config().get('log') self.log_file_name = c.get( 'fileName') if c and c.get('fileName') else 'test.log' # 日志文件 self.backup_count = c.get( 'backupCount') if c and c.get('backupCount') else 5 # 保留的日志数量 # 日志输出的级别 self.console_output_level = c.get( 'consoleLevel') if c and c.get('consoleLevel') else 'WARNING' self.file_output_lever = c.get( 'fileLevel') if c and c.get('fileLevel') else 'DEBUG' # 日志输出格式 pattern = c.get('pattern') if c and c.get('pattern') else \ '%(asctime)s - %(name)s - %(levelname)s - %(message)s' self.formatter = logging.Formatter(pattern)
def __di_register(self, config: Config, request: Request): """DIコンテナーへの登録 :param Config config: コンフィグ :param Request request: リクエスト :raise DataFormatError: コンフィグ内のDI定義が不正 """ self.__register_config(config) self.__register_request(request) deffinitions = config.get('di') if deffinitions: for key, deffinition in deffinitions.items(): self.__register_by_deffinition(key, deffinition)
def run(self, config: Config, event: dict) -> dict: """イベントデータから対応するハンドラーを呼び出し、レスポンスを返却 :param Config config: コンフィグ :param dict event: イベントデータ :return dict: レスポンスの連想配列 :raise Error: Error系の例外発生時にメッセージを整形して再出力 """ try: request = self.__build_request(event) Bootstrap(config, request) router = Router(config.get('routes.path')) receiver = router.dispatch(request.url) handler = receiver.instantiate(config, request) return handler().to_dict() except Error as e: raise Exception(f'[{e.code}] {e.__class__.__name__}: {e.message}')
def test_get(self): config = Config('tests/unit/data/fixtures/config/config.yml') self.assertEqual(123, config.get('hoge.fuga.piyo')) self.assertEqual('value', config.get('arr.2.key'))