diff --git a/src/ska/logging/transactions.py b/src/ska/logging/transactions.py index acf865f5d7cb6375482dcd12194199b375d99156..312990591e7edbc63f23785b6893104847a45c59 100644 --- a/src/ska/logging/transactions.py +++ b/src/ska/logging/transactions.py @@ -5,7 +5,7 @@ import json import logging import os -from typing import Mapping, Text +from typing import Mapping, Optional, Text from ska.skuid.client import SkuidClient @@ -63,11 +63,48 @@ class Transaction: params: dict = {}, transaction_id: str = "", transaction_id_key: str = "transaction_id", + logger: Optional[logging.Logger] = None, ): + """Create the transaction context handler. + + A new transaction ID is generated if none is passed in via `transaction_id` or + in `params`. + + If there is a transaction ID in `params` and `transaction_id` is also passed in + then the passed in `transaction_id` will take precedence. + + By default the key `transaction_id` will be used to get a transaction ID out of + `params`. If a different key is required then `transaction_id_key` can be + specified. + + Parameters + ---------- + name : str + A description for the context. This is usually the Tango device command. + params : dict, optional + The parameters will be logged and will be used to retrieve the transaction + ID if `transaction_id` is not passed in, by default {} + transaction_id : str, optional + The transaction ID to be used for the context, by default "" + transaction_id_key : str, optional + The key to use to get the transaction ID from params, + by default "transaction_id" + logger : logging.Logger, optional + The logger to use for logging, by default None. + If no logger is specified a new one named `ska.transaction` will be used. + + Raises + ------ + TransactionParamsError + If the `params` passed in is not valid. + """ if not isinstance(params, Mapping): raise TransactionParamsError("params must be dict-like (Mapping)") - # Get the root logger - self.logger = logging.getLogger() + + if logger: + self.logger = logger + else: + self.logger = logging.getLogger("ska.transaction") self._name = name self._params = params self._transaction_id_key = transaction_id_key diff --git a/tests/test_transactions.py b/tests/test_transactions.py index 4178cd9024dfb6c3e8f0b810f475d6353663d45b..d96a38b25c3d5c8b01f2f8c18d33573040756d22 100644 --- a/tests/test_transactions.py +++ b/tests/test_transactions.py @@ -138,6 +138,15 @@ class TestTransactionLogging: assert 0, f"RuntimeError and transaction tag not found in exception logs: {record_logs}" + def test_specified_logger(self): + logger = MagicMock() + parameters = {} + with transaction("name", parameters, logger=logger) as transaction_id: + logger.info("A message") + for i, message in enumerate(["Generated", "Enter", "A message", "Exit"]): + assert logger.info.call_args_list[i].starts_with(message) + assert logger.info.call_count == 4, f"Log calls incorrect {logger.info.call_args_list}" + class TestTransactionIdGenerator: """Tests for :class:`~ska.logging.transactions.TransactionIdGenerator`."""