-
Notifications
You must be signed in to change notification settings - Fork 477
chore(logging): remove DD_CALL_BASIC_CONFIG #6612
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
ef21510
2b1be4e
32d2252
06c7017
15c6831
4846702
b385bcc
e147b15
213157f
ac7aef8
3adb946
5095327
18a7553
f0221b3
0e09575
87626e9
0173519
c804748
f52801a
3669723
92683e0
be9154d
72edbf7
be1315f
3d30055
9f50214
e8ec713
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -5,6 +5,11 @@ | |||||||||
| from ddtrace.internal.utils.formats import asbool | ||||||||||
|
|
||||||||||
|
|
||||||||||
| DD_LOG_FORMAT = "%(asctime)s %(levelname)s [%(name)s] [%(filename)s:%(lineno)d] {}- %(message)s".format( | ||||||||||
| "[dd.service=%(dd.service)s dd.env=%(dd.env)s dd.version=%(dd.version)s" | ||||||||||
| " dd.trace_id=%(dd.trace_id)s dd.span_id=%(dd.span_id)s] " | ||||||||||
| ) | ||||||||||
|
|
||||||||||
| DEFAULT_FILE_SIZE_BYTES = 15 << 20 # 15 MB | ||||||||||
|
|
||||||||||
|
|
||||||||||
|
|
@@ -32,14 +37,18 @@ def configure_ddtrace_logger(): | |||||||||
|
|
||||||||||
| """ | ||||||||||
| ddtrace_logger = logging.getLogger("ddtrace") | ||||||||||
|
|
||||||||||
| _configure_ddtrace_debug_logger(ddtrace_logger) | ||||||||||
| _configure_ddtrace_file_logger(ddtrace_logger) | ||||||||||
|
|
||||||||||
|
|
||||||||||
| def _configure_ddtrace_debug_logger(logger): | ||||||||||
| debug_enabled = asbool(os.environ.get("DD_TRACE_DEBUG", "false")) | ||||||||||
| if debug_enabled: | ||||||||||
| logs_injection_enabled = asbool(os.environ.get("DD_LOGS_INJECTION", "false")) | ||||||||||
|
|
||||||||||
| # Logs injection enabled requires a handler present on ddtrace | ||||||||||
| if debug_enabled or logs_injection_enabled: | ||||||||||
| if not os.environ.get("DD_TRACE_LOG_FILE"): | ||||||||||
| logger.addHandler(logging.StreamHandler()) | ||||||||||
Yun-Kim marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||
| logger.setLevel(logging.DEBUG) | ||||||||||
| logger.debug("debug mode has been enabled for the ddtrace logger") | ||||||||||
|
|
||||||||||
|
|
@@ -62,11 +71,21 @@ def _configure_ddtrace_file_logger(logger): | |||||||||
| ddtrace_file_handler = RotatingFileHandler( | ||||||||||
| filename=log_path, mode="a", maxBytes=max_file_bytes, backupCount=num_backup | ||||||||||
| ) | ||||||||||
|
|
||||||||||
| log_format = "%(asctime)s %(levelname)s [%(name)s] [%(filename)s:%(lineno)d] - %(message)s" | ||||||||||
| log_formatter = logging.Formatter(log_format) | ||||||||||
|
|
||||||||||
Yun-Kim marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||
| ddtrace_file_handler.setLevel(file_log_level_value) | ||||||||||
| ddtrace_file_handler.setFormatter(log_formatter) | ||||||||||
| logger.addHandler(ddtrace_file_handler) | ||||||||||
| logger.debug("ddtrace logs will be routed to %s", log_path) | ||||||||||
|
|
||||||||||
|
|
||||||||||
| def _configure_log_injection(): | ||||||||||
| """ | ||||||||||
| Ensures that logging is patched before we inject trace information into logs. | ||||||||||
| """ | ||||||||||
| from ddtrace import patch | ||||||||||
|
|
||||||||||
| patch(logging=True) | ||||||||||
| ddtrace_logger = logging.getLogger("ddtrace") | ||||||||||
| if ddtrace_logger.hasHandlers(): | ||||||||||
| ddtrace_logger.handlers[0].setFormatter(logging.Formatter(DD_LOG_FORMAT)) | ||||||||||
|
||||||||||
| if ddtrace_logger.hasHandlers(): | |
| ddtrace_logger.handlers[0].setFormatter(logging.Formatter(DD_LOG_FORMAT)) | |
| for handler in ddtrace_logger.handlers: | |
| handler.setFormatter(logging.Formatter(DD_LOG_FORMAT)) |
this way if we have log file and stream handler then we properly set the format.
although writing this out, is there a chance someone can configure logging manually before this? wondering if we can end up in a state where we overwrite a log format someone sets themselves?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm pretty confident user code will always run after _configure_log_injection() is called since it's called on tracer module load and sitecustomize.py.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ | |
| import warnings # noqa | ||
|
|
||
| from ddtrace import config # noqa | ||
| from ddtrace._logger import _configure_log_injection | ||
| from ddtrace.debugging._config import di_config # noqa | ||
| from ddtrace.debugging._config import ed_config # noqa | ||
| from ddtrace.internal.compat import PY2 # noqa | ||
|
|
@@ -19,32 +20,12 @@ | |
| from ddtrace.internal.runtime.runtime_metrics import RuntimeWorker # noqa | ||
| from ddtrace.internal.utils.formats import asbool # noqa | ||
| from ddtrace.internal.utils.formats import parse_tags_str # noqa | ||
| from ddtrace.tracer import DD_LOG_FORMAT # noqa | ||
| from ddtrace.vendor.debtcollector import deprecate # noqa | ||
|
|
||
|
|
||
| if config.logs_injection: | ||
| # immediately patch logging if trace id injected | ||
| from ddtrace import patch | ||
| # Debug mode from the tracer will do the same here, so only need to do this otherwise. | ||
| if not config._debug_mode and config.logs_injection: | ||
|
||
| _configure_log_injection() | ||
|
|
||
| patch(logging=True) | ||
|
|
||
|
|
||
| # DEV: Once basicConfig is called here, future calls to it cannot be used to | ||
| # change the formatter since it applies the formatter to the root handler only | ||
| # upon initializing it the first time. | ||
| # See https://github.com/python/cpython/blob/112e4afd582515fcdcc0cde5012a4866e5cfda12/Lib/logging/__init__.py#L1550 | ||
| # Debug mode from the tracer will do a basicConfig so only need to do this otherwise | ||
| if not config._debug_mode and config._call_basic_config: | ||
| deprecate( | ||
| "ddtrace.tracer.logging.basicConfig", | ||
| message="`logging.basicConfig()` should be called in a user's application.", | ||
| removal_version="2.0.0", | ||
| ) | ||
| if config.logs_injection: | ||
| logging.basicConfig(format=DD_LOG_FORMAT) | ||
| else: | ||
| logging.basicConfig() | ||
|
|
||
| log = get_logger(__name__) | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.