Pass more context from Sidekiq jobs to Logster

This commit is contained in:
riking 2014-07-11 15:03:07 -07:00
parent ede8f22971
commit 2b5a955c18
2 changed files with 20 additions and 9 deletions

View file

@ -43,18 +43,28 @@ end
Sidekiq.logger.level = Logger::WARN
class LogsterErrorHandler
def call(ex, hash={})
text = "exception: #{ex}\ncontext: #{hash.inspect}\n"
class SidekiqLogsterReporter
def call(ex, context = {})
# Pass context to Logster
fake_env = {}
context.each do |key, value|
Logster.add_to_env(fake_env, key, value)
end
text = "Job exception: #{ex}\n"
if ex.backtrace
text << "backtrace: #{ex.backtrace.join("\n")}"
Logster.add_to_env(fake_env, :backtrace, ex.backtrace)
end
Rails.logger.error(text)
Thread.current[Logster::Logger::LOGSTER_ENV] = fake_env
Logster.logger.error(text)
rescue => e
Rails.logger.fatal("Failed to log exception #{ex} #{hash}\nReason: #{e}\n#{e.backtrace.join("\n")}")
Logster.logger.fatal("Failed to log exception #{ex} #{hash}\nReason: #{e.class} #{e}\n#{e.backtrace.join("\n")}")
ensure
Thread.current[Logster::Logger::LOGSTER_ENV] = nil
end
end
Sidekiq.error_handlers << LogsterErrorHandler.new
Sidekiq.error_handlers << SidekiqLogsterReporter.new

View file

@ -9,6 +9,7 @@ module Discourse
extend Sidekiq::ExceptionHandler
end
# Responsible for exceptions in Sidekiq jobs - not requests!
def self.handle_exception(ex, context = nil, parent_logger = nil)
context ||= {}
parent_logger ||= SidekiqExceptionHandler