Skip to content

Add docs for loguru integration #6617

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

Merged
merged 10 commits into from
May 15, 2023
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: asyncio
description: "Learn about the asyncio integration and how it adds support for applications the asyncio module."
sidebar_order: 11
sidebar_order: 20
---

The `AsyncioIntegration` integrates with applications doing concurrent code execution using Pythons [asyncio](https://docs.python.org/3/library/asyncio.html) module.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: GNU Backtrace
description: "Learn about the GNU backtrace integration and how to add it to your integrations list."
sidebar_order: 70
sidebar_order: 50
---

<!-- NOTE: Due to being in common, redirects are in vercel.json -->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: GRPC
description: "Learn about the gRPC integration and how it adds support for the grpcio grpc client and server."
sidebar_order: 80
sidebar_order: 60
---

The [gRPC](https://grpc.io/) integration instruments all incoming requests and outgoing unary-unary, unary-stream grpc
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: HTTPX
description: "Learn about the HTTPX integration and how it adds support for the HTTPX HTTP client."
sidebar_order: 80
sidebar_order: 70
---

The [HTTPX](https://www.python-httpx.org/) integration instruments outgoing HTTP requests using either the sync or the async HTTPX clients.
Expand Down
122 changes: 122 additions & 0 deletions src/platforms/python/common/configuration/integrations/loguru.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
---
title: Loguru
description: Learn about using Sentry with Loguru.
sidebar_order: 80
---

The [Loguru](https://github.com/Delgan/loguru#readme) integration lets you capture log messages and send them to Sentry.

The [`logging`](/platforms/python/guides/logging) integration provides most of the Loguru functionality and most examples on that page work with Loguru.

## Install

Install `sentry-sdk` from PyPI with the Loguru extra.

```bash
pip install --upgrade 'sentry-sdk[loguru]'
```

## Configure

Add `LoguruIntegration()` to your integrations list:

```python
import sentry_sdk
from sentry_sdk.integrations.loguru import LoguruIntegration

sentry_sdk.init(
dsn="___PUBLIC_DSN___",
integrations=[
LoguruIntegration(),
],
)
```

## Behavior

By default, logs with a level of `INFO` or higher will be added as breadcrumbs to Sentry events. Sentry issue will be created for logs with a level of `ERROR` or higher:

```python
from loguru import logger

logger.debug("I am ignored")
logger.info("I am a breadcrumb")
logger.error("I am an event", extra=dict(bar=43))
logger.exception("An exception happened")
```

- An error event with the message `"I am an event"` will be created.
- `"I am a breadcrumb"` will be attached as a breadcrumb to that event.
- `bar` will end up in the `extra` attributes of that event.
- `"An exception happened"` will send the current exception from `sys.exc_info()` with the stack trace to Sentry. If there's no exception, the current stack will be attached.
- The debug message `"I am ignored"` will not be captured by Sentry. To capture it, set `level` to `DEBUG` or lower in `LoguruIntegration`.

### Ignoring a logger

Loggers can be noisy. You can ignore a logger by calling `ignore_logger`.

Since most of the logic is proxied to `logging` integration, we use it instead of the Loguru integration:

```python
# Import form `logging` integration
from sentry_sdk.integrations.logging import ignore_logger

ignore_logger("a.spammy.logger")
```

In `a.spammy.logger` module:

```python
from loguru import logger
logger.error("hi") # No error is sent to Sentry
```

This will work with `logging`'s logger too

```python
logger = logging.getLogger("a.spammy.logger")
logger.error("hi") # Again, no error sent to Sentry
```

You can also use `before-send` and `before-breadcrumb` to ignore only certain messages. See <PlatformLink to="/configuration/filtering/">Filtering Events</PlatformLink> for more information.

## Options

You can pass the following keyword arguments to `LoguruIntegration()`:

```python
import sentry_sdk
from loguru import logger

from sentry_sdk.integrations.loguru import LoguruIntegration
from sentry_sdk.integrations.loguru import LoggingLevels

sentry_loguru = LoguruIntegration(
level=LoggingLevels.INFO.value, # Capture info and above as breadcrumbs
event_level=LoggingLevels.ERROR.value # Send errors as events
)

sentry_sdk.init(
dsn="___PUBLIC_DSN___",
integrations=[
sentry_loguru,
],
)
```

- `level`

The Sentry Python SDK will record log records with a level higher than or equal to `level` as breadcrumbs. Inversely, the SDK ignores any log record with a level lower than this one. If set to `None`, the SDK won't send log records as breadcrumbs.

Default: `INFO`

- `event_level`

The Sentry Python SDK will report log records with a level higher than or equal to `event_level` as events. If set to `None`, the SDK won't send log records as events.

Default: `ERROR`

## Supported Versions

- Loguru: 0.5+
- Python: 3.5+
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: Enhanced Locals
description: "Learn about `pure_eval` and how to add it to your integrations list."
sidebar_order: 50
sidebar_order: 40
---

<!-- NOTE: Due to being in common, redirects are in vercel.json -->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: PyMongo
description: "Learn about the PyMongo integration and how it adds support for connections to MongoDB databases."
sidebar_order: 160
sidebar_order: 100
redirect_from:
- /platforms/python/guides/pymongo/
---
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: Redis
description: "Learn about importing the Redis integration."
sidebar_order: 180
sidebar_order: 110
---

<!-- NOTE: Due to being in common, redirects are in vercel.json -->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: SOCKET
description: "Learn about the Socket integration and how it adds support network actions."
sidebar_order: 80
sidebar_order: 90
---

Use this integration to create spans for dns resolves and connection creations.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: SQLAlchemy
description: "Learn about importing the SQLAlchemy integration and how it captures queries from SQLAlchemy as breadcrumbs."
sidebar_order: 190
sidebar_order: 120
redirect_from:
- /platforms/python/guides/aws-lambda/integrations/sqlalchemy/
- /platforms/python/guides/logging/integrations/sqlalchemy/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: WSGI
description: "Learn about the WSGI integration and how it adds support for WSGI applications."
sidebar_order: 220
sidebar_order: 130
redirect_from:
- /platforms/python/guides/wsgi/
---
Expand Down