From a0784b7af138de686e8d677e88d7b6f0d461c72b Mon Sep 17 00:00:00 2001 From: Evgenii Alekseev Date: Wed, 28 Aug 2024 18:13:53 +0300 Subject: [PATCH] feat: add ability to log sql statements --- .../ahriman/settings/ahriman.ini.d/logging.ini | 7 ++++++- src/ahriman/core/database/operations/operations.py | 12 ++++++++++++ .../core/database/operations/test_operations.py | 8 ++++++++ tests/ahriman/core/log/test_lazy_logging.py | 13 +++++++++---- 4 files changed, 35 insertions(+), 5 deletions(-) diff --git a/package/share/ahriman/settings/ahriman.ini.d/logging.ini b/package/share/ahriman/settings/ahriman.ini.d/logging.ini index fce4b390..e120a037 100644 --- a/package/share/ahriman/settings/ahriman.ini.d/logging.ini +++ b/package/share/ahriman/settings/ahriman.ini.d/logging.ini @@ -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 @@ -64,3 +64,8 @@ propagate = 0 level = INFO qualname = s3transfer propagate = 0 + +[logger_sql] +level = INFO +qualname = sql +propagate = 0 diff --git a/src/ahriman/core/database/operations/operations.py b/src/ahriman/core/database/operations/operations.py index 636d0ca5..30d11e98 100644 --- a/src/ahriman/core/database/operations/operations.py +++ b/src/ahriman/core/database/operations/operations.py @@ -46,11 +46,22 @@ class Operations(LazyLogging): 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]: """ @@ -80,6 +91,7 @@ class Operations(LazyLogging): 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: diff --git a/tests/ahriman/core/database/operations/test_operations.py b/tests/ahriman/core/database/operations/test_operations.py index e6362c5f..8143ccd6 100644 --- a/tests/ahriman/core/database/operations/test_operations.py +++ b/tests/ahriman/core/database/operations/test_operations.py @@ -6,6 +6,13 @@ from unittest.mock import MagicMock 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 @@ -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() diff --git a/tests/ahriman/core/log/test_lazy_logging.py b/tests/ahriman/core/log/test_lazy_logging.py index d4383428..acd5521b 100644 --- a/tests/ahriman/core/log/test_lazy_logging.py +++ b/tests/ahriman/core/log/test_lazy_logging.py @@ -4,17 +4,19 @@ import pytest 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: @@ -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"