Back to Guides
Flask Integration Guide
Learn how to integrate Sift Dev logging with your Flask applications.
1. Installation
Install the Sift Dev Logger SDK with Flask support:
pip install sift-dev-logger
pip install "sift-dev-logger[flask]"
2. Environment Setup
Create a .env
file in your project root.
Loading...
# Your Sift Dev endpoint and api key
SIFT_DEV_ENDPOINT=http://<YOUR-ORG-ID>.app.trysift.dev:8000
SIFT_DEV_INGEST_KEY=<YOUR-API-KEY>
# Optional configuration (defaults shown below)
SIFT_DEV_SERVICE_NAME=flask-app
SIFT_DEV_SERVICE_INSTANCE_ID=instance-1
ENV=unspecified
3. Configuration (Optional)
💡 Configuration Options
- Environment variables are automatically detected from your system environment if present
- Manual configuration via code will override environment variables
- Configure must be called before using flask_logger or getLogger
- Flask-specific features:
- Automatic request and response logging
- Performance metrics for each route
- Error tracking with full stack traces
- Request context in all logs
Configuration options and defaults
1from flask import Flask
2from sift_dev_logger import SiftDevConfig, configure
3from sift_dev_logger.flask import flask_logger
4
5# Optional: Configure with defaults or environment variables
6configure(SiftDevConfig(
7 sift_dev_endpoint="Initialize here, or set SIFT_DEV_ENDPOINT env var",
8 sift_dev_ingest_key="Initialize here, or set SIFT_DEV_INGEST_KEY env var",
9 service_name="flask-app",
10 service_instance_id="instance-1",
11 env="unspecified",
12 batch_delay_millis=5000
13))
14
15app = Flask(__name__)
16
17# Initialize the logger with your Flask app
18# Uses previous configuration, or creates a new default config if none exists
19flask_logger(app)
4. Usage
Flask middleware and custom logging
1from flask import Flask
2from sift_dev_logger.flask import flask_logger
3from sift_dev_logger import getLogger, SiftDevConfig
4
5app = Flask(__name__)
6
7# Initialize with custom configuration
8flask_logger(
9 app,
10 config=SiftDevConfig(service_name="custom-flask-app"), # Optional custom config
11 max_body_size=50_000, # Maximum response body size to log (bytes)
12 ignored_paths={"/health", "/metrics"} # Paths to exclude from logging
13)
14
15# Get a logger for custom logging
16logger = getLogger()
17
18@app.route('/users/<user_id>')
19def get_user(user_id):
20 # Custom log with request context
21 logger.info("Processing user request", extra={
22 "user_id": user_id,
23 "action": "get_user"
24 })
25
26 try:
27 user = {"id": user_id, "name": "John Doe"}
28 return user
29 except Exception as error:
30 logger.error("User lookup failed", exc_info=error)
31 return {"error": "Not found"}, 404
Configuring Flask's internal logging
Instead of using the middleware, you can capture Flask's internal logs (Werkzeug) by adding our handler to the existing logger:
1import logging
2from sift_dev_logger import SiftDevHandler, SiftDevConfig
3
4# Configure Werkzeug (Flask's default logger) with Sift Dev
5werkzeug_logger = logging.getLogger('werkzeug')
6werkzeug_logger.setLevel(logging.INFO)
7
8# Add SiftDev handler to capture Flask's internal logs
9handler = SiftDevHandler(config=SiftDevConfig(service_name="flask-internal"))
10werkzeug_logger.addHandler(handler)
5. Advanced Usage
Next Steps
Now that you've set up logging in your Flask application:
- View your logs in the Logs Dashboard
- Set up alerts and notifications
- Configure custom log attributes for better filtering
- Explore our other framework integrations