Skip to content
Snippets Groups Projects
Commit 69590e3e authored by Corné Lukken's avatar Corné Lukken
Browse files

L2SS-389: Improve unit / integration test logging approach

parent 5c165216
No related branches found
No related tags found
1 merge request!132L2SS-389: Very naive approach to setup loggers for tests
......@@ -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():
......
......@@ -12,17 +12,9 @@ from common.lofar_logging import configure_logger
import unittest
import testscenarios
# TODO(Corne): Move to separate test file, parameterize for configuration
class TestLogger:
"""Ensure logger is configured only once per memory space"""
_logger = None
"""Setup logging for integration tests"""
configure_logger(debug=True)
def __init__(self):
if self._logger is None:
self._logger = configure_logger()
"""Setup logging for unit tests"""
TestLogger()
class BaseIntegrationTestCase(testscenarios.WithScenarios, unittest.TestCase):
"""Integration test base class."""
......
......@@ -12,18 +12,9 @@ from common.lofar_logging import configure_logger
import unittest
import testscenarios
# TODO(Corne): Move to separate test file, parameterize for configuration
class TestLogger:
"""Ensure logger is configured only once per memory space"""
_logger = None
def __init__(self):
if self._logger is None:
self._logger = configure_logger()
"""Setup logging for unit tests"""
TestLogger()
configure_logger(debug=True)
class BaseTestCase(testscenarios.WithScenarios, unittest.TestCase):
"""Test base class."""
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment