Skip to content

Commit 3ff2140

Browse files
committed
refactor: use LoggerLevel enum
1 parent 66303aa commit 3ff2140

File tree

4 files changed

+22
-4
lines changed

4 files changed

+22
-4
lines changed

api/src/app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def run() -> None:
7777
port=5000,
7878
factory=True,
7979
reload=config.ENVIRONMENT == "local",
80-
log_level=config.LOGGER_LEVEL.lower(),
80+
log_level=config.log_level,
8181
)
8282

8383

api/src/common/logger.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
uvicorn_logger = logging.getLogger("uvicorn")
1010

1111
logger = logging.getLogger("API")
12-
logger.setLevel(config.LOGGER_LEVEL.upper())
12+
logger.setLevel(config.log_level.upper())
1313
formatter = logging.Formatter("%(levelname)s:%(asctime)s %(message)s")
1414
channel = logging.StreamHandler()
1515
channel.setFormatter(formatter)
16-
channel.setLevel(config.LOGGER_LEVEL.upper())
16+
channel.setLevel(config.log_level.upper())
1717
logger.addHandler(channel)

api/src/common/logger_level.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from enum import Enum
2+
3+
4+
class LoggerLevel(Enum):
5+
"""Enum containing the different levels for logging."""
6+
7+
CRITICAL = "critical"
8+
ERROR = "error"
9+
WARNING = "warning"
10+
INFO = "info"
11+
DEBUG = "debug"
12+
TRACE = "trace"

api/src/config.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22
from pydantic_settings import BaseSettings
33

44
from authentication.models import User
5+
from common.logger_level import LoggerLevel
56

67

78
class Config(BaseSettings):
89
# Pydantic-settings in pydantic v2 automatically fetch config settings from env-variables
910
ENVIRONMENT: str = "local"
1011

1112
# Logging
12-
LOGGER_LEVEL: str = Field("INFO", validation_alias="LOGGING_LEVEL", to_lower=True)
13+
LOGGER_LEVEL: LoggerLevel = Field(default=LoggerLevel.INFO)
1314
APPINSIGHTS_CONSTRING: str | None = None
1415

1516
# Database
@@ -36,6 +37,11 @@ class Config(BaseSettings):
3637
OAUTH_AUDIENCE: str = ""
3738
MICROSOFT_AUTH_PROVIDER: str = "login.microsoftonline.com"
3839

40+
@property
41+
def log_level(self) -> str:
42+
"""Returns LOGGER_LEVEL as a (lower case) string."""
43+
return str(self.LOGGER_LEVEL.value).lower()
44+
3945

4046
config = Config()
4147

0 commit comments

Comments
 (0)