Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sidekiq stacktrace in logs

I'm running a sidekiq application on heroku with papertrails addon and I use exceptions to fail jobs. For each exception full stacktrace is stored in papertrail logs which is definitely not what I want.

I didn't find a way how to turn off that feature. Could you give me a hint what I could do with that? Maybe I should handle job failing in a different way?

Thanks!

like image 478
alobodzk Avatar asked Mar 17 '26 07:03

alobodzk


1 Answers

Here's a modification of the standard error logger that limits the backtrace logging to the lines unique to the application:

class ExceptionHandlerLogger
  def call(ex, ctxHash)
    Sidekiq.logger.warn(Sidekiq.dump_json(ctxHash)) if !ctxHash.empty?
    Sidekiq.logger.warn "#{ex.class.name}: #{ex.message}"

    unless ex.backtrace.nil?
      Sidekiq.logger.warn filter_backtrace(ex.backtrace).join("\n")
    end
  end

  def filter_backtrace(backtrace)
    index = backtrace.index { |item| item.include?('/lib/sidekiq/processor.rb') }
    backtrace.first(index.to_i)
  end
end

if !Sidekiq.error_handlers.delete_if { |h| h.class == Sidekiq::ExceptionHandler::Logger }
  fail "default sidekiq logger class changed!"
end

Sidekiq.error_handlers << ExceptionHandlerLogger.new
like image 68
iloveitaly Avatar answered Mar 19 '26 00:03

iloveitaly