rails_semantic_logger 4.16.0 → 4.18.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7da18e2122c13f2b4325a6f3000dd74d22517608b3a32355ca3d2bf4e5f08789
4
- data.tar.gz: b0503c3227cb0bfa5faa1c6f00b408968783c8d09092a960497ab9f004798875
3
+ metadata.gz: db5e6b03ccd16cbe212ef23f0fad1e7f14138f8b2e22e7572613aa3e52d5b66d
4
+ data.tar.gz: d46ee20a271e77d51f551926629257d221db96301facefe7be9a641d9ddffc77
5
5
  SHA512:
6
- metadata.gz: 30446e6a7553868e37cb56550d0a3e5c15b137991609e2cbb7aee84c7c7f5a75abe0bdd7f5e6263418546645f7a0892472e042ee09e0764be9f0ff9d90311917
7
- data.tar.gz: 051ff94e24b1d9a4231f01d4148a966333b8d7b57539f7fa15ab953ad0a2fc9c998b61413ae2350716763f323eafa6d7302893b0c7b691e165c2200f7a8f3ef1
6
+ metadata.gz: f07be5e786b9f93669b278027cf2ba578132dc90ec3e6aa575c0df7b85fdd851d9d54965172a59bf7e9b40ad002d4fd0bd1a677ee8c3b77de129010c2b93b781
7
+ data.tar.gz: ebcc3799d581accf18af62739310ed25d183346c72a5006fa32a5a82f828a7371a9607df9646664574fd164be524ab76fa0c23f3845632285d76457134ed90f7
data/README.md CHANGED
@@ -87,7 +87,7 @@ The queue latency is the time between when the job was enqueued and when it was
87
87
  filter environment = "production"
88
88
  filter level = "info"
89
89
  filter metric = "sidekiq.queue.latency"
90
- timechart duration:avg(duration), group_by(name)
90
+ timechart latency:avg(metric_amount/1000), group_by(string(named_tags.queue))
91
91
  ~~~
92
92
 
93
93
  * http://github.com/reidmorrison/rails_semantic_logger
@@ -100,8 +100,12 @@ For complete documentation see: https://logger.rocketjob.io/rails
100
100
 
101
101
  Rails Semantic Logger now supports Sidekiq metrics.
102
102
  Below are the metrics that are now available when the JSON logging format is used:
103
- - `sidekiq.job.perform` - The duration of each Sidekiq job.
104
- - `sidekiq.queue.latency` - The time between when a Sidekiq job was enqueued and when it was started.
103
+ - `sidekiq.job.perform`
104
+ - The duration of each Sidekiq job.
105
+ - `duration` contains the time in milliseconds that the job took to run.
106
+ - `sidekiq.queue.latency`
107
+ - The time between when a Sidekiq job was enqueued and when it was started.
108
+ - `metric_amount` contains the time in milliseconds that the job was waiting in the queue.
105
109
 
106
110
  ## Upgrading to Semantic Logger v4.15 & V4.16 - Sidekiq Support
107
111
 
@@ -3,9 +3,13 @@ module RailsSemanticLogger
3
3
  class LogSubscriber < ActiveSupport::LogSubscriber
4
4
  INTERNAL_PARAMS = %w[controller action format _method only_path].freeze
5
5
 
6
+ class << self
7
+ attr_accessor :action_message_format
8
+ end
9
+
6
10
  # Log as debug to hide Processing messages in production
7
11
  def start_processing(event)
8
- controller_logger(event).debug { "Processing ##{event.payload[:action]}" }
12
+ controller_logger(event).debug { action_message("Processing", event.payload) }
9
13
  end
10
14
 
11
15
  def process_action(event)
@@ -59,7 +63,7 @@ module RailsSemanticLogger
59
63
  payload.delete(:response)
60
64
 
61
65
  {
62
- message: "Completed ##{payload[:action]}",
66
+ message: action_message("Completed", event.payload),
63
67
  duration: event.duration,
64
68
  payload: payload
65
69
  }
@@ -122,6 +126,14 @@ module RailsSemanticLogger
122
126
  index = path.index("?")
123
127
  index ? path[0, index] : path
124
128
  end
129
+
130
+ def action_message(message, payload)
131
+ if self.class.action_message_format
132
+ self.class.action_message_format.call(message, payload)
133
+ else
134
+ "#{message} ##{payload[:action]}"
135
+ end
136
+ end
125
137
  end
126
138
  end
127
139
  end
@@ -33,6 +33,7 @@ module RailsSemanticLogger
33
33
  log_payload[:binds] = bind_values(payload) unless (payload[:binds] || []).empty?
34
34
  log_payload[:allocations] = event.allocations if event.respond_to?(:allocations)
35
35
  log_payload[:cached] = event.payload[:cached]
36
+ log_payload[:async] = true if event.payload[:async]
36
37
 
37
38
  log = {
38
39
  message: name,
@@ -54,11 +55,25 @@ module RailsSemanticLogger
54
55
 
55
56
  # When multiple values are received for a single bound field, it is converted into an array
56
57
  def add_bind_value(binds, key, value)
57
- key = key.downcase.to_sym unless key.nil?
58
- value = (Array(binds[key]) << value) if binds.key?(key)
58
+ key = key.downcase.to_sym unless key.nil?
59
+
60
+ if rails_filter_params_include?(key)
61
+ value = "[FILTERED]"
62
+ elsif binds.key?(key)
63
+ value = (Array(binds[key]) << value)
64
+ end
65
+
59
66
  binds[key] = value
60
67
  end
61
68
 
69
+ def rails_filter_params_include?(key)
70
+ filter_parameters = Rails.configuration.filter_parameters
71
+
72
+ return filter_parameters.first.match? key if filter_parameters.first.is_a? Regexp
73
+
74
+ filter_parameters.include? key
75
+ end
76
+
62
77
  def logger
63
78
  self.class.logger
64
79
  end
@@ -196,8 +211,8 @@ module RailsSemanticLogger
196
211
  alias bind_values bind_values_v5_0_3
197
212
  alias render_bind render_bind_v5_0_3
198
213
  alias type_casted_binds type_casted_binds_v5_0_3
199
- elsif (Rails::VERSION::MAJOR == 6 && Rails::VERSION::MINOR > 0) || # ~> 6.1.0
200
- Rails::VERSION::MAJOR == 7
214
+ elsif (Rails::VERSION::MAJOR == 6 && Rails::VERSION::MINOR > 0) ||
215
+ Rails::VERSION::MAJOR >= 7 # ~> 6.1.0 && >= 7.x.x
201
216
  alias bind_values bind_values_v6_1
202
217
  alias render_bind render_bind_v6_1
203
218
  alias type_casted_binds type_casted_binds_v5_1_5
@@ -108,12 +108,31 @@ module RailsSemanticLogger
108
108
  Resque.logger = SemanticLogger[Resque] if defined?(Resque) && Resque.respond_to?(:logger=)
109
109
 
110
110
  # Replace the Sidekiq logger
111
- if defined?(Sidekiq)
112
- if Sidekiq.respond_to?(:logger=)
113
- Sidekiq.logger = SemanticLogger[Sidekiq]
114
- elsif Sidekiq::VERSION[0..1] == "7."
115
- method = Sidekiq.server? ? :configure_server : :configure_client
116
- Sidekiq.public_send(method) { |cfg| cfg.logger = SemanticLogger[Sidekiq] }
111
+ if defined?(::Sidekiq)
112
+ ::Sidekiq.configure_client do |config|
113
+ config.logger = ::SemanticLogger[::Sidekiq]
114
+ end
115
+
116
+ ::Sidekiq.configure_server do |config|
117
+ config.logger = ::SemanticLogger[::Sidekiq]
118
+ if config.respond_to?(:options)
119
+ config.options[:job_logger] = RailsSemanticLogger::Sidekiq::JobLogger
120
+ else
121
+ config[:job_logger] = RailsSemanticLogger::Sidekiq::JobLogger
122
+ end
123
+
124
+ # Add back the default console logger unless already added
125
+ SemanticLogger.add_appender(io: $stdout, formatter: :color) unless SemanticLogger.appenders.console_output?
126
+
127
+ # Replace default error handler when present
128
+ existing = RailsSemanticLogger::Sidekiq::Defaults.delete_default_error_handler(config.error_handlers)
129
+ config.error_handlers << RailsSemanticLogger::Sidekiq::Defaults::ERROR_HANDLER if existing
130
+ end
131
+
132
+ if defined?(::Sidekiq::Job) && (::Sidekiq::VERSION.to_i != 5)
133
+ ::Sidekiq::Job.singleton_class.prepend(RailsSemanticLogger::Sidekiq::Loggable)
134
+ else
135
+ ::Sidekiq::Worker.singleton_class.prepend(RailsSemanticLogger::Sidekiq::Loggable)
117
136
  end
118
137
  end
119
138
 
@@ -154,7 +173,7 @@ module RailsSemanticLogger
154
173
 
155
174
  if config.rails_semantic_logger.semantic
156
175
  # Active Job
157
- if defined?(::ActiveJob) && defined?(::ActiveJob::Logging::LogSubscriber)
176
+ if defined?(::ActiveJob::Logging::LogSubscriber)
158
177
  RailsSemanticLogger.swap_subscriber(
159
178
  ::ActiveJob::Logging::LogSubscriber,
160
179
  RailsSemanticLogger::ActiveJob::LogSubscriber,
@@ -162,7 +181,7 @@ module RailsSemanticLogger
162
181
  )
163
182
  end
164
183
 
165
- if defined?(::ActiveJob) && defined?(::ActiveJob::LogSubscriber)
184
+ if defined?(::ActiveJob::LogSubscriber)
166
185
  RailsSemanticLogger.swap_subscriber(
167
186
  ::ActiveJob::LogSubscriber,
168
187
  RailsSemanticLogger::ActiveJob::LogSubscriber,
@@ -207,6 +226,7 @@ module RailsSemanticLogger
207
226
  if defined?(::ActionController)
208
227
  require "action_controller/log_subscriber"
209
228
 
229
+ RailsSemanticLogger::ActionController::LogSubscriber.action_message_format = config.rails_semantic_logger.action_message_format
210
230
  RailsSemanticLogger.swap_subscriber(
211
231
  ::ActionController::LogSubscriber,
212
232
  RailsSemanticLogger::ActionController::LogSubscriber,
@@ -246,6 +266,12 @@ module RailsSemanticLogger
246
266
  # Re-open appenders after Spring has forked a process
247
267
  Spring.after_fork { |_job| ::SemanticLogger.reopen } if defined?(Spring.after_fork)
248
268
 
269
+ # Re-open appenders after SolidQueue worker/dispatcher/scheduler has finished booting
270
+ SolidQueue.on_start { ::SemanticLogger.reopen } if defined?(SolidQueue.on_start)
271
+ SolidQueue.on_worker_start { ::SemanticLogger.reopen } if defined?(SolidQueue.on_worker_start)
272
+ SolidQueue.on_dispatcher_start { ::SemanticLogger.reopen } if defined?(SolidQueue.on_dispatcher_start)
273
+ SolidQueue.on_scheduler_start { ::SemanticLogger.reopen } if defined?(SolidQueue.on_scheduler_start)
274
+
249
275
  console do |_app|
250
276
  # Don't use a background thread for logging
251
277
  SemanticLogger.sync!
@@ -7,15 +7,19 @@ module ActionDispatch
7
7
 
8
8
  undef_method :log_error
9
9
  if (Rails::VERSION::MAJOR == 7 && Rails::VERSION::MINOR >= 1) || Rails::VERSION::MAJOR > 7
10
- def log_error(_request, wrapper)
10
+ def log_error(request, wrapper)
11
11
  Rails.application.deprecators.silence do
12
- ActionController::Base.logger.fatal(wrapper.exception)
12
+ return if !log_rescued_responses?(request) && wrapper.rescue_response?
13
+
14
+ level = request.get_header("action_dispatch.debug_exception_log_level")
15
+ ActionController::Base.logger.log(level, wrapper.exception)
13
16
  end
14
17
  end
15
18
  else
16
19
  def log_error(_request, wrapper)
17
20
  ActiveSupport::Deprecation.silence do
18
- ActionController::Base.logger.fatal(wrapper.exception)
21
+ level = wrapper.respond_to?(:rescue_response?) && wrapper.rescue_response? ? :debug : :fatal
22
+ ActionController::Base.logger.log(level, wrapper.exception)
19
23
  end
20
24
  end
21
25
  end
@@ -1,89 +1,14 @@
1
1
  # Sidekiq patches
2
- #
3
- # To re-enable stdout logging for sidekiq server processes, add the following snippet to config/initializers/sidekiq.rb:
4
- # Sidekiq.configure_server do |config|
5
- # SemanticLogger.add_appender(io: $stdout, level: :debug, formatter: :color)
6
- # end
7
2
  if Sidekiq::VERSION.to_i == 4
8
- require "sidekiq/exception_handler"
9
3
  require "sidekiq/logging"
10
4
  require "sidekiq/middleware/server/logging"
11
5
  require "sidekiq/processor"
12
- require "sidekiq/worker"
13
6
  elsif Sidekiq::VERSION.to_i == 5
14
- require "sidekiq/exception_handler"
15
- require "sidekiq/job_logger"
16
7
  require "sidekiq/logging"
17
- require "sidekiq/worker"
18
- elsif Sidekiq::VERSION.to_i == 6 && Sidekiq::VERSION.to_f < 6.5
19
- require "sidekiq/exception_handler"
20
- require "sidekiq/job_logger"
21
- require "sidekiq/worker"
22
- elsif Sidekiq::VERSION.to_i == 6
23
- require "sidekiq/job_logger"
24
- require "sidekiq/worker"
25
- else
26
- require "sidekiq/config"
27
- require "sidekiq/job_logger"
28
- require "sidekiq/job"
29
8
  end
30
9
 
31
10
  module Sidekiq
32
- # Sidekiq > v4
33
- if defined?(::Sidekiq::JobLogger)
34
- # Let Semantic Logger handle duration logging
35
- class JobLogger
36
- def call(item, queue, &block)
37
- klass = item["wrapped"] || item["class"]
38
- logger = klass ? SemanticLogger[klass] : Sidekiq.logger
39
-
40
- SemanticLogger.tagged(queue: queue) do
41
- # Latency is the time between when the job was enqueued and when it started executing.
42
- logger.info(
43
- "Start #perform",
44
- metric: "sidekiq.queue.latency",
45
- metric_amount: job_latency_ms(item)
46
- )
47
-
48
- # Measure the duration of running the job
49
- logger.measure_info(
50
- "Completed #perform",
51
- on_exception_level: :error,
52
- log_exception: :full,
53
- metric: "sidekiq.job.perform",
54
- &block
55
- )
56
- end
57
- end
58
-
59
- def prepare(job_hash, &block)
60
- level = job_hash["log_level"]
61
- if level
62
- SemanticLogger.silence(level) do
63
- SemanticLogger.tagged(job_hash_context(job_hash), &block)
64
- end
65
- else
66
- SemanticLogger.tagged(job_hash_context(job_hash), &block)
67
- end
68
- end
69
-
70
- def job_hash_context(job_hash)
71
- h = {jid: job_hash["jid"]}
72
- h[:bid] = job_hash["bid"] if job_hash["bid"]
73
- h[:tags] = job_hash["tags"] if job_hash["tags"]
74
- h[:queue] = job_hash["queue"] if job_hash["queue"]
75
- h
76
- end
77
-
78
- def job_latency_ms(job)
79
- return unless job && job["enqueued_at"]
80
-
81
- (Time.now.to_f - job["enqueued_at"].to_f) * 1000
82
- end
83
- end
84
- end
85
-
86
- # Sidekiq <= v6
11
+ # Sidekiq v4 & v5
87
12
  if defined?(::Sidekiq::Logging)
88
13
  # Replace Sidekiq logging context
89
14
  module Logging
@@ -100,106 +25,8 @@ module Sidekiq
100
25
  end
101
26
  end
102
27
 
103
- # Exception is already logged by Semantic Logger during the perform call
104
- if defined?(::Sidekiq::ExceptionHandler)
105
- # Sidekiq <= v6.5
106
- module ExceptionHandler
107
- class Logger
108
- def call(_exception, ctx)
109
- return if ctx.empty?
110
-
111
- job_hash = ctx[:job] || {}
112
- klass = job_hash["display_class"] || job_hash["wrapped"] || job_hash["class"]
113
- logger = klass ? SemanticLogger[klass] : Sidekiq.logger
114
- ctx[:context] ? logger.warn(ctx[:context], ctx) : logger.warn(ctx)
115
- end
116
- end
117
- end
118
- elsif defined?(::Sidekiq::Config)
119
- # Sidekiq >= v7
120
- class Config
121
- remove_const :ERROR_HANDLER
122
-
123
- ERROR_HANDLER = ->(ex, ctx, cfg = Sidekiq.default_configuration) do
124
- unless ctx.empty?
125
- job_hash = ctx[:job] || {}
126
- klass = job_hash["display_class"] || job_hash["wrapped"] || job_hash["class"]
127
- logger = klass ? SemanticLogger[klass] : Sidekiq.logger
128
- ctx[:context] ? logger.warn(ctx[:context], ctx) : logger.warn(ctx)
129
- end
130
- end
131
- end
132
- else
133
- # Sidekiq >= 6.5
134
- Sidekiq.error_handlers.delete(Sidekiq::DEFAULT_ERROR_HANDLER)
135
- Sidekiq.error_handlers << ->(ex, ctx) do
136
- unless ctx.empty?
137
- job_hash = ctx[:job] || {}
138
- klass = job_hash["display_class"] || job_hash["wrapped"] || job_hash["class"]
139
- logger = klass ? SemanticLogger[klass] : Sidekiq.logger
140
- ctx[:context] ? logger.warn(ctx[:context], ctx) : logger.warn(ctx)
141
- end
142
- end
143
- end
144
-
145
- # Logging within each worker should use its own logger
146
- case Sidekiq::VERSION.to_i
147
- when 4
148
- module Worker
149
- def self.included(base)
150
- if base.ancestors.any? { |c| c.name == "ActiveJob::Base" }
151
- raise ArgumentError, "You cannot include Sidekiq::Worker in an ActiveJob: #{base.name}"
152
- end
153
-
154
- base.extend(ClassMethods)
155
- base.include(SemanticLogger::Loggable)
156
- base.class_attribute :sidekiq_options_hash
157
- base.class_attribute :sidekiq_retry_in_block
158
- base.class_attribute :sidekiq_retries_exhausted_block
159
- end
160
- end
161
- when 5
162
- module Worker
163
- def self.included(base)
164
- if base.ancestors.any? { |c| c.name == "ActiveJob::Base" }
165
- raise ArgumentError, "You cannot include Sidekiq::Worker in an ActiveJob: #{base.name}"
166
- end
167
-
168
- base.extend(ClassMethods)
169
- base.include(SemanticLogger::Loggable)
170
- base.sidekiq_class_attribute :sidekiq_options_hash
171
- base.sidekiq_class_attribute :sidekiq_retry_in_block
172
- base.sidekiq_class_attribute :sidekiq_retries_exhausted_block
173
- end
174
- end
175
- when 6
176
- module Worker
177
- def self.included(base)
178
- if base.ancestors.any? { |c| c.name == "ActiveJob::Base" }
179
- raise ArgumentError, "Sidekiq::Worker cannot be included in an ActiveJob: #{base.name}"
180
- end
181
-
182
- base.include(Options)
183
- base.extend(ClassMethods)
184
- base.include(SemanticLogger::Loggable)
185
- end
186
- end
187
- else
188
- module Job
189
- def self.included(base)
190
- if base.ancestors.any? { |c| c.name == "ActiveJob::Base" }
191
- raise ArgumentError, "Sidekiq::Job cannot be included in an ActiveJob: #{base.name}"
192
- end
193
-
194
- base.include(Options)
195
- base.extend(ClassMethods)
196
- base.include(SemanticLogger::Loggable)
197
- end
198
- end
199
- end
200
-
28
+ # Sidekiq v4
201
29
  if defined?(::Sidekiq::Middleware::Server::Logging)
202
- # Sidekiq v4
203
30
  # Convert string to machine readable format
204
31
  class Processor
205
32
  def log_context(job_hash)
@@ -214,6 +41,7 @@ module Sidekiq
214
41
  module Middleware
215
42
  module Server
216
43
  class Logging
44
+ # rubocop:disable Style/ExplicitBlockArgument
217
45
  def call(worker, item, queue)
218
46
  SemanticLogger.tagged(queue: queue) do
219
47
  worker.logger.info(
@@ -22,14 +22,14 @@ module RailsSemanticLogger
22
22
  #
23
23
  # config.rails_semantic_logger.rendered = false
24
24
  #
25
- # * Override the Awesome Print options for logging Hash data as text:
25
+ # * Override the Amazing Print options for logging Hash data as text:
26
26
  #
27
- # Any valid AwesomePrint option for rendering data.
27
+ # Any valid Amazing Print option for rendering data.
28
28
  # The defaults can changed be creating a `~/.aprc` file.
29
- # See: https://github.com/michaeldv/awesome_print
29
+ # See: https://github.com/amazing-print/amazing_print
30
30
  #
31
31
  # Note: The option :multiline is set to false if not supplied.
32
- # Note: Has no effect if Awesome Print is not installed.
32
+ # Note: Has no effect if Amazing Print is not installed.
33
33
  #
34
34
  # config.rails_semantic_logger.ap_options = {multiline: false}
35
35
  #
@@ -100,23 +100,32 @@ module RailsSemanticLogger
100
100
  # * named_tags: *DEPRECATED*
101
101
  # Instead, supply a Hash to config.log_tags
102
102
  # config.rails_semantic_logger.named_tags = nil
103
+ #
104
+ # * Change the message format of Action Controller action.
105
+ # A block that will be called to format the message.
106
+ # It is supplied with the `message` and `payload` and should return the formatted data.
107
+ #
108
+ # config.rails_semantic_logger.action_message_format = -> (message, payload) do
109
+ # "#{message} - #{payload[:controller]}##{payload[:action]}"
110
+ # end
103
111
  class Options
104
112
  attr_accessor :semantic, :started, :processing, :rendered, :ap_options, :add_file_appender,
105
- :quiet_assets, :format, :named_tags, :filter, :console_logger
113
+ :quiet_assets, :format, :named_tags, :filter, :console_logger, :action_message_format
106
114
 
107
115
  # Setup default values
108
116
  def initialize
109
- @semantic = true
110
- @started = false
111
- @processing = false
112
- @rendered = false
113
- @ap_options = {multiline: false}
114
- @add_file_appender = true
115
- @quiet_assets = false
116
- @format = :default
117
- @named_tags = nil
118
- @filter = nil
119
- @console_logger = true
117
+ @semantic = true
118
+ @started = false
119
+ @processing = false
120
+ @rendered = false
121
+ @ap_options = {multiline: false}
122
+ @add_file_appender = true
123
+ @quiet_assets = false
124
+ @format = :default
125
+ @named_tags = nil
126
+ @filter = nil
127
+ @console_logger = true
128
+ @action_message_format = nil
120
129
  end
121
130
  end
122
131
  end
@@ -36,10 +36,18 @@ module RailsSemanticLogger
36
36
 
37
37
  def call_app(request, env)
38
38
  instrumenter = ActiveSupport::Notifications.instrumenter
39
- instrumenter_state = instrumenter.start "request.action_dispatch", request: request
40
- instrumenter_finish = lambda {
41
- instrumenter.finish_with_state(instrumenter_state, "request.action_dispatch", request: request)
42
- }
39
+ if (Rails::VERSION::MAJOR == 7 && Rails::VERSION::MINOR >= 1) || Rails::VERSION::MAJOR > 7
40
+ handle = instrumenter.build_handle "request.action_dispatch", request: request
41
+ instrumenter_finish = lambda {
42
+ handle.finish
43
+ }
44
+ handle.start
45
+ else
46
+ instrumenter_state = instrumenter.start "request.action_dispatch", request: request
47
+ instrumenter_finish = lambda {
48
+ instrumenter.finish_with_state(instrumenter_state, "request.action_dispatch", request: request)
49
+ }
50
+ end
43
51
 
44
52
  logger.send(self.class.started_request_log_level) { started_request_message(request) }
45
53
  status, headers, body = @app.call(env)
@@ -0,0 +1,40 @@
1
+ module RailsSemanticLogger
2
+ module Sidekiq
3
+ module Defaults
4
+ # Prevent exception logging during standard error handling since the Job Logger below already logs the exception.
5
+ ERROR_HANDLER =
6
+ if ::Sidekiq::VERSION.to_f < 7.1 ||
7
+ (::Sidekiq::VERSION.to_f == 7.1 && ::Sidekiq::VERSION.split(".").last.to_i < 6)
8
+ lambda do |_ex, ctx|
9
+ unless ctx.empty?
10
+ job_hash = ctx[:job] || {}
11
+ klass = job_hash["display_class"] || job_hash["wrapped"] || job_hash["class"]
12
+ logger = klass ? SemanticLogger[klass] : ::Sidekiq.logger
13
+ ctx[:context] ? logger.warn(ctx[:context], ctx) : logger.warn(ctx)
14
+ end
15
+ end
16
+ else
17
+ lambda do |_ex, ctx, _default_configuration|
18
+ unless ctx.empty?
19
+ job_hash = ctx[:job] || {}
20
+ klass = job_hash["display_class"] || job_hash["wrapped"] || job_hash["class"]
21
+ logger = klass ? SemanticLogger[klass] : ::Sidekiq.logger
22
+ ctx[:context] ? logger.warn(ctx[:context], ctx) : logger.warn(ctx)
23
+ end
24
+ end
25
+ end
26
+
27
+ # Returns the default logger after removing from the supplied list.
28
+ # Returns [nil] when the default logger was not present.
29
+ def self.delete_default_error_handler(error_handlers)
30
+ return error_handlers.delete(::Sidekiq::Config::ERROR_HANDLER) if defined?(::Sidekiq::Config::ERROR_HANDLER)
31
+ return error_handlers.delete(::Sidekiq::DEFAULT_ERROR_HANDLER) if defined?(::Sidekiq::DEFAULT_ERROR_HANDLER)
32
+
33
+ return unless defined?(::Sidekiq::ExceptionHandler)
34
+
35
+ existing = error_handlers.find { |handler| handler.is_a?(::Sidekiq::ExceptionHandler::Logger) }
36
+ error_handlers.delete(existing) if existing
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,59 @@
1
+ module RailsSemanticLogger
2
+ module Sidekiq
3
+ class JobLogger
4
+ # Sidekiq 6.5 does not take any arguments, whereas v7 is given a logger
5
+ def initialize(*_args)
6
+ end
7
+
8
+ def call(item, queue, &block)
9
+ klass = item["wrapped"] || item["class"]
10
+ logger = klass ? SemanticLogger[klass] : Sidekiq.logger
11
+
12
+ SemanticLogger.tagged(queue: queue) do
13
+ # Latency is the time between when the job was enqueued and when it started executing.
14
+ logger.info(
15
+ "Start #perform",
16
+ metric: "sidekiq.queue.latency",
17
+ metric_amount: job_latency_ms(item)
18
+ )
19
+
20
+ # Measure the duration of running the job
21
+ logger.measure_info(
22
+ "Completed #perform",
23
+ on_exception_level: :error,
24
+ log_exception: :full,
25
+ metric: "sidekiq.job.perform",
26
+ &block
27
+ )
28
+ end
29
+ end
30
+
31
+ def prepare(job_hash, &block)
32
+ level = job_hash["log_level"]
33
+ if level
34
+ SemanticLogger.silence(level) do
35
+ SemanticLogger.tagged(job_hash_context(job_hash), &block)
36
+ end
37
+ else
38
+ SemanticLogger.tagged(job_hash_context(job_hash), &block)
39
+ end
40
+ end
41
+
42
+ private
43
+
44
+ def job_hash_context(job_hash)
45
+ h = {jid: job_hash["jid"]}
46
+ h[:bid] = job_hash["bid"] if job_hash["bid"]
47
+ h[:tags] = job_hash["tags"] if job_hash["tags"]
48
+ h[:queue] = job_hash["queue"] if job_hash["queue"]
49
+ h
50
+ end
51
+
52
+ def job_latency_ms(job)
53
+ return unless job && job["enqueued_at"]
54
+
55
+ (Time.now.to_f - job["enqueued_at"].to_f) * 1000
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,10 @@
1
+ module RailsSemanticLogger
2
+ module Sidekiq
3
+ module Loggable
4
+ def included(base)
5
+ super
6
+ base.include(SemanticLogger::Loggable)
7
+ end
8
+ end
9
+ end
10
+ end
@@ -1,3 +1,3 @@
1
1
  module RailsSemanticLogger
2
- VERSION = "4.16.0".freeze
2
+ VERSION = "4.18.0".freeze
3
3
  end
@@ -31,6 +31,12 @@ module RailsSemanticLogger
31
31
  autoload :Plugin, "rails_semantic_logger/delayed_job/plugin"
32
32
  end
33
33
 
34
+ module Sidekiq
35
+ autoload :Defaults, "rails_semantic_logger/sidekiq/defaults"
36
+ autoload :JobLogger, "rails_semantic_logger/sidekiq/job_logger"
37
+ autoload :Loggable, "rails_semantic_logger/sidekiq/loggable"
38
+ end
39
+
34
40
  autoload :Options, "rails_semantic_logger/options"
35
41
 
36
42
  # Swap an existing subscriber with a new one
@@ -43,7 +49,7 @@ module RailsSemanticLogger
43
49
 
44
50
  def self.unattach(subscriber)
45
51
  subscriber_patterns(subscriber).each do |pattern|
46
- ActiveSupport::Notifications.notifier.listeners_for(pattern).each do |sub|
52
+ listeners_for(ActiveSupport::Notifications.notifier, pattern).each do |sub|
47
53
  next unless sub.instance_variable_get(:@delegate) == subscriber
48
54
 
49
55
  ActiveSupport::Notifications.unsubscribe(sub)
@@ -61,7 +67,15 @@ module RailsSemanticLogger
61
67
  end
62
68
  end
63
69
 
64
- private_class_method :subscriber_patterns, :unattach
70
+ def self.listeners_for(notifier, pattern)
71
+ if notifier.respond_to?(:all_listeners_for) # Rails >= 7.1
72
+ notifier.all_listeners_for(pattern)
73
+ else
74
+ notifier.listeners_for(pattern)
75
+ end
76
+ end
77
+
78
+ private_class_method :listeners_for, :subscriber_patterns, :unattach
65
79
  end
66
80
 
67
81
  require("rails_semantic_logger/extensions/mongoid/config") if defined?(Mongoid)
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_semantic_logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.16.0
4
+ version: 4.18.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Reid Morrison
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2024-07-01 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: rack
@@ -44,16 +43,14 @@ dependencies:
44
43
  requirements:
45
44
  - - "~>"
46
45
  - !ruby/object:Gem::Version
47
- version: '4.13'
46
+ version: '4.16'
48
47
  type: :runtime
49
48
  prerelease: false
50
49
  version_requirements: !ruby/object:Gem::Requirement
51
50
  requirements:
52
51
  - - "~>"
53
52
  - !ruby/object:Gem::Version
54
- version: '4.13'
55
- description:
56
- email:
53
+ version: '4.16'
57
54
  executables: []
58
55
  extensions: []
59
56
  extra_rdoc_files: []
@@ -85,6 +82,9 @@ files:
85
82
  - lib/rails_semantic_logger/extensions/sidekiq/sidekiq.rb
86
83
  - lib/rails_semantic_logger/options.rb
87
84
  - lib/rails_semantic_logger/rack/logger.rb
85
+ - lib/rails_semantic_logger/sidekiq/defaults.rb
86
+ - lib/rails_semantic_logger/sidekiq/job_logger.rb
87
+ - lib/rails_semantic_logger/sidekiq/loggable.rb
88
88
  - lib/rails_semantic_logger/version.rb
89
89
  homepage: https://logger.rocketjob.io
90
90
  licenses:
@@ -92,9 +92,8 @@ licenses:
92
92
  metadata:
93
93
  bug_tracker_uri: https://github.com/reidmorrison/rails_semantic_logger/issues
94
94
  documentation_uri: https://logger.rocketjob.io
95
- source_code_uri: https://github.com/reidmorrison/rails_semantic_logger/tree/v4.16.0
95
+ source_code_uri: https://github.com/reidmorrison/rails_semantic_logger/tree/v4.18.0
96
96
  rubygems_mfa_required: 'true'
97
- post_install_message:
98
97
  rdoc_options: []
99
98
  require_paths:
100
99
  - lib
@@ -109,8 +108,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
109
108
  - !ruby/object:Gem::Version
110
109
  version: '0'
111
110
  requirements: []
112
- rubygems_version: 3.5.3
113
- signing_key:
111
+ rubygems_version: 3.6.9
114
112
  specification_version: 4
115
113
  summary: Feature rich logging framework that replaces the Rails logger.
116
114
  test_files: []