Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions elasticapm/conf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 0 additions & 33 deletions elasticapm/contrib/django/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
68 changes: 0 additions & 68 deletions tests/contrib/django/django_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"])
Expand Down Expand Up @@ -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)

Expand Down