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

Refined log redirection code

parent 31c801aa
No related branches found
No related tags found
1 merge request!7Resolve #2021 "03 16 branched from master elk stack"
...@@ -30,7 +30,7 @@ from lofar_logging import device_logging_to_python, log_exceptions ...@@ -30,7 +30,7 @@ from lofar_logging import device_logging_to_python, log_exceptions
__all__ = ["SDP", "main"] __all__ = ["SDP", "main"]
@device_logging_to_python("SDP") @device_logging_to_python({"device": "SDP"})
class SDP(Device): class SDP(Device):
""" """
......
import logging import logging
import tango
from functools import wraps from functools import wraps
def configured_logger(name=''): def configure_logger(logger: logging.Logger, log_extra=None):
logger = logging.getLogger(name)
logger.setLevel(logging.DEBUG) logger.setLevel(logging.DEBUG)
try: 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: except Exception:
logger.exception("Cannot import or configure logstash_async module, not forwarding logs to ELK stack.") logger.exception("Cannot import or configure logstash_async module, not forwarding logs to ELK stack.")
return logger 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. """ """ 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): 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 # Monkey patch the python logger to replace the tango logger
cls.debug_stream = logger.debug cls.debug_stream = logger.debug
cls.info_stream = logger.info cls.info_stream = logger.info
...@@ -34,6 +42,8 @@ def device_logging_to_python(name = ''): ...@@ -34,6 +42,8 @@ def device_logging_to_python(name = ''):
return inner return inner
def log_exceptions(func): def log_exceptions(func):
""" Decorator that logs all exceptions that the function raises. """
@wraps(func) @wraps(func)
def inner(self, *args, **kwargs): def inner(self, *args, **kwargs):
try: try:
......
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