rails_semantic_logger 4.4.0 → 4.4.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c3247822b891055f310e6e68af2b31624876f5a523f0768ef2d17caa9364331a
4
- data.tar.gz: bb8d1aad6d97d292973ea2a60327d173bfb678c5287added39897ddaf218540d
3
+ metadata.gz: c831151af372b8348f982154c5a02da3e3373f0156f36952dcebc421667f91fc
4
+ data.tar.gz: 82e796f801009b66c39daf81b9526d5be20f48ecf81cd5159a7d659ba64f0cdc
5
5
  SHA512:
6
- metadata.gz: 3220a7c5b8310af1fca09ec48b6e418e9e7114ac3bac6604d26b455b624b305e2bc3d6e1e2ef2240cbebd844a1400fad2d118cea23ae7ba38854517554251f96
7
- data.tar.gz: 66d4ebb1c3169b46128246511c77af1b0fb3982d187077aa3fe92c1425ba5e3eb235159b8706eba973f84872fb2f845a9d19d155223f3f2ab3b95a91d490019d
6
+ metadata.gz: 564d2e2b9a0bdb7140dd37a9be1aaaccbeed5c06f60cd7c4c31a7d886502d0e77433da3887e8262b4d614752c8a7f2b677fc8be8d9796e8e8af772252c84c2e2
7
+ data.tar.gz: 1a22dc7a52079a3845a5453f85eafda8edda153166afc44fe973cbe02f4e5d5b0f145c8bd506da972afcf942b0f9ccbbc802c1c18aab20424d54fa37b3bef685
@@ -10,5 +10,126 @@ module ActiveJob
10
10
  def tag_logger(*tags, &block)
11
11
  logger.tagged(*tags, &block)
12
12
  end
13
+
14
+ class LogSubscriber < ActiveSupport::LogSubscriber
15
+ def enqueue(event)
16
+ log_with_formatter event: event do |fmt|
17
+ "Enqueued #{fmt.job_info}"
18
+ end
19
+ end
20
+
21
+ def enqueue_at(event)
22
+ log_with_formatter event: event do |fmt|
23
+ "Enqueued #{fmt.job_info} at #{fmt.scheduled_at}"
24
+ end
25
+ end
26
+
27
+ def perform_start(event)
28
+ log_with_formatter event: event do |fmt|
29
+ "Performing #{fmt.job_info}"
30
+ end
31
+ end
32
+
33
+ def perform(event)
34
+ ex = event.payload[:exception_object]
35
+ if ex
36
+ logger.error ex
37
+ else
38
+ log_with_formatter event: event, log_duration: true do |fmt|
39
+ "Performed #{fmt.job_info} in #{event.duration.round(2)}ms"
40
+ end
41
+ end
42
+ end
43
+
44
+ private
45
+
46
+ class EventFormatter
47
+ def initialize(event:, log_duration: false)
48
+ @event = event
49
+ @log_duration = log_duration
50
+ end
51
+
52
+ def job_info
53
+ "#{job.class.name} (Job ID: #{job.job_id}) to #{queue_name}"
54
+ end
55
+
56
+ def payload
57
+ {}.tap do |h|
58
+ h[:event_name] = event.name
59
+ h[:adapter] = adapter_name
60
+ h[:queue] = job.queue_name
61
+ h[:job_class] = job.class.name
62
+ h[:job_id] = job.job_id
63
+ h[:provider_job_id] = job.try(:provider_job_id) # Not available in Rails 4.2
64
+ h[:duration] = event.duration.round(2) if log_duration?
65
+ h[:arguments] = formatted_args
66
+ end
67
+ end
68
+
69
+ def queue_name
70
+ adapter_name + "(#{job.queue_name})"
71
+ end
72
+
73
+ def scheduled_at
74
+ Time.at(event.payload[:job].scheduled_at).utc
75
+ end
76
+
77
+ private
78
+
79
+ attr_reader :event
80
+
81
+ def job
82
+ event.payload[:job]
83
+ end
84
+
85
+ def adapter_name
86
+ event.payload[:adapter].class.name.demodulize.remove('Adapter')
87
+ end
88
+
89
+ def formatted_args
90
+ JSON.pretty_generate(job.arguments.map { |arg| format(arg) })
91
+ end
92
+
93
+ def format(arg)
94
+ case arg
95
+ when Hash
96
+ arg.transform_values { |value| format(value) }
97
+ when Array
98
+ arg.map { |value| format(value) }
99
+ when GlobalID::Identification
100
+ begin
101
+ arg.to_global_id
102
+ rescue StandardError
103
+ arg
104
+ end
105
+ else
106
+ arg
107
+ end
108
+ end
109
+
110
+ def log_duration?
111
+ @log_duration
112
+ end
113
+ end
114
+
115
+ def log_with_formatter(**kw_args)
116
+ fmt = EventFormatter.new(**kw_args)
117
+ msg = yield fmt
118
+ logger.info msg, fmt.payload
119
+ end
120
+
121
+ def logger
122
+ ActiveJob::Base.logger
123
+ end
124
+ end
13
125
  end
14
126
  end
127
+
128
+ if defined?(ActiveSupport::Notifications)
129
+ ActiveSupport::Notifications.unsubscribe('perform_start.active_job')
130
+ ActiveSupport::Notifications.unsubscribe('perform.active_job')
131
+ ActiveSupport::Notifications.unsubscribe('enqueue_at.active_job')
132
+ ActiveSupport::Notifications.unsubscribe('enqueue.active_job')
133
+
134
+ ActiveJob::Logging::LogSubscriber.attach_to :active_job
135
+ end
@@ -1,3 +1,3 @@
1
1
  module RailsSemanticLogger
2
- VERSION = '4.4.0'.freeze
2
+ VERSION = '4.4.1'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_semantic_logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.4.0
4
+ version: 4.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Reid Morrison
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-02-07 00:00:00.000000000 Z
11
+ date: 2019-04-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails