1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Properly log nested parameters to Active Job

Refactor arguments logging method for Active Job
This commit is contained in:
Marek Pieczyk 2015-09-06 23:39:42 +02:00
parent 997acb9aad
commit 26f37f7c40
2 changed files with 23 additions and 1 deletions

View file

@ -1,3 +1,4 @@
require 'active_support/core_ext/hash/transform_values'
require 'active_support/core_ext/string/filters'
require 'active_support/tagged_logging'
require 'active_support/logger'
@ -87,12 +88,25 @@ module ActiveJob
def args_info(job)
if job.arguments.any?
' with arguments: ' +
job.arguments.map { |arg| arg.try(:to_global_id).try(:to_s) || arg.inspect }.join(', ')
job.arguments.map { |arg| format(arg).inspect }.join(', ')
else
''
end
end
def format(arg)
case arg
when Hash
arg.transform_values { |value| format(value) }
when Array
arg.map { |value| format(value) }
when GlobalID::Identification
arg.to_global_id rescue arg
else
arg
end
end
def scheduled_at(event)
Time.at(event.payload[:job].scheduled_at).utc
end

View file

@ -74,6 +74,14 @@ class LoggingTest < ActiveSupport::TestCase
assert_match(%r{Performing.*gid://aj/Person/123}, @logger.messages)
end
def test_globalid_nested_parameter_logging
person = Person.new(123)
LoggingJob.perform_later(person: person)
assert_match(%r{Enqueued.*gid://aj/Person/123}, @logger.messages)
assert_match(%r{Dummy, here is it: .*#<Person:.*>}, @logger.messages)
assert_match(%r{Performing.*gid://aj/Person/123}, @logger.messages)
end
def test_enqueue_job_logging
HelloJob.perform_later "Cristian"
assert_match(/Enqueued HelloJob \(Job ID: .*?\) to .*?:.*Cristian/, @logger.messages)