Guides / Platforms & Tools

Business Event Logging for Ruby on Rails

4 min read·Platforms & Tools

Setup: A Simple LogIt Client

Ruby's standard library includes net/http, so no gem is needed. Add a shared client:

ruby
# app/services/logit_client.rb
require "net/http"
require "json"
require "uri"

class LogitClient
  ENDPOINT = URI("https://api.logit.now/api/ingest").freeze

  def self.log(channel:, event:, description:, icon: "📌", notify: false, tags: {}, metadata: {})
    http = Net::HTTP.new(ENDPOINT.host, ENDPOINT.port)
    http.use_ssl = true

    request = Net::HTTP::Post.new(ENDPOINT)
    request["Content-Type"] = "application/json"
    request["Authorization"] = "Bearer #{ENV.fetch("LOGIT_API_KEY")}"
    request.body = {
      channel: channel,
      event: event,
      description: description,
      icon: icon,
      notify: notify,
      tags: tags,
      metadata: metadata,
    }.to_json

    http.request(request)
  rescue => e
    Rails.logger.warn "LogitClient failed: #{e.message}"
  end
end

Log a Signup in a Controller

ruby
# app/controllers/registrations_controller.rb
def create
  @user = User.new(user_params)
  if @user.save
    LogitClient.log(
      channel: "signups",
      event: "New user signed up",
      description: @user.email,
      icon: "👤",
      notify: true,
      tags: { plan: @user.plan, source: params[:utm_source] || "direct" },
      metadata: { user_id: @user.id.to_s, email: @user.email }
    )
    redirect_to dashboard_path
  else
    render :new
  end
end

Log in an Active Job

ruby
# app/jobs/subscription_cancelled_job.rb
class SubscriptionCancelledJob < ApplicationJob
  queue_as :default

  def perform(subscription_id)
    subscription = Subscription.find(subscription_id)

    LogitClient.log(
      channel: "churn",
      event: "Subscription cancelled",
      description: "#{subscription.user.email} cancelled #{subscription.plan}",
      icon: "😢",
      notify: true,
      tags: { plan: subscription.plan, reason: subscription.cancel_reason || "unspecified" },
      metadata: { subscription_id: subscription_id, user_id: subscription.user_id.to_s }
    )
  end
end

Log from a Stripe Webhook

ruby
# app/controllers/webhooks/stripe_controller.rb
def create
  payload = request.body.read
  event = Stripe::Webhook.construct_event(payload, request.env["HTTP_STRIPE_SIGNATURE"], ENV["STRIPE_WEBHOOK_SECRET"])

  case event.type
  when "checkout.session.completed"
    session = event.data.object
    LogitClient.log(
      channel: "payments",
      event: "Payment received",
      description: "#{session.customer_email}#{format_amount(session.amount_total, session.currency)}",
      icon: "💰",
      notify: true,
      tags: { currency: session.currency },
      metadata: { session_id: session.id, customer: session.customer }
    )
  end

  head :ok
end

Tips

  • Wrap the LogitClient.log call in a rescue so a LogIt failure never crashes your request.
  • Add LOGIT_API_KEY to your Rails credentials or environment config.
  • For background logging (non-blocking), enqueue a lightweight ActiveJob that calls LogitClient.log.

Try LogIt free

7-day trial. No credit card required.

Start free