Introduction
The Lescopr gem integrates with Hanami through the standard Rack middleware stack.
Hanami is fully Rack-compatible, so Lescopr::Integrations::Rack::Middleware can be mounted directly and will capture all unhandled exceptions across your slices.
lescopr is available on RubyGems.org and works with Hanami 2.0+.
Requirements
Before installing the Lescopr gem, make sure your environment meets the following requirements:
- Ruby >= 2.7
- Hanami >= 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 in your Hanami app boot file (e.g. config/app.rb or a dedicated provider) and add the middleware to the stack:
Set your keys via environment variables or .lescopr.json:
Available options:
sdk_key— Your Lescopr SDK key (required)api_key— Your Lescopr API key (required)environment— The current environment (default:ENV['HANAMI_ENV'])
Hanami.app.register_provider(:lescopr) do
start do
require 'lescopr'
Lescopr.init!(
sdk_key: ENV['LESCOPR_SDK_KEY'],
api_key: ENV['LESCOPR_API_KEY'],
environment: ENV['HANAMI_ENV']
)
end
endmodule MyApp
class App < Hanami::App
config.middleware.use Lescopr::Integrations::Rack::Middleware
end
endLESCOPR_SDK_KEY=lsk_xxxx
LESCOPR_API_KEY=lak_xxxx
HANAMI_ENV=productionUsage
Once the middleware is mounted, all unhandled exceptions are captured automatically across every slice.
You can also log custom events from any action or interactor:
The daemon flushes the queue to the Lescopr API every 5 seconds in a background thread — completely non-blocking.
module MyApp
module Actions
module Orders
class Index < MyApp::Action
def handle(request, response)
Lescopr.log(:info, 'Orders listed', { user_id: request.session[:user_id] })
# ...
end
end
end
end
endAdvanced
Container registration (Hanami 2)
You can register the Lescopr client as a provider in config/providers/lescopr.rb so it is available via dependency injection throughout your slices.
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
Hanami.app.register_provider(:lescopr) do
start do
require 'lescopr'
client = Lescopr.init!(
sdk_key: ENV['LESCOPR_SDK_KEY'],
api_key: ENV['LESCOPR_API_KEY']
)
register('lescopr.client', client)
end
end