Skip to content

Commit

Permalink
Merge pull request #286 from DiamondLightSource/hyperion_1027_fix_log…
Browse files Browse the repository at this point in the history
…ging_filters

Ensure beamline tag is applied to all graylog messages
  • Loading branch information
d-perl authored Jan 11, 2024
2 parents 6857722 + b506b32 commit 8e466bf
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/dodal/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ def set_up_graylog_handler(logging_level: str, dev_mode: bool = False, logger=LO
"""
graylog_host, graylog_port = _get_graylog_configuration(dev_mode)
graylog_handler = GELFTCPHandler(graylog_host, graylog_port)
graylog_handler.addFilter(beamline_filter)
_add_handler(logger, graylog_handler, logging_level)
logger.addFilter(beamline_filter)

# Warn users if trying to run in prod in debug mode
if not dev_mode and logging_level == "DEBUG":
Expand Down
53 changes: 52 additions & 1 deletion tests/unit_tests/test_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def test_messages_logged_from_dodal_get_sent_to_graylog_and_file(
mock_graylog_handler_class.return_value.level = logging.DEBUG
handlers = [None, None, None]
with patch("dodal.log.GELFTCPHandler", mock_graylog_handler_class):
handlers = log.set_up_logging_handlers(None, False)
handlers = log.set_up_logging_handlers(None, False) # type: ignore
logger = log.LOGGER
logger.info("test")
mock_GELFTCPHandler = handlers[1]
Expand All @@ -140,6 +140,57 @@ def test_messages_logged_from_dodal_get_sent_to_graylog_and_file(
mock_filehandler_emit.assert_called()


@patch("dodal.log.logging.FileHandler.emit")
def test_various_messages_to_graylog_get_beamline_filter(
mock_filehandler_emit: MagicMock,
):
from os import environ

if environ.get("BEAMLINE"):
del environ["BEAMLINE"]
log.beamline_filter = log.BeamlineFilter()

def mock_set_up_graylog_handler(
logging_level: str, dev_mode: bool = False, logger=log.LOGGER
):
graylog_host, graylog_port = log._get_graylog_configuration(dev_mode=True)
graylog_handler = GELFTCPHandler(graylog_host, graylog_port)
graylog_handler.emit = MagicMock()
graylog_handler.addFilter(log.beamline_filter)
log._add_handler(logger, graylog_handler, logging_level)
return graylog_handler

for handler in log.LOGGER.handlers:
handler.close()
log.LOGGER.handlers = []
with patch("dodal.log.set_up_graylog_handler", mock_set_up_graylog_handler):
handlers = log.set_up_logging_handlers(None, False)
logger = log.LOGGER

mock_GELFTCPHandler: GELFTCPHandler = handlers[1] # type: ignore
assert mock_GELFTCPHandler is not None
assert mock_GELFTCPHandler.host == "localhost"
assert mock_GELFTCPHandler.port == 5555

logger.info("test")
mock_GELFTCPHandler.emit.assert_called()
assert mock_GELFTCPHandler.emit.call_args.args[0].beamline == "dev"

from dodal.beamlines import i03

_aperture_scatterguard = i03.aperture_scatterguard(fake_with_ophyd_sim=True)
assert mock_GELFTCPHandler.emit.call_args.args[0].name == "ophyd"
assert mock_GELFTCPHandler.emit.call_args.args[0].beamline == "dev"

from bluesky.run_engine import RunEngine

RE = RunEngine()
RE.log.logger.info("RunEngine log message")

assert mock_GELFTCPHandler.emit.call_args.args[0].name == "bluesky"
assert mock_GELFTCPHandler.emit.call_args.args[0].beamline == "dev"


def test_when_EnhancedRollingFileHandler_reaches_max_size_then_rolls_over():
rolling_file_handler = log.EnhancedRollingFileHandler("test", delay=True)
mock_stream = MagicMock()
Expand Down

0 comments on commit 8e466bf

Please sign in to comment.