fix(fastapi): centralize logging initialization

With this change, we provide a wrapper to `logging.getLogger`
in the `aurweb.logging` module. Modules wishing to log using
logging.conf should get their module-local loggers by calling
`aurweb.logging.getLogger(__name__)`, similar to `logging.getLogger`,
this way initialization with logging.conf is guaranteed.

Signed-off-by: Kevin Morris <kevr@0cost.org>
This commit is contained in:
Kevin Morris 2021-10-21 10:08:45 -07:00
parent 5ae9d09e98
commit a06f4ec19c
No known key found for this signature in database
GPG key ID: F7E46DED420788F3
10 changed files with 46 additions and 23 deletions

View file

@ -1,4 +1,3 @@
import logging
import re
import tempfile
@ -11,7 +10,7 @@ import pytest
from fastapi.testclient import TestClient
from aurweb import captcha, db
from aurweb import captcha, db, logging
from aurweb.asgi import app
from aurweb.db import create, query
from aurweb.models.accepted_term import AcceptedTerm
@ -32,7 +31,7 @@ TEST_EMAIL = "test@example.org"
client = TestClient(app)
user = None
logger = logging.getLogger(__name__)
logger = logging.get_logger(__name__)
def make_ssh_pubkey():

16
test/test_logging.py Normal file
View file

@ -0,0 +1,16 @@
from aurweb import logging
logger = logging.get_logger(__name__)
def test_logging(caplog):
logger.info("Test log.")
# Test that we logged once.
assert len(caplog.records) == 1
# Test that our log record was of INFO level.
assert caplog.records[0].levelname == "INFO"
# Test that our message got logged.
assert "Test log." in caplog.text

View file

@ -1,5 +1,3 @@
import logging
from datetime import datetime
from http import HTTPStatus
@ -8,7 +6,7 @@ import pytest
from fastapi.testclient import TestClient
from aurweb import db
from aurweb import db, logging
from aurweb.asgi import app
from aurweb.models.account_type import AccountType
from aurweb.models.package import Package
@ -16,7 +14,7 @@ from aurweb.models.package_base import PackageBase
from aurweb.models.user import User
from aurweb.testing import setup_test_db
logger = logging.getLogger(__name__)
logger = logging.get_logger(__name__)
@pytest.fixture(autouse=True)