Skip to content
Snippets Groups Projects
Commit 31c801aa authored by Jan David Mol's avatar Jan David Mol
Browse files

Improve logger rerouting and added decorator to log exceptions.

parent 12b7195d
No related branches found
No related tags found
1 merge request!7Resolve #2021 "03 16 branched from master elk stack"
...@@ -26,11 +26,12 @@ import numpy ...@@ -26,11 +26,12 @@ import numpy
from wrappers import only_in_states, only_when_on, fault_on_error from wrappers import only_in_states, only_when_on, fault_on_error
from opcua_connection import OPCUAConnection from opcua_connection import OPCUAConnection
from lofar_logging import PythonLoggingMixin from lofar_logging import device_logging_to_python, log_exceptions
__all__ = ["SDP", "main"] __all__ = ["SDP", "main"]
class SDP(Device, PythonLoggingMixin): @device_logging_to_python("SDP")
class SDP(Device):
""" """
**Properties:** **Properties:**
...@@ -193,6 +194,8 @@ class SDP(Device, PythonLoggingMixin): ...@@ -193,6 +194,8 @@ class SDP(Device, PythonLoggingMixin):
self.info_stream("Mapping OPC-UA MP/CP to attributes done.") self.info_stream("Mapping OPC-UA MP/CP to attributes done.")
@log_exceptions
@DebugIt()
def init_device(self): def init_device(self):
""" Instantiates the device in the OFF state. """ """ Instantiates the device in the OFF state. """
...@@ -201,6 +204,7 @@ class SDP(Device, PythonLoggingMixin): ...@@ -201,6 +204,7 @@ class SDP(Device, PythonLoggingMixin):
self.set_state(DevState.OFF) self.set_state(DevState.OFF)
@log_exceptions
def initialise(self): def initialise(self):
"""Initialises the attributes and properties of the SDP.""" """Initialises the attributes and properties of the SDP."""
...@@ -476,7 +480,6 @@ class SDP(Device, PythonLoggingMixin): ...@@ -476,7 +480,6 @@ class SDP(Device, PythonLoggingMixin):
""" """
self.set_state(DevState.FAULT) self.set_state(DevState.FAULT)
# ---------- # ----------
# Run server # Run server
# ---------- # ----------
......
import logging import logging
import tango
from functools import wraps
def configured_logger(name=''): def configured_logger(name=''):
logger = logging.getLogger(name) logger = logging.getLogger(name)
...@@ -13,17 +15,31 @@ def configured_logger(name=''): ...@@ -13,17 +15,31 @@ def configured_logger(name=''):
return logger return logger
logger = configured_logger() def device_logging_to_python(name = ''):
""" Call this on a Tango Device instance or class to have your Tango Device log to python instead. """
class PythonLoggingMixin(object):
""" Also inherit this class to have your Tango Device log to python instead. """ logger = configured_logger(name)
# Monkey patch the python logger to replace the tango logger def inner(cls):
debug_stream = logger.debug # Monkey patch the python logger to replace the tango logger
info_stream = logger.info cls.debug_stream = logger.debug
warn_stream = logger.warning cls.info_stream = logger.info
warning_stream = logger.warning cls.warn_stream = logger.warning
error_stream = logger.error cls.warning_stream = logger.warning
fatal_stream = logger.fatal cls.error_stream = logger.error
critical_stream = logger.critical cls.fatal_stream = logger.fatal
cls.critical_stream = logger.critical
return cls
return inner
def log_exceptions(func):
@wraps(func)
def inner(self, *args, **kwargs):
try:
return func(self, *args, **kwargs)
except Exception as e:
self.error_stream("Caught exception: %s: %s", e.__class__.__name__, e, exc_info=1)
raise e
return inner
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment