feat(logging): honor the LOG_LEVEL environment variable
LOG_LEVEL was documented but never read; the level was fixed at INFO. The JSON handler now sits on the "src" package logger so all service modules share format and level. Invalid values fall back to INFO. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
+30
-5
@@ -35,12 +35,35 @@ class JSONFormatter(logging.Formatter):
|
|||||||
return json.dumps(log_data)
|
return json.dumps(log_data)
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
def get_log_level() -> str:
|
||||||
if not logger.handlers:
|
"""Read the log level from the LOG_LEVEL environment variable.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Upper-case level name; 'INFO' when unset or not a valid level
|
||||||
|
"""
|
||||||
|
level = os.environ.get("LOG_LEVEL", "INFO").strip().upper()
|
||||||
|
if level not in ("DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"):
|
||||||
|
return "INFO"
|
||||||
|
return level
|
||||||
|
|
||||||
|
|
||||||
|
def configure_logging() -> None:
|
||||||
|
"""Attach the JSON handler to the 'src' package logger and set its level.
|
||||||
|
|
||||||
|
Configuring the package logger makes all service modules (extractor,
|
||||||
|
validator, ...) log in the same format and at the same level.
|
||||||
|
"""
|
||||||
|
package_logger = logging.getLogger("src")
|
||||||
|
if not package_logger.handlers:
|
||||||
handler = logging.StreamHandler()
|
handler = logging.StreamHandler()
|
||||||
handler.setFormatter(JSONFormatter())
|
handler.setFormatter(JSONFormatter())
|
||||||
logger.addHandler(handler)
|
package_logger.addHandler(handler)
|
||||||
logger.setLevel(logging.INFO)
|
package_logger.setLevel(get_log_level())
|
||||||
|
|
||||||
|
|
||||||
|
# Fixed name: __name__ is '__main__' when started via `python -m src.main`
|
||||||
|
logger = logging.getLogger("src.main")
|
||||||
|
configure_logging()
|
||||||
|
|
||||||
|
|
||||||
app = FastAPI(
|
app = FastAPI(
|
||||||
@@ -171,4 +194,6 @@ def run(host: str = "0.0.0.0", port: int = 5000) -> None:
|
|||||||
)
|
)
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
uvicorn.run(app, host=args.host, port=args.port)
|
uvicorn.run(
|
||||||
|
app, host=args.host, port=args.port, log_level=get_log_level().lower()
|
||||||
|
)
|
||||||
|
|||||||
+30
-1
@@ -1,9 +1,10 @@
|
|||||||
import base64
|
import base64
|
||||||
|
import logging
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from fastapi.testclient import TestClient
|
from fastapi.testclient import TestClient
|
||||||
|
|
||||||
from src.main import app
|
from src.main import app, configure_logging
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
@@ -93,3 +94,31 @@ def test_validate_pflichtfelder(client):
|
|||||||
def test_validate_empty_checks(client):
|
def test_validate_empty_checks(client):
|
||||||
response = client.post("/validate", json={"xml_data": {}, "checks": []})
|
response = client.post("/validate", json={"xml_data": {}, "checks": []})
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
|
|
||||||
|
|
||||||
|
class TestLogLevel:
|
||||||
|
"""LOG_LEVEL environment variable controls the service log level."""
|
||||||
|
|
||||||
|
@pytest.fixture(autouse=True)
|
||||||
|
def _restore_level(self):
|
||||||
|
yield
|
||||||
|
logging.getLogger("src").setLevel(logging.INFO)
|
||||||
|
|
||||||
|
def test_log_level_from_env(self, monkeypatch):
|
||||||
|
monkeypatch.setenv("LOG_LEVEL", "debug")
|
||||||
|
configure_logging()
|
||||||
|
|
||||||
|
assert logging.getLogger("src").level == logging.DEBUG
|
||||||
|
assert logging.getLogger("src.extractor").getEffectiveLevel() == logging.DEBUG
|
||||||
|
|
||||||
|
def test_default_is_info(self, monkeypatch):
|
||||||
|
monkeypatch.delenv("LOG_LEVEL", raising=False)
|
||||||
|
configure_logging()
|
||||||
|
|
||||||
|
assert logging.getLogger("src").level == logging.INFO
|
||||||
|
|
||||||
|
def test_invalid_value_falls_back_to_info(self, monkeypatch):
|
||||||
|
monkeypatch.setenv("LOG_LEVEL", "chatty")
|
||||||
|
configure_logging()
|
||||||
|
|
||||||
|
assert logging.getLogger("src").level == logging.INFO
|
||||||
|
|||||||
Reference in New Issue
Block a user