|
31 | 31 |
|
32 | 32 | from __future__ import absolute_import |
33 | 33 |
|
| 34 | +import logging |
| 35 | +import warnings |
| 36 | + |
34 | 37 | import flask |
35 | 38 | from flask import request, signals |
36 | 39 |
|
37 | 40 | import elasticapm |
38 | 41 | import elasticapm.instrumentation.control |
39 | 42 | from elasticapm import get_client |
40 | 43 | from elasticapm.base import Client |
41 | | -from elasticapm.conf import constants |
| 44 | +from elasticapm.conf import constants, setup_logging |
42 | 45 | from elasticapm.contrib.flask.utils import get_data_from_request, get_data_from_response |
| 46 | +from elasticapm.handlers.logging import LoggingHandler |
43 | 47 | from elasticapm.traces import execution_context |
44 | 48 | from elasticapm.utils import build_name_with_http_method_prefix |
45 | 49 | from elasticapm.utils.disttracing import TraceParent |
@@ -77,8 +81,14 @@ class ElasticAPM(object): |
77 | 81 | >>> elasticapm.capture_message('hello, world!') |
78 | 82 | """ |
79 | 83 |
|
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: |
81 | 85 | 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 | + ) |
82 | 92 | self.client = client or get_client() |
83 | 93 | self.client_cls = client_cls |
84 | 94 |
|
@@ -117,6 +127,14 @@ def init_app(self, app, **defaults) -> None: |
117 | 127 |
|
118 | 128 | self.client = self.client_cls(config, **defaults) |
119 | 129 |
|
| 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 | + |
120 | 138 | signals.got_request_exception.connect(self.handle_exception, sender=app, weak=False) |
121 | 139 |
|
122 | 140 | try: |
|
0 commit comments