Skip to content

Commit b11d2cb

Browse files
committed
Revert "contrib/flask: remove deprecated log shipping integration (elastic#2346)"
This reverts commit 39f4191.
1 parent 0953fb9 commit b11d2cb

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

elasticapm/contrib/flask/__init__.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,19 @@
3131

3232
from __future__ import absolute_import
3333

34+
import logging
35+
import warnings
36+
3437
import flask
3538
from flask import request, signals
3639

3740
import elasticapm
3841
import elasticapm.instrumentation.control
3942
from elasticapm import get_client
4043
from elasticapm.base import Client
41-
from elasticapm.conf import constants
44+
from elasticapm.conf import constants, setup_logging
4245
from elasticapm.contrib.flask.utils import get_data_from_request, get_data_from_response
46+
from elasticapm.handlers.logging import LoggingHandler
4347
from elasticapm.traces import execution_context
4448
from elasticapm.utils import build_name_with_http_method_prefix
4549
from elasticapm.utils.disttracing import TraceParent
@@ -77,8 +81,14 @@ class ElasticAPM(object):
7781
>>> elasticapm.capture_message('hello, world!')
7882
"""
7983

80-
def __init__(self, app=None, client=None, client_cls=Client, **defaults) -> None:
84+
def __init__(self, app=None, client=None, client_cls=Client, logging=False, **defaults) -> None:
8185
self.app = app
86+
self.logging = logging
87+
if self.logging:
88+
warnings.warn(
89+
"Flask log shipping is deprecated. See the Flask docs for more info and alternatives.",
90+
DeprecationWarning,
91+
)
8292
self.client = client or get_client()
8393
self.client_cls = client_cls
8494

@@ -117,6 +127,14 @@ def init_app(self, app, **defaults) -> None:
117127

118128
self.client = self.client_cls(config, **defaults)
119129

130+
# 0 is a valid log level (NOTSET), so we need to check explicitly for it
131+
if self.logging or self.logging is logging.NOTSET:
132+
if self.logging is not True:
133+
kwargs = {"level": self.logging}
134+
else:
135+
kwargs = {}
136+
setup_logging(LoggingHandler(self.client, **kwargs))
137+
120138
signals.got_request_exception.connect(self.handle_exception, sender=app, weak=False)
121139

122140
try:

tests/contrib/flask/flask_tests.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,32 @@ def test_rum_tracing_context_processor(flask_apm_client):
441441
assert callable(context["apm"]["span_id"])
442442

443443

444+
@pytest.mark.parametrize("flask_apm_client", [{"logging": True}], indirect=True)
445+
def test_logging_enabled(flask_apm_client):
446+
logger = logging.getLogger()
447+
logger.error("test")
448+
error = flask_apm_client.client.events[ERROR][0]
449+
assert error["log"]["level"] == "error"
450+
assert error["log"]["message"] == "test"
451+
452+
453+
@pytest.mark.parametrize("flask_apm_client", [{"logging": False}], indirect=True)
454+
def test_logging_disabled(flask_apm_client):
455+
logger = logging.getLogger()
456+
logger.error("test")
457+
assert len(flask_apm_client.client.events[ERROR]) == 0
458+
459+
460+
@pytest.mark.parametrize("flask_apm_client", [{"logging": logging.ERROR}], indirect=True)
461+
def test_logging_by_level(flask_apm_client):
462+
logger = logging.getLogger()
463+
logger.warning("test")
464+
logger.error("test")
465+
assert len(flask_apm_client.client.events[ERROR]) == 1
466+
error = flask_apm_client.client.events[ERROR][0]
467+
assert error["log"]["level"] == "error"
468+
469+
444470
def test_flask_transaction_ignore_urls(flask_apm_client):
445471
resp = flask_apm_client.app.test_client().get("/users/")
446472
resp.close()

0 commit comments

Comments
 (0)