diff --git a/docs/platforms/python/integrations/logging/index.mdx b/docs/platforms/python/integrations/logging/index.mdx index 9683270ad8810..9ba52e3c3e2ff 100644 --- a/docs/platforms/python/integrations/logging/index.mdx +++ b/docs/platforms/python/integrations/logging/index.mdx @@ -54,6 +54,22 @@ main() - `"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` (See below). +Log records can additionally also be captured as [Sentry logs](/platforms/python/logs/) as long as the `enable_logs` experimental option is `True`. + +```python +import logging +import sentry_sdk + +sentry_sdk.init( + # ... + _experiments={ + "enable_logs": True, + }, +) + +logging.info("I will be sent to Sentry logs") +``` + ## Options To change the default behavior of the logging integration, instantiate the integration manually and pass it to Sentry's `init` function: @@ -86,7 +102,7 @@ You can pass the following keyword arguments to `LoggingIntegration()`: - `event_level` (default `ERROR`): The Sentry Python SDK will report log records with a level higher than or equal to `event_level` as events as long as the logger itself is set to output records of those log levels (see note below). If a value of `None` occurs, the SDK won't send log records as events. -- `sentry_logs_level` (default `INFO`): The Sentry Python SDK will capture records with a level higher than or equal to `sentry_logs_level` as logs as long as the `enable_logs` experimental option is `True`: +- `sentry_logs_level` (default `INFO`): The Sentry Python SDK will capture records with a level higher than or equal to `sentry_logs_level` as [Sentry structured logs](/platforms/python/logs/) as long as the `enable_logs` experimental option is `True`. ```python sentry_sdk.init( diff --git a/docs/platforms/python/integrations/loguru/index.mdx b/docs/platforms/python/integrations/loguru/index.mdx index 8301995f88219..1712da6489a3f 100644 --- a/docs/platforms/python/integrations/loguru/index.mdx +++ b/docs/platforms/python/integrations/loguru/index.mdx @@ -68,6 +68,22 @@ logger.exception("An exception happened") - `"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`. +Loguru log records can additionally also be captured as [Sentry logs](/platforms/python/logs/) as long as the `enable_logs` experimental option is `True`. + +```python +import sentry_sdk +from loguru import logger + +sentry_sdk.init( + # ... + _experiments={ + "enable_logs": True, + }, +) + +logger.info("I will be sent to Sentry logs") +``` + ### Ignoring a logger Loggers can be noisy. You can ignore a logger by calling `ignore_logger`. @@ -134,7 +150,7 @@ sentry_sdk.init( - `sentry_logs_level` - The Sentry Python SDK will capture log records with a level higher than or equal to `sentry_logs_level` as logs. If set to `None`, the SDK won't send records as logs. + The Sentry Python SDK will capture log records with a level higher than or equal to `sentry_logs_level` as [Sentry structured logs](/platforms/python/logs/). If set to `None`, the SDK won't send records as logs. To capture Loguru log records as Sentry logs, you must enable the experimental `enable_logs` option when initializing the SDK (regardless of the `sentry_logs_level` setting). diff --git a/platform-includes/logs/integrations/python.mdx b/platform-includes/logs/integrations/python.mdx index d9e218b3aa0de..7cf4b0bdb5d5a 100644 --- a/platform-includes/logs/integrations/python.mdx +++ b/platform-includes/logs/integrations/python.mdx @@ -1,6 +1,6 @@ -### Python Logger Integration +### Standard library logging -Sentry automatically instruments Python loggers via its `LoggingIntegration`. +The SDK's `LoggingIntegration` instruments standard library loggers in order to send Sentry structured logs to Sentry. ```python import sentry_sdk @@ -9,8 +9,8 @@ import logging sentry_sdk.init( dsn="___PUBLIC_DSN___", _experiments={ - "enable_logs": True - } + "enable_logs": True, + }, ) # Your existing logging setup @@ -22,7 +22,7 @@ my_logger.debug('In this example debug events will not be sent to Sentry logs. m my_logger.info('But info events will be sent to Sentry logs. my_value=%s', my_value) ``` -By default, the logging integration sends INFO-level logs and higher to Sentry logs. You can set a different threshold via the `LoggingIntegration`'s `sentry_logs_level` parameter. However, regardless of the `sentry_logs_level` setting, the SDK only sends logs if they are at or above the logger's level. +By default, the logging integration sends `INFO`-level logs and higher to Sentry logs. You can set a different threshold via the `LoggingIntegration`'s `sentry_logs_level` parameter. However, regardless of the `sentry_logs_level` setting, the SDK only sends logs if they are at or above the logger's level. ```python import sentry_sdk @@ -61,15 +61,79 @@ from sentry_sdk.integrations.logging import SentryLogsHandler from sentry_sdk.integrations.logging import LoggingIntegration sentry_sdk.init( - dsn="...", - _experiments={ - "enable_logs": True - }, - integrations=[ - LoggingIntegration(sentry_logs_level=None), # Do not monkeypatch the sentry handler - ] + dsn="___PUBLIC_DSN___", + _experiments={ + "enable_logs": True + }, + integrations=[ + LoggingIntegration(sentry_logs_level=None), # Do not monkeypatch the sentry handler + ], } # Instead, configure the root logger to send INFO-level logs to Sentry logging.basicConfig(level=logging.INFO, handlers=[SentryLogsHandler(level=logging.INFO)]) ``` + +### Loguru + +Loguru logs can also be automatically captured and sent to Sentry. + +```python +import sentry_sdk +from loguru import logger + +sentry_sdk.init( + dsn="___PUBLIC_DSN___", + _experiments={ + "enable_logs": True, + }, +) + +loguru.debug("In this example, debug events will not be sent to Sentry logs.") +loguru.info("On the other hand, info events will be sent to Sentry logs.") +``` + +By default, the Loguru integration sends `INFO`-level and higher logs to Sentry logs as long as `enable_logs` is `True`. A different threshold can be set via the `LoguruIntegration`'s `sentry_logs_level` parameter. + +```python +import sentry_sdk +from loguru import logger +from sentry_sdk.integrations.loguru import LoggingLevels, LoguruIntegration + +sentry_sdk.init( + dsn="___PUBLIC_DSN___", + _experiments={ + "enable_logs": True + }, + integrations=[ + # Only send WARNING (and higher) logs to Sentry logs + LoguruIntegration(sentry_logs_level=LoggingLevels.WARNING.value), + ], +) + +logger.info("This INFO log won't be sent to Sentry logs.") +logger.warning("This WARNING log will be sent to Sentry logs.") +``` + +If you want other logging integrations to send logs to Sentry logs, but not Loguru, setting `sentry_logs_level` to `None` on the integration level will stop the Loguru integration for capturing Sentry logs. + +```python + +import sentry_sdk +from loguru import logger +from sentry_sdk.integrations.loguru import LoguruIntegration + +sentry_sdk.init( + dsn="___PUBLIC_DSN___", + _experiments={ + # In general, we want to capture logs as Sentry logs... + "enable_logs": True, + }, + integrations=[ + # ...just not from Loguru + LoguruIntegration(sentry_logs_level=None), + ] +) + +loguru.error("This won't be sent to Sentry logs.") +```