Sentry Error Logging for Flask Requests

  • Share this:

Code introduction


This function is used to log exceptions raised by Flask requests and perform error tracking with the Sentry SDK. It initializes Sentry with Flask integration and captures exceptions, while also attaching additional request information.


Technology Stack : Sentry, Flask

Code Type : Function

Code Difficulty : Intermediate


                
                    
import random
import sentry_sdk
from sentry_sdk.integrations.flask import FlaskIntegration

def log_error_with_sentry(request, error):
    # Initialize Sentry SDK with Flask integration
    sentry_sdk.init(
        dsn="your_dsn_here",
        integrations=[FlaskIntegration()]
    )
    
    # Log the error with additional data from the request
    sentry_sdk.capture_exception(error, extra={"request": request.as_dict()})

# JSON representation of the code                
              
Tags: