Skip to content
Snippets Groups Projects
Select Git revision
  • 75c78bc76b65b417e2d0524dbc4b2010c9f9fa38
  • master default protected
  • L2SS-2199-apply-dab-to-xy
  • L2SS-2417-more-vector-memory
  • test-pytango-10.0.3
  • revert-cs032-ccd-ip
  • deploy-components-parallel
  • fix-chrony-exporter
  • L2SS-2407-swap-iers-caltable-monitoring-port
  • L2SS-2357-fix-ruff
  • sync-up-with-meta-pypcc
  • stabilise-landing-page
  • all-stations-lofar2
  • v0.39.7-backports
  • Move-sdptr-to-v1.5.0
  • fix-build-ubuntu
  • tokens-in-env-files
  • fix-build
  • L2SS-2214-deploy-cdb
  • fix-missing-init
  • add-power-hardware-apply
  • v0.55.5-r2 protected
  • v0.52.8-rc1 protected
  • v0.55.5 protected
  • v0.55.4 protected
  • 0.55.2.dev0
  • 0.55.1.dev0
  • 0.55.0.dev0
  • v0.54.0 protected
  • 0.53.2.dev0
  • 0.53.1.dev0
  • v0.52.3-r2 protected
  • remove-snmp-client
  • v0.52.3 protected
  • v0.52.3dev0 protected
  • 0.53.1dev0
  • v0.52.2-rc3 protected
  • v0.52.2-rc2 protected
  • v0.52.2-rc1 protected
  • v0.52.1.1 protected
  • v0.52.1 protected
41 results

lofar_logging.py

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    lofar_logging.py 2.68 KiB
    import logging
    from functools import wraps
    import sys
    
    # Always also log the hostname because it makes the origin of the log clear.
    import socket
    hostname = socket.gethostname()
    
    def configure_logger(logger: logging.Logger, log_extra=None):
        logger.setLevel(logging.DEBUG)
    
        try:
            from logstash_async.handler import AsynchronousLogstashHandler, LogstashFormatter
    
            # 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)
    
            # for now, also log to stderr
            # Set up logging in a way that it can be understood by a human reader, be
            # easily grep'ed, be parsed with a couple of shell commands and
            # easily fed into an Kibana/Elastic search system.
            handler = logging.StreamHandler()
            formatter = logging.Formatter(fmt = '%(asctime)s.%(msecs)d %(levelname)s - HOST="{}" PID="%(process)d" TNAME="%(threadName)s" TID="%(thread)d" FILE="%(pathname)s" LINE="%(lineno)d" FUNC="%(funcName)s" MSG="%(message)s"'.format(hostname), datefmt = '%Y-%m-%dT%H:%M:%S')
            handler.setFormatter(formatter)
            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(log_extra: dict = None):
        """ Call this on a Tango Device instance or class to have your Tango Device log to python instead. """
    
        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
            cls.warn_stream = logger.warning
            cls.warning_stream = logger.warning
            cls.error_stream = logger.error
            cls.fatal_stream = logger.fatal
            cls.critical_stream = logger.critical
            return cls
    
        return inner
    
    def log_exceptions():
        """ Decorator that logs all exceptions that the function raises. """
    
        def wrapper(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
    
        return wrapper