-
Notifications
You must be signed in to change notification settings - Fork 8k
/
Copy pathconfig.py
70 lines (51 loc) · 2.15 KB
/
config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import logging
import sys
from types import FrameType
from typing import List, cast
from loguru import logger
from pydantic import AnyHttpUrl, BaseSettings
class LoggingSettings(BaseSettings):
LOGGING_LEVEL: int = logging.INFO # logging levels are type int
class Settings(BaseSettings):
API_V1_STR: str = "/api/v1"
# Meta
logging: LoggingSettings = LoggingSettings()
# BACKEND_CORS_ORIGINS is a comma-separated list of origins
# e.g: http://localhost,http://localhost:4200,http://localhost:3000
BACKEND_CORS_ORIGINS: List[AnyHttpUrl] = [
"http://localhost:3000", # type: ignore
"http://localhost:8000", # type: ignore
"https://localhost:3000", # type: ignore
"https://localhost:8000", # type: ignore
]
PROJECT_NAME: str = "house-prices-api"
class Config:
case_sensitive = True
# See: https://loguru.readthedocs.io/en/stable/overview.html#entirely-compatible-with-standard-logging # noqa
class InterceptHandler(logging.Handler):
def emit(self, record: logging.LogRecord) -> None: # pragma: no cover
# Get corresponding Loguru level if it exists
try:
level = logger.level(record.levelname).name
except ValueError:
level = str(record.levelno)
# Find caller from where originated the logged message
frame, depth = logging.currentframe(), 2
while frame.f_code.co_filename == logging.__file__: # noqa: WPS609
frame = cast(FrameType, frame.f_back)
depth += 1
logger.opt(depth=depth, exception=record.exc_info).log(
level,
record.getMessage(),
)
def setup_app_logging(config: Settings) -> None:
"""Prepare custom logging for our application."""
LOGGERS = ("uvicorn.asgi", "uvicorn.access")
logging.getLogger().handlers = [InterceptHandler()]
for logger_name in LOGGERS:
logging_logger = logging.getLogger(logger_name)
logging_logger.handlers = [InterceptHandler(level=config.logging.LOGGING_LEVEL)]
logger.configure(
handlers=[{"sink": sys.stderr, "level": config.logging.LOGGING_LEVEL}]
)
settings = Settings()