A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://docs.sentry.io/platforms/python/integrations/logging/ below:

Logging | Sentry for Python

Logging Learn about logging with Python.

Adds support for Python logging.

Install

Install sentry-sdk from PyPI:

Configure

The logging integrations is a default integration so it will be enabled automatically when you initialize the Sentry SDK.

Copied

import sentry_sdk

sentry_sdk.init(
    dsn="https://examplePublicKey@o0.ingest.sentry.io/0",
    
    
    send_default_pii=True,
)
Verify

Copied

import logging

def main():
    sentry_sdk.init(...)  

    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()

Log records can additionally also be captured as Sentry logs as long as the enable_logs experimental option is True.

Copied

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:

Copied

import logging
import sentry_sdk
from sentry_sdk.integrations.logging import LoggingIntegration




logging.basicConfig(level=logging.INFO)

sentry_sdk.init(
    
    integrations=[
        LoggingIntegration(
            level=logging.INFO,              
            event_level=logging.ERROR,       
            sentry_logs_level=logging.INFO,  
        ),
    ],
)

You can pass the following keyword arguments to LoggingIntegration():

The Sentry Python SDK will honor the configured level of each logger (set with logger.setLevel(level) or logging.basicConfig(level=level)). That means that you will not see any INFO or DEBUG events from a logger with the level set to WARNING, regardless of how you configure the integration. If not set explicitly, the logging level defaults to WARNING.

Ignoring a logger

Sometimes a logger is extremely noisy and spams you with pointless errors. You can ignore that logger by calling ignore_logger:

Copied

from sentry_sdk.integrations.logging import ignore_logger

ignore_logger("a.spammy.logger")

logger = logging.getLogger("a.spammy.logger")
logger.error("hi")  

You can also use before-send and before-breadcrumb to ignore only certain messages. See Filtering Events for more information.

Handler classes

Instead of using LoggingIntegration, you can use two regular logging logging.Handler subclasses that the integration exports.

Usually, you don't need this. You can use this together with default_integrations=False if you want to opt into what the Sentry Python SDK captures. However, correctly setting up logging is difficult. Also, an opt-in approach to capture data will miss errors you may not think of on your own.

See the API documentation for more information.

Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4