"""This file tests logging function of an app.""" import importlib import logging from owslogger import logger from video.connectors import loggly def clear_handlers(): """Remove all handlers from apps logger.""" for h in loggly.app_logger.handlers: loggly.app_logger.removeHandler(h) def _has_handler(cls): """Check if an instance of cls is present in app_logger.handlers.""" importlib.reload(loggly) importlib.reload(logger) importlib.reload(logging) return any([isinstance(h, cls) for h in loggly.app_logger.handlers]) def test_loggly_disabled(): """Test StreamHandler availability. Test if StreamHandler is present in logger handlers when config.LOGGER_DSN is not set. """ clear_handlers() assert _has_handler(logging.StreamHandler) def test_current_logger_correlation_id(): """Test correlation_id link with logging adapter. Test whether correlation_id is assigned to logging adapter, when using get_current_logger(correlation_id) function. """ correlation_id = 'test_correlation_id' current_logger = loggly.get_current_logger(correlation_id) assert 'correlation_id' in current_logger.extra assert current_logger.extra['correlation_id'] == correlation_id def teardown_module(module): """Clear all handlers when app tears down.""" clear_handlers()