lib/heavylog.rb in heavylog-0.0.8 vs lib/heavylog.rb in heavylog-0.0.9
- old
+ new
@@ -4,10 +4,12 @@
require 'heavylog/formatters/json'
require 'heavylog/log_subscriber'
require 'heavylog/middleware'
require 'heavylog/ordered_options'
require 'heavylog/request_logger'
+require 'heavylog/sidekiq_logger'
+require 'heavylog/sidekiq_exception_handler'
module Heavylog
module_function
TRUNCATION = '[TRUNCATED]'.freeze
@@ -16,10 +18,11 @@
def setup(app)
self.application = app
patch_loggers
attach_to_action_controller
+ attach_to_sidekiq
setup_custom_payload
set_options
end
def patch_loggers
@@ -38,10 +41,20 @@
def attach_to_action_controller
Heavylog::LogSubscriber.attach_to :action_controller
end
+ def attach_to_sidekiq
+ return if !config.log_sidekiq
+
+ Sidekiq.configure_server do |config|
+ config.options[:job_logger] = SidekiqLogger
+ end
+
+ Sidekiq.error_handlers << SidekiqExceptionHandler.new
+ end
+
def setup_custom_payload
return unless config.custom_payload_method.respond_to?(:call)
klasses = Array(config.base_controller_class)
klasses.map! { |klass| klass.try(:constantize) }
@@ -83,10 +96,26 @@
else
RequestStore.store[:heavylog_buffer].puts(message)
end
end
+ def log_sidekiq(jid, klass, args)
+ return if !config.enabled
+
+ RequestStore.store[:heavylog_request_id] = jid
+ RequestStore.store[:heavylog_request_start] = Time.now.iso8601
+ RequestStore.store[:heavylog_request_ip] = "127.0.0.1"
+
+ RequestStore.store[:heavylog_request_data] = {
+ controller: "SidekiqLogger",
+ action: klass,
+ args: args.to_s,
+ }
+
+ RequestStore.store[:heavylog_buffer] ||= StringIO.new
+ end
+
def finish
return if !config.enabled
buffer = RequestStore.store[:heavylog_buffer]
return if !buffer
@@ -98,9 +127,14 @@
messages: buffer.string.dup
}.merge(RequestStore.store[:heavylog_request_data] || {})
formatted = Heavylog.formatter.call(request)
Heavylog.logger.send(Heavylog.log_level, formatted)
+ end
+
+ def finish_sidekiq
+ finish
+ RequestStore.store[:heavylog_buffer] = nil
end
def config
return OrderedOptions.new if !application
application.config.heavylog