From d7e880a3d61c0938da9dddbb9058b7d14b4df98a Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Mon, 18 Sep 2023 10:50:56 +0200 Subject: [PATCH 1/3] Getting Started: logging --- src/platforms/python/guides/logging/index.mdx | 92 ++++++++++++------- 1 file changed, 58 insertions(+), 34 deletions(-) diff --git a/src/platforms/python/guides/logging/index.mdx b/src/platforms/python/guides/logging/index.mdx index 8a9f19abbe1c7..f1bb70b7fba79 100644 --- a/src/platforms/python/guides/logging/index.mdx +++ b/src/platforms/python/guides/logging/index.mdx @@ -6,69 +6,77 @@ redirect_from: description: "Learn about logging with Python." --- -Calling `sentry_sdk.init()` already integrates with the logging module. It is -equivalent to this explicit configuration: +Adds support for Python logging. + +## Install + +Install `sentry-sdk` from PyPI: + +```bash +pip install --upgrade sentry-sdk +``` + +## Configure + +The logging integrations is a default integration so it will be enabled automatically when you initialize the Sentry SDK. ```python -import logging import sentry_sdk -from sentry_sdk.integrations.logging import LoggingIntegration -# All of this is already happening by default! -sentry_logging = LoggingIntegration( - level=logging.INFO, # Capture info and above as breadcrumbs - event_level=logging.ERROR # Send errors as events -) sentry_sdk.init( dsn="___PUBLIC_DSN___", - integrations=[ - sentry_logging, - ], - # Set traces_sample_rate to 1.0 to capture 100% # of transactions for performance monitoring. - # We recommend adjusting this value in production, traces_sample_rate=1.0, ) ``` -## Usage +## Verify ```python import logging -logging.debug("I am ignored") -logging.info("I am a breadcrumb") -logging.error("I am an event", extra=dict(bar=43)) -logging.exception("An exception happened") -``` +def main(): + sentry_sdk.init(...) # same as above + + logging.debug("I am ignored") + logging.info("I am a breadcrumb") + logging.error("I am an event", extra=dict(bar=43)) + logging.exception("An exception happened") + +main() +``` - There will be an error event with the message `"I am an event"`. - `"I am a breadcrumb"` will be attached as a breadcrumb to that event. - `bar` will end up in the event's `extra` attributes. - `"An exception happened"` will send the current exception from `sys.exc_info()` with the stack trace and everything to the Sentry Python SDK. If there's no exception, the current stack will be attached. -- The debug message `"I am ignored"` will not surface anywhere. To capture it, you need to lower `level` to `DEBUG`. +- The debug message `"I am ignored"` will not surface anywhere. To capture it, you need to lower `level` to `DEBUG` (See below). -## Ignoring a logger +## Options -Sometimes a logger is extremely noisy and spams you with pointless errors. You can completely ignore that logger by calling `ignore_logger`: +If you want to change the default behavior of the logging integration, you need to instantiate the integration by hand and pass it to Sentry's `init` function: ```python -from sentry_sdk.integrations.logging import ignore_logger - - -ignore_logger("a.spammy.logger") +import logging +import sentry_sdk +from sentry_sdk.integrations.logging import LoggingIntegration -logger = logging.getLogger("a.spammy.logger") -logger.error("hi") # no error sent to sentry +sentry_sdk.init( + dsn="___PUBLIC_DSN___", + # Set traces_sample_rate to 1.0 to capture 100% + # of transactions for performance monitoring. + traces_sample_rate=1.0, + integrations=[ + LoggingIntegration( + level=logging.INFO, # Capture info and above as breadcrumbs + event_level=logging.ERROR # Send errors as events + ), + ], +) ``` -You can also use `before-send` and `before-breadcrumb` to ignore -only certain messages. See [_Filtering Events_](configuration/filtering/) for more information. - -## Options - You can pass the following keyword arguments to `LoggingIntegration()`: - `level` (default `INFO`): The Sentry Python SDK will record log records with a level higher than or equal to `level` as breadcrumbs. Inversely, the SDK completely ignores any log record with a level lower than this one. If a value of `None` occurs, the SDK won't send log records as breadcrumbs. @@ -81,6 +89,22 @@ The Sentry Python SDK will honor the configured level of each logger (set with ` +## Ignoring a logger + +Sometimes a logger is extremely noisy and spams you with pointless errors. You can completely ignore that logger by calling `ignore_logger`: + +```python +from sentry_sdk.integrations.logging import ignore_logger + +ignore_logger("a.spammy.logger") + +logger = logging.getLogger("a.spammy.logger") +logger.error("hi") # no error sent to sentry +``` + +You can also use `before-send` and `before-breadcrumb` to ignore +only certain messages. See [_Filtering Events_](configuration/filtering/) for more information. + ## Handler classes Instead of using `LoggingIntegration`, you can use two regular logging `logging.Handler` subclasses that the integration exports. From 73b9c0c4a956b5916ad89ac0d42c42f0b433b0ef Mon Sep 17 00:00:00 2001 From: "getsantry[bot]" <66042841+getsantry[bot]@users.noreply.github.com> Date: Mon, 18 Sep 2023 08:53:00 +0000 Subject: [PATCH 2/3] style(lint): Auto commit lint changes --- src/platforms/python/guides/logging/index.mdx | 1 + 1 file changed, 1 insertion(+) diff --git a/src/platforms/python/guides/logging/index.mdx b/src/platforms/python/guides/logging/index.mdx index f1bb70b7fba79..641c1dd537ce0 100644 --- a/src/platforms/python/guides/logging/index.mdx +++ b/src/platforms/python/guides/logging/index.mdx @@ -48,6 +48,7 @@ def main(): main() ``` + - There will be an error event with the message `"I am an event"`. - `"I am a breadcrumb"` will be attached as a breadcrumb to that event. - `bar` will end up in the event's `extra` attributes. From ddd157e350f3c850c7b4dabec122faf17abbb7e8 Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Wed, 20 Sep 2023 08:27:36 +0200 Subject: [PATCH 3/3] Apply suggestions from code review Co-authored-by: Shana Matthews --- src/platforms/python/guides/logging/index.mdx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/platforms/python/guides/logging/index.mdx b/src/platforms/python/guides/logging/index.mdx index 641c1dd537ce0..85114252ef2b7 100644 --- a/src/platforms/python/guides/logging/index.mdx +++ b/src/platforms/python/guides/logging/index.mdx @@ -57,7 +57,7 @@ main() ## Options -If you want to change the default behavior of the logging integration, you need to instantiate the integration by hand and pass it to Sentry's `init` function: +To change the default behavior of the logging integration, instantiate the integration manually and pass it to Sentry's `init` function: ```python import logging @@ -92,7 +92,7 @@ The Sentry Python SDK will honor the configured level of each logger (set with ` ## Ignoring a logger -Sometimes a logger is extremely noisy and spams you with pointless errors. You can completely ignore that logger by calling `ignore_logger`: +Sometimes a logger is extremely noisy and spams you with pointless errors. You can ignore that logger by calling `ignore_logger`: ```python from sentry_sdk.integrations.logging import ignore_logger