Skip to content
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

Python: Getting Started logging #7860

Merged
merged 3 commits into from
Sep 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 58 additions & 33 deletions src/platforms/python/guides/logging/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,69 +6,78 @@ 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.

<SignInNote />

```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`:
To change the default behavior of the logging integration, instantiate the integration manually 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.
Expand All @@ -81,6 +90,22 @@ The Sentry Python SDK will honor the configured level of each logger (set with `

</Alert>

## Ignoring a 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

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.
Expand Down
Loading