Skip to content

feat: add an option to log ddtrace logs to a file#3214

Merged
mergify[bot] merged 77 commits into
DataDog:1.xfrom
wantsui:wantsui/debug-log-files
Jun 16, 2022
Merged

feat: add an option to log ddtrace logs to a file#3214
mergify[bot] merged 77 commits into
DataDog:1.xfrom
wantsui:wantsui/debug-log-files

Conversation

@wantsui

@wantsui wantsui commented Feb 1, 2022

Copy link
Copy Markdown
Collaborator

This PR follows up on the discussion in #2603 to add options for ddtrace logs to be written to a file. This is useful when troubleshooting.

Environment Variables

This PR introduces three new environment variables:

  1. DD_TRACE_LOG_FILE - when used with debug mode, this forwards ddtrace logs to a specific file.
  2. DD_TRACE_LOG_FILE_SIZE_BYTES - when used, sets the max file size. The default is 15 mb.
  3. DD_TRACE_LOG_FILE_LEVEL used to control the log level defined for the RotatingFileHandler. It will accept values such as DEBUG, WARNING, ERROR, etc based on the logging library.

How it works

  1. DD_TRACE_DEBUG controls the log level of the ddtrace logger, while DD_TRACE_LOG_FILE_LEVEL controls the log level of the RotatingFileHandler used with file creation. DD_TRACE_LOG_FILE_LEVEL defaults to debug mode, but logs aren't written unless DD_TRACE_LOG_FILE is configured.
  2. Child loggers under the ddtrace follow the configured used by ddtrace.
    * Exception: The ddtrace-run logs under commands/ddtrace-run do not use DD_TRACE_LOG_FILE if DD_TRACE_DEBUG is enabled.
    * Example: Logs that start with program executable
  3. Behavior is unaffected by the import order of ddtrace is imported relative to logging when using patch_all/patch.
  4. The logging levels follow the logging module from the standard Python library.
  5. Python 2 only: If the application is using DD_TRACE_DEBUG=true, a basic logging handler will need to be configured,
    ie: logging.basicConfig(). Otherwise, console logs will not be emitted due to No handlers could be found for logger "ddtrace". This logging behavior does not occur in Python 3.

Example usage

To get debug level logs written to a file:

  1. DD_TRACE_DEBUG=true DD_TRACE_LOG_FILE=/path/to/my/debuglogs
  2. DD_TRACE_LOG_FILE_LEVEL=DEBUG DD_TRACE_LOG_FILE=/path/to/my/debuglogs

Summary of Options

Further customization of the logging configuration is possible:

DD_TRACE_DEBUG DD_TRACE_LOG_FILE_LEVEL DD_TRACE_LOG_FILE DD_TRACE_FILE_SIZE_BYTES Result
False (Default) DEBUG (Default) or CRITICAL OR ERROR OR WARNING OR INFO None (Default) 15728640 (Default) or Any Defaults to user’s application logging configuration: Up to WARNING logs (Critical, Error, and Warning) inheriting from ddtrace are emitted to the stderr equivalent. / RotatingFileHandler: Not configured.
False (Default) DEBUG (Default) or CRITICAL OR ERROR OR WARNING OR INFO (Path) 15728640 (Default) or Any Defaults to user’s application logging configuration: Not configured. / RotatingFileHandler: Up to WARNING/DEBUG/CRITICAL/ERROR/INFO logs inheriting from ddtrace are emitted to a file path, depending on DD_TRACE_LOG_FILE_LEVEL. When the file has reached DD_TRACE_FILE_SIZE_BYTES bytes, it will create 1 backup log file.
True DEBUG (Default) or CRITICAL OR ERROR OR WARNING OR INFO None (Default) 15728640 (Default) or Any Defaults to user’s application logging configuration: Up to DEBUG logs inheriting from ddtrace are emitted to the stderr equivalent. / RotatingFileHandler: Not configured.
True DEBUG (Default) (Path) 15728640 (Default) or Any Defaults to user’s application logging configuration: Not configured / RotatingFileHandler: Up to DEBUG logs inheriting from ddtrace are emitted to a file path. When the file has reached DD_TRACE_FILE_SIZE_BYTES bytes, it will create 1 backup log file.
True CRITICAL OR ERROR OR WARNING OR INFO (Path) 15728640 (Default) or Any Defaults to user’s application logging configuration: Not configured / RotatingFileHandler: Logs inheriting from ddtrace are set to emit up to DEBUG logs. Up to WARNING/CRITICAL/ERROR/INFO based on DD_TRACE_LOG_FILE_LEVEL get written to the logs. When the file has reached DD_TRACE_FILE_SIZE_BYTES bytes, it will create 1 backup log file.

Checklist

  • Added to the correct milestone.
  • Tests provided or description of manual testing performed is included in the code or PR.
  • Library documentation is updated.
  • Corp site documentation is updated (link to the PR).

@brettlangdon brettlangdon left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looking good!

Comment thread ddtrace/__init__.py Outdated
Comment thread ddtrace/logger.py Outdated
Comment thread ddtrace/logger.py Outdated
Comment thread ddtrace/logger.py Outdated
… what to do with log levels when not specified.
Comment thread ddtrace/__init__.py Outdated
Comment thread ddtrace/logger.py Outdated
Comment thread ddtrace/logger.py Outdated
Comment thread ddtrace/logger.py Outdated
Comment thread ddtrace/logger.py Outdated
Comment thread ddtrace/logger.py Outdated
@mergify

mergify Bot commented Feb 18, 2022

Copy link
Copy Markdown
Contributor

@wantsui this pull request is now in conflict 😩

@mergify mergify Bot added the conflict label Feb 18, 2022
@mergify mergify Bot removed the conflict label Feb 22, 2022

@brettlangdon brettlangdon left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice. mostly minor nits, this is looking good!

Comment thread ddtrace/logger.py Outdated
Comment thread ddtrace/logger.py Outdated
Comment thread ddtrace/logger.py Outdated
Comment thread ddtrace/logger.py Outdated
Comment thread tests/tracer/test_logging.py Outdated
Comment thread tests/tracer/test_logging.py Outdated
Comment thread tests/tracer/test_logging.py Outdated
Comment thread ddtrace/logger.py Outdated
Comment thread ddtrace/logger.py Outdated
Comment thread ddtrace/logger.py Outdated
wantsui and others added 2 commits March 22, 2022 14:45
Comment thread ddtrace/__init__.py Outdated
Comment thread ddtrace/logger.py Outdated
Comment thread ddtrace/logger.py Outdated
Comment thread tests/tracer/test_logging.py Outdated
Comment thread tests/tracer/test_logging.py Outdated
Comment thread tests/tracer/test_logging.py Outdated
Comment thread tests/tracer/test_logging.py Outdated
Comment thread tests/tracer/test_logging.py Outdated
Comment thread tests/tracer/test_logging.py Outdated
Comment thread tests/tracer/test_logging.py Outdated
Comment thread tests/tracer/test_logging.py

@mabdinur mabdinur left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some nits about tests and doc strings. We should revisit some of the parameterized tests and see if some parameters can be removed

Comment thread tests/tracer/test_logging.py
Comment thread tests/tracer/test_logging.py Outdated
Comment thread tests/tracer/test_logging.py
Comment thread tests/tracer/test_logging.py
Comment thread ddtrace/_logger.py Outdated
Comment thread ddtrace/_logger.py Outdated
Comment thread tests/tracer/test_logging.py
Comment thread ddtrace/_logger.py Outdated
Comment thread ddtrace/_logger.py
Comment thread ddtrace/_logger.py Outdated
Comment thread ddtrace/_logger.py Outdated
Comment thread ddtrace/_logger.py Outdated
Comment thread ddtrace/_logger.py
Comment thread docs/configuration.rst
Comment thread tests/contrib/logging/test_tracer_logging.py
Comment thread tests/contrib/logging/test_tracer_logging.py Outdated
Comment thread tests/contrib/logging/test_tracer_logging.py

@ZStriker19 ZStriker19 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super excited for this one! Awesome work!

@Kyle-Verhoog Kyle-Verhoog left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏 👏 👏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants