logging from service objects

24 Views Asked by At

In rails, how can I log from a service object used by both my controllers and workers ? Should I use Rails.logger ? Sidekiq.logger ? How should they be configured ?

# jobs/calculate_distances_job.rb
class CalculateDistancesJob < ApplicationJob


  def perform(home)
    distance = DistanceCalculator.new.calculate home, from: :shore_line
  end

end

# controllers/homes_controller.rb
class HomeController < ApplicationController


  def create
    ...
    distance = DistanceCalculator.new.distance_of @home, from: :shore_line
  end

end

# services/distance_calculator.rb
class DistanceCalculator

  def distance_of(args)
     ...
  rescue 
      Rails.logger.warn "an error occured"
  end

end

0

There are 0 best solutions below