diff --git a/SDP/SDP/SDP.py b/SDP/SDP/SDP.py index 3dffae225596c61521835463c45c0176716152bd..ee3209e07c141d3b029198bb148c5d15d920eb4c 100644 --- a/SDP/SDP/SDP.py +++ b/SDP/SDP/SDP.py @@ -30,7 +30,7 @@ from lofar_logging import device_logging_to_python, log_exceptions __all__ = ["SDP", "main"] -@device_logging_to_python("SDP") +@device_logging_to_python({"device": "SDP"}) class SDP(Device): """ diff --git a/SDP/SDP/lofar_logging.py b/SDP/SDP/lofar_logging.py index 4330985c8f873583e20abbcbbb5a7b3252546c97..5c3996dddeb5094621659b99e8ccae03a263f387 100644 --- a/SDP/SDP/lofar_logging.py +++ b/SDP/SDP/lofar_logging.py @@ -1,26 +1,34 @@ import logging -import tango from functools import wraps -def configured_logger(name=''): - logger = logging.getLogger(name) +def configure_logger(logger: logging.Logger, log_extra=None): logger.setLevel(logging.DEBUG) try: - from logstash_async.handler import AsynchronousLogstashHandler + from logstash_async.handler import AsynchronousLogstashHandler, LogstashFormatter - logger.addHandler(AsynchronousLogstashHandler("elk", 5959, database_path='logstash_test.db')) + # log to the tcp_input of logstash in our ELK stack + handler = AsynchronousLogstashHandler("elk", 5959, database_path='pending_log_messages.db') + + # configure log messages + formatter = LogstashFormatter(extra=log_extra, tags=["python", "lofar"]) + handler.setFormatter(formatter) + + # install the handler + logger.addHandler(handler) except Exception: logger.exception("Cannot import or configure logstash_async module, not forwarding logs to ELK stack.") return logger -def device_logging_to_python(name = ''): +def device_logging_to_python(log_extra: dict = None): """ Call this on a Tango Device instance or class to have your Tango Device log to python instead. """ - logger = configured_logger(name) - def inner(cls): + # Create a logger that logs to ELK, dedicated for this class + logger = logging.getLogger(cls.__name__) + configure_logger(logger, log_extra) + # Monkey patch the python logger to replace the tango logger cls.debug_stream = logger.debug cls.info_stream = logger.info @@ -34,6 +42,8 @@ def device_logging_to_python(name = ''): return inner def log_exceptions(func): + """ Decorator that logs all exceptions that the function raises. """ + @wraps(func) def inner(self, *args, **kwargs): try: