Event logger
In Getting Started we have seen that
log_events() is a quick utility
to live-log events from a set of devices and attributes.
If you wish to make something more sophisticated, you can use directly the
TangoEventLogger class.
When subscribing to events with
log_events_from_device(),
method, this class permits you to:
add a filter (similar to
query_events()predicate) to select only some events instead of all of them (usingfiltering_ruleparameter);customize the message format providing a custom function that generates a string message starting from a
ReceivedEventobject (usingmessage_builderparameter);
Usage example:
from ska_tango_testing import TangoEventLogger
def test_with_logger():
logger = TangoEventLogger()
# log all events from attribute "attr" of device "A"
logger.log_events_from_device("A", "attr")
# log only events from attribute "attr2" of device "A"
# when value > 10
logger.log_events_from_device(
"A", "attr2",
filtering_rule=lambda e: e.attribute_value > 10
)
# display a custom message when "B" changes its state
logger.log_events_from_device(
"B", "State",
message_builder=lambda e:
f"B STATE CHANGED INTO {e.attribute_value}"
)
Comments to the code
the first call to
log_events_from_device()works exactly as thelog_events()function, but just for one device and one attribute;the second and the third calls show how to use the
filtering_ruleand themessage_builderparameters to customize the logging behaviour on the specified subscription;both
filtering_ruleandmessage_builderare optional parameters, the first essentially defaults to “no filter” (seeDEFAULT_LOG_ALL_EVENTS()) and the second to a basic message format that includes the device name, the attribute name, the attribute value and the timestamp of the event (seeDEFAULT_LOG_MESSAGE_BUILDER());in this example both
filtering_ruleandmessage_builderare lambda functions, but you can define them as you prefer (e.g. as named functions using thedefstatement).
For more details, see API Documentation.