FastAPI — Lescopr SDK

Integrate the Lescopr SDK into your FastAPI application for automatic log capture, exception tracking, and real-time monitoring.

Introduction

The Lescopr Python SDK integrates with FastAPI by hooking into Python's logging module at application startup.

Add lescopr.logs() before instantiating your FastAPI app and every logging.getLogger(...) call in your codebase — including third-party libraries — will be captured and forwarded in real-time to your Lescopr dashboard.

The package is available on PyPI and requires Python 3.8+ and FastAPI 0.70+.

Requirements

Before installing the Lescopr SDK, make sure your environment meets the following requirements:

  • Python >= 3.8
  • FastAPI >= 0.70
  • An ASGI server: Uvicorn or Hypercorn
  • A valid LESCOPR_SDK_KEY and API key (available from your Lescopr dashboard)

Optional: Install lescopr[psutil] for enhanced daemon process detection.

Installation

Install the package from PyPI:

Or with the optional psutil dependency:

Terminal
bash
pip install lescopr
Terminal
bash
pip install lescopr[psutil]

Configuration

Initialise the SDK in your project directory:

Then add lescopr.logs() to the top of your main.py, before the FastAPI() constructor:

Available options (direct instantiation):

  • sdk_key — Your Lescopr SDK key (required)
  • api_key — Your Lescopr API key (required)
  • environment — development (default) or production
  • auto_logging — Attach to root logger immediately (default: True)
  • auto_grpc — Connect to daemon immediately (default: True)
Terminal
bash
lescopr init --sdk-key YOUR_SDK_KEY
main.py
python
import lescopr
lescopr.logs()

from fastapi import FastAPI
app = FastAPI()

@app.get('/')
async def root():
    return {'message': 'Hello World'}

Usage

Once lescopr.logs() is called, all logging calls throughout your application are forwarded automatically:

You can also use a lifespan handler to ensure the daemon is connected before the first request:

main.py
python
import logging
logger = logging.getLogger(__name__)

@app.post('/orders')
async def create_order(order: OrderSchema):
    logger.info('Order received', extra={'order_id': order.id})
    try:
        result = await process(order)
        return result
    except Exception as e:
        logger.error('Processing failed', exc_info=True)
        raise
main.py
python
from contextlib import asynccontextmanager
import lescopr

@asynccontextmanager
async def lifespan(app: FastAPI):
    lescopr.logs()   # ensure daemon connected before first request
    yield

app = FastAPI(lifespan=lifespan)

Advanced

Direct instantiation

For full control, instantiate the SDK directly instead of using .lescopr.json:

Debug mode

Enable verbose internal output to troubleshoot the connection:

CLI commands

  • lescopr init --sdk-key KEY — Initialise and register the project
  • lescopr start / stop / status — Manage the background daemon
  • lescopr diagnose --verbose --check-server — Full connectivity report

Environment variables

  • LESCOPR_DAEMON_MODE=true — Prevents the SDK from starting inside its own daemon process
main.py
python
from lescopr import Lescopr

client = Lescopr(
    sdk_key='YOUR_SDK_KEY',
    api_key='YOUR_API_KEY',
    environment='production',
)
main.py
python
import lescopr
lescopr.set_debug_mode()
lescopr.logs()