diff options
author | David Heinemeier Hansson <david@basecamp.com> | 2014-05-29 16:14:28 +0200 |
---|---|---|
committer | David Heinemeier Hansson <david@basecamp.com> | 2014-05-29 16:14:28 +0200 |
commit | 79d0adef3869c1c4d9c203a4ca9e96875295fdea (patch) | |
tree | 170a585c28b1da538aa4aabe893c0e84d9f92398 /lib/active_job/logging.rb | |
parent | 895f06a2e16bb5da2d56f5bccc304d90c13836c4 (diff) | |
parent | 6eb163a706635c0ab5ede9056a0720f1a5f18f22 (diff) | |
download | rails-79d0adef3869c1c4d9c203a4ca9e96875295fdea.tar.gz rails-79d0adef3869c1c4d9c203a4ca9e96875295fdea.tar.bz2 rails-79d0adef3869c1c4d9c203a4ca9e96875295fdea.zip |
Merge pull request #61 from cristianbica/tagged-logging
Tagged logging
Diffstat (limited to 'lib/active_job/logging.rb')
-rw-r--r-- | lib/active_job/logging.rb | 50 |
1 files changed, 39 insertions, 11 deletions
diff --git a/lib/active_job/logging.rb b/lib/active_job/logging.rb index ede1606027..ea0ba2dddc 100644 --- a/lib/active_job/logging.rb +++ b/lib/active_job/logging.rb @@ -3,26 +3,51 @@ require 'active_support/core_ext/string/filters' module ActiveJob module Logging extend ActiveSupport::Concern - + included do cattr_accessor(:logger) { ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new(STDOUT)) } + around_enqueue do |job, block, _| + tag_logger do + block.call + end + end + + around_perform do |job, block, _| + tag_logger(job.class.name, job.job_id) do + payload = {adapter: job.class.queue_adapter, job: job.class, args: job.arguments} + ActiveSupport::Notifications.instrument("perform_start.active_job", payload.dup) + ActiveSupport::Notifications.instrument("perform.active_job", payload) do |payload| + block.call + end + end + end + before_enqueue do |job| if job.enqueued_at - ActiveSupport::Notifications.instrument "enqueue_at.active_job", + ActiveSupport::Notifications.instrument "enqueue_at.active_job", adapter: job.class.queue_adapter, job: job.class, args: job.arguments, timestamp: job.enqueued_at else ActiveSupport::Notifications.instrument "enqueue.active_job", adapter: job.class.queue_adapter, job: job.class, args: job.arguments end end - - before_perform do |job| - ActiveSupport::Notifications.instrument "perform.active_job", - adapter: job.class.queue_adapter, job: job.class, args: job.arguments - end end - + + private + def tag_logger(*tags) + if logger.respond_to?(:tagged) + tags.unshift "ActiveJob" unless logger_tagged_by_active_job? + ActiveJob::Base.logger.tagged(*tags){ yield } + else + yield + end + end + + def logger_tagged_by_active_job? + logger.formatter.current_tags.include?("ActiveJob") + end + class LogSubscriber < ActiveSupport::LogSubscriber def enqueue(event) info "Enqueued #{event.payload[:job].name} to #{queue_name(event)}" + args_info(event) @@ -32,10 +57,13 @@ module ActiveJob info "Enqueued #{event.payload[:job].name} to #{queue_name(event)} at #{enqueued_at(event)}" + args_info(event) end - def perform(event) - info "Performed #{event.payload[:job].name} from #{queue_name(event)}" + args_info(event) + def perform_start(event) + info "Performing #{event.payload[:job].name} from #{queue_name(event)}" + args_info(event) end + def perform(event) + info "Performed #{event.payload[:job].name} from #{queue_name(event)} in #{event.duration.round(2).to_s}ms" + end private def queue_name(event) @@ -43,7 +71,7 @@ module ActiveJob end def args_info(event) - event.payload[:args].any? ? ": #{event.payload[:args].inspect}" : "" + event.payload[:args].any? ? " with arguments: #{event.payload[:args].map(&:inspect).join(", ")}" : "" end def enqueued_at(event) |