Skip to content
Snippets Groups Projects
Unverified Commit b1c063e6 authored by SKAJohanVenter's avatar SKAJohanVenter
Browse files

SAR-149 Added optional logger for transaction context and test

parent c3aac0e8
No related branches found
No related tags found
No related merge requests found
...@@ -5,7 +5,7 @@ import json ...@@ -5,7 +5,7 @@ import json
import logging import logging
import os import os
from typing import Mapping, Text from typing import Mapping, Optional, Text
from ska.skuid.client import SkuidClient from ska.skuid.client import SkuidClient
...@@ -63,11 +63,48 @@ class Transaction: ...@@ -63,11 +63,48 @@ class Transaction:
params: dict = {}, params: dict = {},
transaction_id: str = "", transaction_id: str = "",
transaction_id_key: str = "transaction_id", 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): if not isinstance(params, Mapping):
raise TransactionParamsError("params must be dict-like (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._name = name
self._params = params self._params = params
self._transaction_id_key = transaction_id_key self._transaction_id_key = transaction_id_key
......
...@@ -138,6 +138,15 @@ class TestTransactionLogging: ...@@ -138,6 +138,15 @@ class TestTransactionLogging:
assert 0, f"RuntimeError and transaction tag not found in exception logs: {record_logs}" 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: class TestTransactionIdGenerator:
"""Tests for :class:`~ska.logging.transactions.TransactionIdGenerator`.""" """Tests for :class:`~ska.logging.transactions.TransactionIdGenerator`."""
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment