diff --git a/devices/common/lofar_logging.py b/devices/common/lofar_logging.py index c605d8cf927f890083dafc3ec85a16c1dab70d9d..e571ebb1f92c87f7963a2c8c8f623ed79346f068 100644 --- a/devices/common/lofar_logging.py +++ b/devices/common/lofar_logging.py @@ -94,7 +94,7 @@ class LogAnnotator(logging.Formatter): # we just annotate, we don't filter return True -def configure_logger(logger: logging.Logger=None, log_extra=None): +def configure_logger(logger: logging.Logger=None, log_extra=None, debug=False): """ Configure the given logger (or root if None) to: - send logs to the ELK stack @@ -114,6 +114,26 @@ def configure_logger(logger: logging.Logger=None, log_extra=None): # remove spam from the OPC-UA client connection logging.getLogger("opcua").setLevel(logging.WARN) + # 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() + + # Always also log the hostname because it makes the origin of the log clear. + hostname = socket.gethostname() + + formatter = logging.Formatter(fmt = '%(asctime)s.%(msecs)d %(levelname)s - HOST="{}" DEVICE="%(tango_device)s" PID="%(process)d" TNAME="%(threadName)s" FILE="%(pathname)s" LINE="%(lineno)d" FUNC="%(funcName)s" MSG="%(message)s"'.format(hostname), datefmt = '%Y-%m-%dT%H:%M:%S') + handler.setFormatter(formatter) + handler.addFilter(LogSuppressErrorSpam()) + handler.addFilter(LogAnnotator()) + + logger.addHandler(handler) + + # If configuring for debug; exit early + if debug: + return logger + # Log to ELK stack try: from logstash_async.handler import AsynchronousLogstashHandler, LogstashFormatter @@ -143,23 +163,6 @@ def configure_logger(logger: logging.Logger=None, log_extra=None): except Exception: logger.exception("Cannot forward logs to Tango.") - - # 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() - - # Always also log the hostname because it makes the origin of the log clear. - hostname = socket.gethostname() - - formatter = logging.Formatter(fmt = '%(asctime)s.%(msecs)d %(levelname)s - HOST="{}" DEVICE="%(tango_device)s" PID="%(process)d" TNAME="%(threadName)s" FILE="%(pathname)s" LINE="%(lineno)d" FUNC="%(funcName)s" MSG="%(message)s"'.format(hostname), datefmt = '%Y-%m-%dT%H:%M:%S') - handler.setFormatter(formatter) - handler.addFilter(LogSuppressErrorSpam()) - handler.addFilter(LogAnnotator()) - - logger.addHandler(handler) - return logger def device_logging_to_python(): diff --git a/devices/integration_test/base.py b/devices/integration_test/base.py index 92601ec2d440753ae7f7be22fcbfad0c5028875c..085cbc540dba035969685c3a0fbfbef8c6c7e394 100644 --- a/devices/integration_test/base.py +++ b/devices/integration_test/base.py @@ -7,9 +7,14 @@ # Distributed under the terms of the APACHE license. # See LICENSE.txt for more info. +from common.lofar_logging import configure_logger + import unittest import testscenarios +"""Setup logging for integration tests""" +configure_logger(debug=True) + class BaseIntegrationTestCase(testscenarios.WithScenarios, unittest.TestCase): """Integration test base class.""" diff --git a/devices/test/base.py b/devices/test/base.py index 2bcbf59b33b605ba15faa0ad71c0fd53d80274ff..aecaaebc3b57909c49e0425d755f52f5028e0ded 100644 --- a/devices/test/base.py +++ b/devices/test/base.py @@ -7,9 +7,14 @@ # Distributed under the terms of the APACHE license. # See LICENSE.txt for more info. +from common.lofar_logging import configure_logger + import unittest import testscenarios +"""Setup logging for unit tests""" +configure_logger(debug=True) + class BaseTestCase(testscenarios.WithScenarios, unittest.TestCase): """Test base class."""