Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add env var for logging level #3751

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion boefjes/boefjes/clients/scheduler_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from httpx import Client, HTTPTransport, Response
from pydantic import BaseModel, TypeAdapter

from boefjes.config import settings
from boefjes.job_models import BoefjeMeta, NormalizerMeta


Expand Down Expand Up @@ -57,7 +58,7 @@ def push_item(self, p_item: Task) -> None:

class SchedulerAPIClient(SchedulerClientInterface):
def __init__(self, base_url: str):
self._session = Client(base_url=base_url, transport=HTTPTransport(retries=6))
self._session = Client(base_url=base_url, transport=HTTPTransport(retries=6), timeout=settings.httpx_timeout)

@staticmethod
def _verify_response(response: Response) -> None:
Expand Down
2 changes: 2 additions & 0 deletions boefjes/boefjes/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ class Settings(BaseSettings):

logging_format: Literal["text", "json"] = Field("text", description="Logging format")

httpx_timeout: int = Field(30, description="httpx timeout")

model_config = SettingsConfigDict(env_prefix="BOEFJES_")

@classmethod
Expand Down
4 changes: 1 addition & 3 deletions boefjes/boefjes/job_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ def get_environment_settings(boefje_meta: BoefjeMeta, schema: dict | None = None

allowed_keys = schema.get("properties", []) if schema else []
new_env = {
key.split("BOEFJE_", 1)[1]: value
for key, value in os.environ.items()
if key.startswith("BOEFJE_") and key in allowed_keys
key[7:]: value for key, value in os.environ.items() if key.startswith("BOEFJE_") and key[7:] not in allowed_keys
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new_env = {key.removeprefix("BOEFJE_"): value for key, value in os.environ.items() if key.startswith("BOEFJE_") and key.removeprefix("BOEFJE_") not in allowed_keys}

}

settings_from_katalogus = response.json()
Expand Down
1 change: 1 addition & 0 deletions mula/scheduler/config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class Settings(BaseSettings):
debug: bool = Field(False, alias="DEBUG", description="Enables/disables global debugging mode")

log_cfg: Path = Field(BASE_DIR / "logging.json", description="Path to the logging configuration file")
log_level: str = Field("INFO", description="Logging level")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
log_level: str = Field("INFO", description="Logging level")
log_level: Literal["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"] = Field("INFO", description="Logging level")


collect_metrics: bool = Field(
False, description="Enables/disables the collection of metrics to be used with tools like Prometheus"
Expand Down
8 changes: 8 additions & 0 deletions mula/scheduler/context/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ def __init__(self) -> None:
with Path(self.config.log_cfg).open("rt", encoding="utf-8") as f:
logging.config.dictConfig(json.load(f))

# Set the logging level to the value specified by the env var
level = logging.getLevelName(self.config.log_level.upper())
root_logger = logging.getLogger()
root_logger.setLevel(level)
root_logger.handlers[0].setLevel(level)
logging.getLogger("uvicorn.access").setLevel(level)
logging.getLogger("uvicorn.error").setLevel(level)
Comment on lines +45 to +51
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned offline and maybe for later: we could extend the dict config with this, so we'd have a consistent way of loading the logging configuration and printing out the logging configuration


# Check if we enabled structured logging in the configuration
if self.config.logging_format == "json":
structlog.configure(
Expand Down
Loading