diff --git a/elasticapm/conf/__init__.py b/elasticapm/conf/__init__.py index 6d19eb96c..45b4db5a2 100644 --- a/elasticapm/conf/__init__.py +++ b/elasticapm/conf/__init__.py @@ -887,11 +887,6 @@ def setup_logging(handler): >>> client = ElasticAPM(...) >>> setup_logging(LoggingHandler(client)) - Within Django: - - >>> from elasticapm.contrib.django.handlers import LoggingHandler - >>> setup_logging(LoggingHandler()) - Returns a boolean based on if logging was configured or not. """ # TODO We should probably revisit this. Does it make more sense as diff --git a/elasticapm/contrib/django/handlers.py b/elasticapm/contrib/django/handlers.py index c980acc4f..550cfae87 100644 --- a/elasticapm/contrib/django/handlers.py +++ b/elasticapm/contrib/django/handlers.py @@ -31,44 +31,11 @@ from __future__ import absolute_import -import logging import sys import warnings from django.conf import settings as django_settings -from elasticapm import get_client -from elasticapm.handlers.logging import LoggingHandler as BaseLoggingHandler -from elasticapm.utils.logging import get_logger - -logger = get_logger("elasticapm.logging") - - -class LoggingHandler(BaseLoggingHandler): - def __init__(self, level=logging.NOTSET) -> None: - warnings.warn( - "The LoggingHandler is deprecated and will be removed in v7.0 of the agent. " - "Please use `log_ecs_reformatting` and ship the logs with Elastic " - "Agent or Filebeat instead. " - "https://www.elastic.co/guide/en/apm/agent/python/current/logs.html", - DeprecationWarning, - ) - # skip initialization of BaseLoggingHandler - logging.Handler.__init__(self, level=level) - - @property - def client(self): - return get_client() - - def _emit(self, record, **kwargs): - from elasticapm.contrib.django.middleware import LogMiddleware - - # Fetch the request from a threadlocal variable, if available - request = getattr(LogMiddleware.thread, "request", None) - request = getattr(record, "request", request) - - return super(LoggingHandler, self)._emit(record, request=request, **kwargs) - def exception_handler(client, request=None, **kwargs): def actually_do_stuff(request=None, **kwargs) -> None: diff --git a/tests/contrib/django/django_tests.py b/tests/contrib/django/django_tests.py index 535729bcf..ad88e462e 100644 --- a/tests/contrib/django/django_tests.py +++ b/tests/contrib/django/django_tests.py @@ -62,7 +62,6 @@ from elasticapm.conf.constants import ERROR, SPAN, TRANSACTION from elasticapm.contrib.django.apps import ElasticAPMConfig from elasticapm.contrib.django.client import client, get_client -from elasticapm.contrib.django.handlers import LoggingHandler from elasticapm.contrib.django.middleware.wsgi import ElasticAPM from elasticapm.utils.disttracing import TraceParent from tests.contrib.django.conftest import BASE_TEMPLATE_DIR @@ -410,25 +409,6 @@ def test_ignored_exception_is_ignored(django_elasticapm_client, client): assert len(django_elasticapm_client.events[ERROR]) == 0 -def test_record_none_exc_info(django_elasticapm_client): - # sys.exc_info can return (None, None, None) if no exception is being - # handled anywhere on the stack. See: - # http://docs.python.org/library/sys.html#sys.exc_info - record = logging.LogRecord( - "foo", logging.INFO, pathname=None, lineno=None, msg="test", args=(), exc_info=(None, None, None) - ) - handler = LoggingHandler() - handler.emit(record) - - assert len(django_elasticapm_client.events[ERROR]) == 1 - event = django_elasticapm_client.events[ERROR][0] - - assert event["log"]["param_message"] == "test" - assert event["log"]["logger_name"] == "foo" - assert event["log"]["level"] == "info" - assert "exception" not in event - - def test_404_middleware(django_elasticapm_client, client): with override_settings( **middleware_setting(django.VERSION, ["elasticapm.contrib.django.middleware.Catch404Middleware"]) @@ -1032,54 +1012,6 @@ def test_filter_matches_module_only(django_sending_elasticapm_client): assert len(django_sending_elasticapm_client.httpserver.requests) == 1 -def test_django_logging_request_kwarg(django_elasticapm_client): - handler = LoggingHandler() - - logger = logging.getLogger(__name__) - logger.handlers = [] - logger.addHandler(handler) - - logger.error( - "This is a test error", - extra={ - "request": WSGIRequest( - environ={ - "wsgi.input": io.StringIO(), - "REQUEST_METHOD": "POST", - "SERVER_NAME": "testserver", - "SERVER_PORT": "80", - "CONTENT_TYPE": "application/json", - "ACCEPT": "application/json", - } - ) - }, - ) - - assert len(django_elasticapm_client.events[ERROR]) == 1 - event = django_elasticapm_client.events[ERROR][0] - assert "request" in event["context"] - request = event["context"]["request"] - assert request["method"] == "POST" - - -def test_django_logging_middleware(django_elasticapm_client, client): - handler = LoggingHandler() - - logger = logging.getLogger("logmiddleware") - logger.handlers = [] - logger.addHandler(handler) - logger.level = logging.INFO - - with override_settings( - **middleware_setting(django.VERSION, ["elasticapm.contrib.django.middleware.LogMiddleware"]) - ): - client.get(reverse("elasticapm-logging")) - assert len(django_elasticapm_client.events[ERROR]) == 1 - event = django_elasticapm_client.events[ERROR][0] - assert "request" in event["context"] - assert event["context"]["request"]["url"]["pathname"] == reverse("elasticapm-logging") - - def client_get(client, url): return client.get(url)