references/ruby.md · 24000
out of the box. See our Rails error tracking guide for details.
For non-Rails Ruby applications, you can manually capture exceptions with capture_exception:
begin
# Code that might raise an exception
raise StandardError, 'Something went wrong'
rescue => e
posthog.capture_exception(
e,
'user_distinct_id',
{
custom_property: 'custom_value'
}
)
end
The capture_exception method accepts the following parameters:
| Parameter | Type | Description |
| --- | --- | --- |
| exception | Exception, String, or exception-like object | The exception to capture. Required. |
| distinct_id | String | The distinct ID of the user. Optional; request context can provide a default, otherwise the SDK generates a UUID. |
| additional_properties | Hash | Additional properties to attach to the exception event. Optional. |
| flags | PostHog::FeatureFlagEvaluations | Optional keyword argument. Adds the same feature flag properties as capture({ flags: flags }). |
You can also override the fingerprint to customize how exceptions are grouped into issues:
posthog.capture_exception(
e,
'user_distinct_id',
{
'$exception_fingerprint': 'CustomExceptionGroup'
}
)
Debug mode
The Ruby SDK logs warnings by default. You can change the log level to DEBUG to debug the client:
posthog.logger.level = Logger::DEBUG
You can also replace the SDK logger globally:
PostHog::Logging.logger = Rails.logger
Test helpers
When test_mode: true, events remain queued. You can inspect and clear the queue in tests:
posthog = PostHog::Client.new({ api_key: '<ph_project_token>', test_mode: true })
posthog.capture({ distinct_id: 'user_123', event: 'test_event' })
posthog.queued_messages
posthog.dequeue_last_message
posthog.clear
Thank you
This library is largely based on the analytics-ruby package.