Introduction
The Lescopr gem supports Sinatra through a dedicated extension — Lescopr::Integrations::Sinatra::Extension.
Once registered, all unhandled exceptions in your Sinatra routes are automatically captured and reported to your Lescopr dashboard.
lescopr is available on RubyGems.org and works with Sinatra 2.0+.
Requirements
Before installing the Lescopr gem, make sure your environment meets the following requirements:
- Ruby >= 2.7
- Sinatra >= 2.0
- A valid
LESCOPR_SDK_KEYandLESCOPR_API_KEY(available from your Lescopr dashboard) - Bundler (recommended)
Installation
Add the lescopr gem to your Gemfile:
Then run Bundler:
Or install it directly:
gem 'lescopr'bundle installgem install lescoprConfiguration
Initialise the SDK before your application boots, then register the extension on your Sinatra class:
You can use environment variables to keep your keys out of version control:
Available options:
sdk_key— Your Lescopr SDK key (required)api_key— Your Lescopr API key (required)environment— The current environment (default:ENV['RACK_ENV'])
require 'sinatra'
require 'lescopr'
Lescopr.init!(
sdk_key: ENV['LESCOPR_SDK_KEY'],
api_key: ENV['LESCOPR_API_KEY']
)
class MyApp < Sinatra::Base
register Lescopr::Integrations::Sinatra::Extension
get '/' do
lescopr_log(:info, 'Home page visited')
'Hello!'
end
endLESCOPR_SDK_KEY=lsk_xxxx
LESCOPR_API_KEY=lak_xxxx
LESCOPR_ENVIRONMENT=productionUsage
After registering the extension, all unhandled exceptions are captured automatically.
You can also log events manually from any route using the lescopr_log helper:
Or call the SDK directly from anywhere in your code:
get '/orders' do
lescopr_log(:info, 'Orders listed', { user_id: session[:user_id] })
# ...
endbegin
risky_operation
rescue => e
Lescopr.report(e)
raise
endAdvanced
Rack middleware
You can also use the lower-level Rack middleware directly in config.ru, which works with any Rack-compatible framework:
Environment variables
LESCOPR_SDK_KEY— SDK key (overrides.lescopr.json)LESCOPR_API_KEY— API keyLESCOPR_ENVIRONMENT—developmentorproductionLESCOPR_DEBUG=true— Enables verbose output to.lescopr.log
require 'lescopr'
Lescopr.init!(sdk_key: ENV['LESCOPR_SDK_KEY'], api_key: ENV['LESCOPR_API_KEY'])
use Lescopr::Integrations::Rack::Middleware
run MyApp