From 21f19be8ab2b8d555d161801d5e0ddb93e09691c Mon Sep 17 00:00:00 2001 From: Oscar Krause Date: Thu, 13 Jun 2024 20:16:51 +0200 Subject: [PATCH] code styling & improved logging --- app/main.py | 4 +++- app/util.py | 16 +++++++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/app/main.py b/app/main.py index bbeb3b8..adafd34 100644 --- a/app/main.py +++ b/app/main.py @@ -60,9 +60,11 @@ app.add_middleware( allow_headers=['*'], ) +LOG_LEVEL = logging.DEBUG if DEBUG else logging.INFO logging.basicConfig() logger = logging.getLogger(__name__) -logger.setLevel(logging.DEBUG if DEBUG else logging.INFO) +logger.setLevel(LOG_LEVEL) +logging.getLogger('util').setLevel(LOG_LEVEL) def __get_token(request: Request) -> dict: diff --git a/app/util.py b/app/util.py index ca3eec6..530e5c0 100644 --- a/app/util.py +++ b/app/util.py @@ -1,10 +1,17 @@ -def load_file(filename) -> bytes: +import logging + +logging.basicConfig() + + +def load_file(filename: str) -> bytes: + log = logging.getLogger(f'{__name__}') + log.debug(f'Loading contents of file "{filename}') with open(filename, 'rb') as file: content = file.read() return content -def load_key(filename) -> "RsaKey": +def load_key(filename: str) -> "RsaKey": try: # Crypto | Cryptodome on Debian from Crypto.PublicKey import RSA @@ -13,6 +20,8 @@ def load_key(filename) -> "RsaKey": from Cryptodome.PublicKey import RSA from Cryptodome.PublicKey.RSA import RsaKey + log = logging.getLogger(__name__) + log.debug(f'Importing RSA-Key from "{filename}"') return RSA.import_key(extern_key=load_file(filename), passphrase=None) @@ -24,5 +33,6 @@ def generate_key() -> "RsaKey": except ModuleNotFoundError: from Cryptodome.PublicKey import RSA from Cryptodome.PublicKey.RSA import RsaKey - + log = logging.getLogger(__name__) + log.debug(f'Generating RSA-Key') return RSA.generate(bits=2048)