Skip to content

Commit

Permalink
feat: add ability to log sql statements
Browse files Browse the repository at this point in the history
  • Loading branch information
arcan1s committed Sep 4, 2024
1 parent 4c4c9b2 commit a0784b7
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
7 changes: 6 additions & 1 deletion package/share/ahriman/settings/ahriman.ini.d/logging.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[loggers]
keys = root,http,stderr,boto3,botocore,nose,s3transfer
keys = root,http,stderr,boto3,botocore,nose,s3transfer,sql

[handlers]
keys = console_handler,journald_handler,syslog_handler
Expand Down Expand Up @@ -64,3 +64,8 @@ propagate = 0
level = INFO
qualname = s3transfer
propagate = 0

[logger_sql]
level = INFO
qualname = sql
propagate = 0
12 changes: 12 additions & 0 deletions src/ahriman/core/database/operations/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,22 @@ def __init__(self, path: Path, repository_id: RepositoryId, repository_paths: Re
Args:
path(Path): path to the database file
repository_id(RepositoryId): repository unique identifier
repository_paths(RepositoryPaths): repository paths
"""
self.path = path
self._repository_id = repository_id
self._repository_paths = repository_paths

@property
def logger_name(self) -> str:
"""
extract logger name for the class
Returns:
str: logger name override
"""
return "sql"

@staticmethod
def factory(cursor: sqlite3.Cursor, row: tuple[Any, ...]) -> dict[str, Any]:
"""
Expand Down Expand Up @@ -80,6 +91,7 @@ def with_connection(self, query: Callable[[sqlite3.Connection], T], *, commit: b
T: result of the ``query`` call
"""
with sqlite3.connect(self.path, detect_types=sqlite3.PARSE_DECLTYPES) as connection:
connection.set_trace_callback(self.logger.debug)
connection.row_factory = self.factory
result = query(connection)
if commit:
Expand Down
8 changes: 8 additions & 0 deletions tests/ahriman/core/database/operations/test_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
from ahriman.core.database import SQLite


def test_logger_name(database: SQLite) -> None:
"""
must return correct logger name
"""
assert database.logger_name == "sql"


def test_factory(database: SQLite) -> None:
"""
must convert response to dictionary
Expand All @@ -24,6 +31,7 @@ def test_with_connection(database: SQLite, mocker: MockerFixture) -> None:

database.with_connection(lambda conn: conn.execute("select 1"))
connect_mock.assert_called_once_with(database.path, detect_types=sqlite3.PARSE_DECLTYPES)
connection_mock.__enter__().set_trace_callback.assert_called_once_with(database.logger.debug)
connection_mock.__enter__().commit.assert_not_called()


Expand Down
13 changes: 9 additions & 4 deletions tests/ahriman/core/log/test_lazy_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@
from pytest_mock import MockerFixture

from ahriman.core.alpm.repo import Repo
from ahriman.core.build_tools.task import Task
from ahriman.core.database import SQLite
from ahriman.models.log_record_id import LogRecordId
from ahriman.models.package import Package


def test_logger_name(database: SQLite, repo: Repo) -> None:
def test_logger_name(database: SQLite, repo: Repo, task_ahriman: Task) -> None:
"""
must correctly generate logger name
"""
assert database.logger_name == "ahriman.core.database.sqlite.SQLite"
assert database.logger_name == "sql"
assert repo.logger_name == "ahriman.core.alpm.repo.Repo"
assert task_ahriman.logger_name == "ahriman.core.build_tools.task.Task"


def test_package_logger_set_reset(database: SQLite) -> None:
Expand Down Expand Up @@ -75,9 +77,12 @@ def test_in_package_context_failed(database: SQLite, package_ahriman: Package, m
reset_mock.assert_called_once_with()


def test_logger(database: SQLite) -> None:
def test_logger(database: SQLite, repo: Repo) -> None:
"""
must set logger attribute
"""
assert database.logger
assert database.logger.name == "ahriman.core.database.sqlite.SQLite"
assert database.logger.name == "sql"

assert repo.logger
assert repo.logger.name == "ahriman.core.alpm.repo.Repo"

0 comments on commit a0784b7

Please sign in to comment.