|
| 1 | +# Copyright 2010 New Relic, Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from conftest import logger as conf_logger |
| 16 | +import logging |
| 17 | +import pytest |
| 18 | + |
| 19 | +from newrelic.api.background_task import background_task |
| 20 | +from newrelic.api.log import NewRelicLogForwardingHandler |
| 21 | +from testing_support.fixtures import reset_core_stats_engine |
| 22 | +from testing_support.validators.validate_log_event_count import validate_log_event_count |
| 23 | +from testing_support.validators.validate_log_event_count_outside_transaction import validate_log_event_count_outside_transaction |
| 24 | +from testing_support.validators.validate_function_called import validate_function_called |
| 25 | + |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | +@pytest.fixture(scope="function") |
| 30 | +def uninstrument_logging(): |
| 31 | + instrumented = logging.Logger.callHandlers |
| 32 | + while hasattr(logging.Logger.callHandlers, "__wrapped__"): |
| 33 | + logging.Logger.callHandlers = logging.Logger.callHandlers.__wrapped__ |
| 34 | + yield |
| 35 | + logging.Logger.callHandlers = instrumented |
| 36 | + |
| 37 | + |
| 38 | +@pytest.fixture(scope="function") |
| 39 | +def logger(conf_logger, uninstrument_logging): |
| 40 | + handler = NewRelicLogForwardingHandler() |
| 41 | + conf_logger.addHandler(handler) |
| 42 | + yield conf_logger |
| 43 | + conf_logger.removeHandler(handler) |
| 44 | + |
| 45 | + |
| 46 | +def exercise_logging(logger): |
| 47 | + logger.warning("C") |
| 48 | + assert len(logger.caplog.records) == 1 |
| 49 | + |
| 50 | + |
| 51 | +def test_handler_inside_transaction(logger): |
| 52 | + @validate_log_event_count(1) |
| 53 | + @validate_function_called("newrelic.api.log", "NewRelicLogForwardingHandler.emit") |
| 54 | + @background_task() |
| 55 | + def test(): |
| 56 | + exercise_logging(logger) |
| 57 | + |
| 58 | + test() |
| 59 | + |
| 60 | + |
| 61 | +@reset_core_stats_engine() |
| 62 | +def test_handler_outside_transaction(logger): |
| 63 | + @validate_log_event_count_outside_transaction(1) |
| 64 | + @validate_function_called("newrelic.api.log", "NewRelicLogForwardingHandler.emit") |
| 65 | + def test(): |
| 66 | + exercise_logging(logger) |
| 67 | + |
| 68 | + test() |
0 commit comments