Skip to content

Commit 7337ab1

Browse files
beniwohlibasepi
authored andcommitted
use structlog's get_logger if it is available (#591)
* use structlog's get_logger if it is available If the user has set up structlog, this will add some additional context to each log line, like level and timestamp. E.g. {"event": "Read traceparent header 00-42d5718b9ac8289e439a688afb17cbae-d6f9a1dad4c90dec-01"} becomes {"event": "Read traceparent header 00-42d5718b9ac8289e439a688afb17cbae-d6f9a1dad4c90dec-01", "timestamp": "2019-09-17T15:37:00.049627Z", "logger": "elasticapm.traces", "level": "debug"} This can be overridden by setting an environment variable `ELASTIC_APM_DISABLE_STRUCTLOG` to "true". * make use of flag necessary to switch to structlog, and add docs
1 parent 970404d commit 7337ab1

File tree

18 files changed

+90
-36
lines changed

18 files changed

+90
-36
lines changed

docs/logging.asciidoc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ log = logger.new()
6464
log.msg("some_event")
6565
----
6666

67+
===== Use structlog for agent-internal logging
68+
69+
The Elastic APM Python agent uses logging to log internal events and issues.
70+
By default, it will use a `logging` logger.
71+
If your project uses structlog, you can tell the agent to use a structlog logger
72+
by setting the environment variable `ELASTIC_APM_USE_STRUCTLOG` to `true`.
6773

6874
[[log-correlation]]
6975
=== Log correlation in Elasticsearch

elasticapm/base.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
from elasticapm.traces import Tracer, execution_context
4949
from elasticapm.utils import cgroup, compat, is_master_process, stacks, varmap
5050
from elasticapm.utils.encoding import enforce_label_format, keyword_field, shorten, transform
51+
from elasticapm.utils.logging import get_logger
5152
from elasticapm.utils.module_import import import_string
5253
from elasticapm.utils.threading import IntervalTimer
5354

@@ -83,13 +84,13 @@ class Client(object):
8384
>>> print ("Exception caught; reference is %%s" %% ident)
8485
"""
8586

86-
logger = logging.getLogger("elasticapm")
87+
logger = get_logger("elasticapm")
8788

8889
def __init__(self, config=None, **inline):
8990
# configure loggers first
9091
cls = self.__class__
91-
self.logger = logging.getLogger("%s.%s" % (cls.__module__, cls.__name__))
92-
self.error_logger = logging.getLogger("elasticapm.errors")
92+
self.logger = get_logger("%s.%s" % (cls.__module__, cls.__name__))
93+
self.error_logger = get_logger("elasticapm.errors")
9394

9495
self.tracer = None
9596
self.processors = []

elasticapm/conf/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,11 @@
3636
import threading
3737

3838
from elasticapm.utils import compat, starmatch_to_regex
39+
from elasticapm.utils.logging import get_logger
3940

4041
__all__ = ("setup_logging", "Config")
4142

42-
logger = logging.getLogger("elasticapm.conf")
43+
logger = get_logger("elasticapm.conf")
4344

4445

4546
class ConfigurationError(ValueError):

elasticapm/contrib/async_worker.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@
2929
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3030

3131

32-
import logging
3332
import os
3433
import sys
3534
import time
3635
from threading import Lock, Thread
3736

3837
from elasticapm.utils.compat import queue
38+
from elasticapm.utils.logging import get_logger
3939

40-
logger = logging.getLogger("elasticapm")
40+
logger = get_logger("elasticapm")
4141

4242
ELASTIC_APM_WAIT_SECONDS = 10
4343

elasticapm/contrib/django/apps.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2929
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3030

31-
import logging
3231
from functools import partial
3332

3433
from django.apps import AppConfig
@@ -37,8 +36,9 @@
3736
from elasticapm.conf import constants
3837
from elasticapm.contrib.django.client import get_client
3938
from elasticapm.utils.disttracing import TraceParent
39+
from elasticapm.utils.logging import get_logger
4040

41-
logger = logging.getLogger("elasticapm.traces")
41+
logger = get_logger("elasticapm.traces")
4242

4343
ERROR_DISPATCH_UID = "elasticapm-exceptions"
4444
REQUEST_START_DISPATCH_UID = "elasticapm-request-start"

elasticapm/contrib/django/client.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@
3131

3232
from __future__ import absolute_import
3333

34-
import logging
35-
3634
import django
3735
from django.conf import settings as django_settings
3836
from django.core.exceptions import DisallowedHost
@@ -43,6 +41,7 @@
4341
from elasticapm.conf import constants
4442
from elasticapm.contrib.django.utils import iterate_with_template_sources
4543
from elasticapm.utils import compat, encoding, get_url_dict
44+
from elasticapm.utils.logging import get_logger
4645
from elasticapm.utils.module_import import import_string
4746
from elasticapm.utils.wsgi import get_environ, get_headers
4847

@@ -78,7 +77,7 @@ def get_client(client=None):
7877

7978

8079
class DjangoClient(Client):
81-
logger = logging.getLogger("elasticapm.errors.client.django")
80+
logger = get_logger("elasticapm.errors.client.django")
8281

8382
def __init__(self, config=None, **inline):
8483
if config is None:

elasticapm/contrib/django/handlers.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@
3939
from django.conf import settings as django_settings
4040

4141
from elasticapm.handlers.logging import LoggingHandler as BaseLoggingHandler
42+
from elasticapm.utils.logging import get_logger
4243

43-
logger = logging.getLogger("elasticapm.logging")
44+
logger = get_logger("elasticapm.logging")
4445

4546

4647
class LoggingHandler(BaseLoggingHandler):

elasticapm/contrib/flask/__init__.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@
3131

3232
from __future__ import absolute_import
3333

34-
import logging
35-
3634
import flask
3735
from flask import request, signals
3836

@@ -45,8 +43,9 @@
4543
from elasticapm.traces import execution_context
4644
from elasticapm.utils import build_name_with_http_method_prefix
4745
from elasticapm.utils.disttracing import TraceParent
46+
from elasticapm.utils.logging import get_logger
4847

49-
logger = logging.getLogger("elasticapm.errors.client")
48+
logger = get_logger("elasticapm.errors.client")
5049

5150

5251
def make_client(client_cls, app, **defaults):
@@ -128,7 +127,7 @@ def init_app(self, app, **defaults):
128127
self.client = make_client(self.client_cls, app, **defaults)
129128

130129
# 0 is a valid log level (NOTSET), so we need to check explicitly for it
131-
if self.logging or self.logging is 0:
130+
if self.logging or self.logging is 0: # noqa F632
132131
if self.logging is not True:
133132
kwargs = {"level": self.logging}
134133
else:

elasticapm/contrib/opentracing/span.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,12 @@
2828
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2929
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3030

31-
import logging
32-
3331
from opentracing.span import Span as OTSpanBase
3432
from opentracing.span import SpanContext as OTSpanContextBase
3533

3634
from elasticapm import traces
3735
from elasticapm.utils import compat, get_url_dict
36+
from elasticapm.utils.logging import get_logger
3837

3938
try:
4039
# opentracing-python 2.1+
@@ -47,7 +46,7 @@
4746
ot_logs = None
4847

4948

50-
logger = logging.getLogger("elasticapm.contrib.opentracing")
49+
logger = get_logger("elasticapm.contrib.opentracing")
5150

5251

5352
class OTSpan(OTSpanBase):

elasticapm/events.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,18 @@
2929
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3030

3131

32-
import logging
3332
import random
3433
import sys
3534

3635
from elasticapm.conf.constants import EXCEPTION_CHAIN_MAX_DEPTH
3736
from elasticapm.utils import compat, varmap
3837
from elasticapm.utils.encoding import keyword_field, shorten, to_unicode
38+
from elasticapm.utils.logging import get_logger
3939
from elasticapm.utils.stacks import get_culprit, get_stack_info, iter_traceback_frames
4040

4141
__all__ = ("BaseEvent", "Exception", "Message")
4242

43-
logger = logging.getLogger("elasticapm.events")
43+
logger = get_logger("elasticapm.events")
4444

4545

4646
class BaseEvent(object):

0 commit comments

Comments
 (0)