add "ticker" config component, move LogConfig to __init__

This commit is contained in:
Jörn-Michael Miehe 2022-09-05 13:11:57 +00:00
parent 077ac8efa5
commit 1d62e59052
3 changed files with 42 additions and 31 deletions

View file

@ -7,6 +7,38 @@ This file: Sets up logging.
import logging.config import logging.config
from .config import LogConfig from pydantic import BaseModel
class LogConfig(BaseModel):
"""
Logging configuration to be set for the server.
https://stackoverflow.com/a/67937084
"""
LOG_FORMAT: str = "%(levelprefix)s [%(asctime)s] %(name)s: %(message)s"
LOG_LEVEL: str = "DEBUG"
# Logging config
version = 1
disable_existing_loggers = False
formatters = {
"default": {
"()": "uvicorn.logging.DefaultFormatter",
"fmt": LOG_FORMAT,
"datefmt": "%Y-%m-%d %H:%M:%S",
},
}
handlers = {
"default": {
"formatter": "default",
"class": "logging.StreamHandler",
"stream": "ext://sys.stderr",
},
}
loggers = {
"ovdashboard_api": {"handlers": ["default"], "level": LOG_LEVEL},
}
logging.config.dictConfig(LogConfig().dict()) logging.config.dictConfig(LogConfig().dict())

View file

@ -13,35 +13,13 @@ from webdav3.exceptions import RemoteResourceNotFound
from .dav_file import DavFile from .dav_file import DavFile
class LogConfig(BaseModel): class TickerConfig(BaseModel):
""" """
Logging configuration to be set for the server. Sections "[ticker.*]" in "config.txt".
https://stackoverflow.com/a/67937084
""" """
LOG_FORMAT: str = "%(levelprefix)s [%(asctime)s] %(name)s: %(message)s" separator: str = " +++ "
LOG_LEVEL: str = "DEBUG" comment_marker: str = "#"
# Logging config
version = 1
disable_existing_loggers = False
formatters = {
"default": {
"()": "uvicorn.logging.DefaultFormatter",
"fmt": LOG_FORMAT,
"datefmt": "%Y-%m-%d %H:%M:%S",
},
}
handlers = {
"default": {
"formatter": "default",
"class": "logging.StreamHandler",
"stream": "ext://sys.stderr",
},
}
loggers = {
"ovdashboard_api": {"handlers": ["default"], "level": LOG_LEVEL},
}
class ImageConfig(BaseModel): class ImageConfig(BaseModel):
@ -61,8 +39,7 @@ class Config(BaseModel):
Main representation of "config.txt". Main representation of "config.txt".
""" """
ticker_separator: str = " +++ " ticker: TickerConfig = TickerConfig()
image: ImageConfig = ImageConfig() image: ImageConfig = ImageConfig()
@classmethod @classmethod

View file

@ -46,10 +46,12 @@ async def get_ticker_lines() -> Iterator[str]:
async def get_ticker_content_lines( async def get_ticker_content_lines(
ticker_lines: Iterator[str] = Depends(get_ticker_lines), ticker_lines: Iterator[str] = Depends(get_ticker_lines),
) -> Iterator[str]: ) -> Iterator[str]:
cfg = await Config.get()
return ( return (
line line
for line in ticker_lines for line in ticker_lines
if not line.startswith("#") if not line.startswith(cfg.ticker.comment_marker)
) )
@ -58,7 +60,7 @@ async def get_ticker_content(
) -> str: ) -> str:
cfg = await Config.get() cfg = await Config.get()
ticker_content_lines = ["", *ticker_content_lines, ""] ticker_content_lines = ["", *ticker_content_lines, ""]
ticker_content = cfg.ticker_separator.join(ticker_content_lines) ticker_content = cfg.ticker.separator.join(ticker_content_lines)
return ticker_content.strip() return ticker_content.strip()