"""This file tests logging function of an app.""" import importlib from logging import StreamHandler from flexmock import flexmock from owslogger.logger import DSNHandler from label_copy_export import config from label_copy_export.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 test_loggly_enabled(): """Test DSNHandler availability. Test if DSNHandler is present in log handlers when config.LOGGER_DSN is set. """ clear_handlers() flexmock(config).should_receive('LOGGER_DSN').and_return('loggly_dsn') flexmock(DSNHandler).new_instances('dsn_handler') importlib.reload(loggly) assert 'dsn_handler' 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() flexmock(config).should_receive('LOGGER_DSN').and_return('') flexmock(StreamHandler).new_instances('stream_handler') importlib.reload(loggly) assert 'stream_handler' in loggly.app_logger.handlers 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()