semantic_logger 2.15.0 → 2.16.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -15,7 +15,7 @@ module SemanticLogger
15
15
  # ruby_logger = Logger.new(STDOUT)
16
16
  # SemanticLogger.add_appender(ruby_logger)
17
17
  # logger = SemanticLogger['test']
18
- # logger.info('Hello World', :some => :payload)
18
+ # logger.info('Hello World', some: :payload)
19
19
  #
20
20
  # Enhance the Rails Logger
21
21
  # # Add the Rails logger to the list of appenders
@@ -28,8 +28,8 @@ module SemanticLogger
28
28
  # Note: Since the log level is controlled by setting the Ruby or Rails logger directly
29
29
  # the level is ignored for this appender
30
30
  def initialize(logger, filter=nil, &block)
31
- raise "logger cannot be null when initiailizing the SemanticLogging::Appender::Wrapper" unless logger
32
- @logger = logger
31
+ raise 'logger cannot be null when initiailizing the SemanticLogging::Appender::Wrapper' unless logger
32
+ @logger = logger
33
33
 
34
34
  # Set the formatter to the supplied block
35
35
  @formatter = block || self.default_formatter
@@ -21,7 +21,7 @@ module SemanticLogger
21
21
  # nil if this logger instance should use the global default level
22
22
  def level=(level)
23
23
  @level_index = SemanticLogger.level_to_index(level)
24
- @level = level
24
+ @level = level
25
25
  end
26
26
 
27
27
  # Returns the current log level if set, otherwise it returns the global
@@ -68,7 +68,7 @@ module SemanticLogger
68
68
  # logger.debug("Only display this if log level is set to Debug or lower")
69
69
  #
70
70
  # # Log semantic information along with a text message
71
- # logger.info("Request received", :user => "joe", :duration => 100)
71
+ # logger.info("Request received", user: "joe", duration: 100)
72
72
  #
73
73
  # # Log an exception in a semantic way
74
74
  # logger.info("Parsing received XML", exc)
@@ -134,8 +134,8 @@ module SemanticLogger
134
134
  # Returns the list of tags pushed after flattening them out and removing blanks
135
135
  def push_tags *tags
136
136
  # Need to flatten and reject empties to support calls from Rails 4
137
- new_tags = tags.flatten.collect(&:to_s).reject(&:empty?)
138
- t = Thread.current[:semantic_logger_tags]
137
+ new_tags = tags.flatten.collect(&:to_s).reject(&:empty?)
138
+ t = Thread.current[:semantic_logger_tags]
139
139
  Thread.current[:semantic_logger_tags] = t.nil? ? new_tags : t.concat(new_tags)
140
140
  new_tags
141
141
  end
@@ -150,18 +150,18 @@ module SemanticLogger
150
150
  #
151
151
  # Add a payload to all log calls on This Thread within the supplied block
152
152
  #
153
- # logger.with_payload(:tracking_number=>12345) do
153
+ # logger.with_payload(tracking_number: 12345) do
154
154
  # logger.debug('Hello World')
155
155
  # end
156
156
  #
157
157
  # If a log call already includes a pyload, this payload will be merged with
158
158
  # the supplied payload, with the supplied payload taking precedence
159
159
  #
160
- # logger.with_payload(:tracking_number=>12345) do
161
- # logger.debug('Hello World', :result => 'blah')
160
+ # logger.with_payload(tracking_number: 12345) do
161
+ # logger.debug('Hello World', result: 'blah')
162
162
  # end
163
163
  def with_payload(payload)
164
- current_payload = self.payload
164
+ current_payload = self.payload
165
165
  Thread.current[:semantic_logger_payload] = current_payload ? current_payload.merge(payload) : payload
166
166
  yield
167
167
  ensure
@@ -218,7 +218,7 @@ module SemanticLogger
218
218
  # #silence does not affect any loggers which have had their log level set
219
219
  # explicitly. I.e. That do not rely on the global default level
220
220
  def silence(new_level = :error)
221
- current_index = Thread.current[:semantic_logger_silence]
221
+ current_index = Thread.current[:semantic_logger_silence]
222
222
  Thread.current[:semantic_logger_silence] = SemanticLogger.level_to_index(new_level)
223
223
  yield
224
224
  ensure
@@ -227,13 +227,13 @@ module SemanticLogger
227
227
 
228
228
  # DEPRECATED See SemanticLogger.default_level=
229
229
  def self.default_level=(level)
230
- warn "[DEPRECATION] `SemanticLogger::Logger.default_level=` is deprecated. Please use `SemanticLogger.default_level=` instead."
230
+ warn '[DEPRECATION] SemanticLogger::Logger.default_level= is deprecated. Please use SemanticLogger.default_level= instead.'
231
231
  SemanticLogger.default_level = level
232
232
  end
233
233
 
234
234
  # DEPRECATED See SemanticLogger.default_level
235
235
  def self.default_level
236
- warn "[DEPRECATION] `SemanticLogger::Logger.default_level` is deprecated. Please use `SemanticLogger.default_level` instead."
236
+ warn '[DEPRECATION] SemanticLogger::Logger.default_level is deprecated. Please use SemanticLogger.default_level instead.'
237
237
  SemanticLogger.default_level
238
238
  end
239
239
 
@@ -260,7 +260,7 @@ module SemanticLogger
260
260
  def initialize(klass, level=nil, filter=nil)
261
261
  # Support filtering all messages to this logger using a Regular Expression
262
262
  # or Proc
263
- raise ":filter must be a Regexp or Proc" unless filter.nil? || filter.is_a?(Regexp) || filter.is_a?(Proc)
263
+ raise ':filter must be a Regexp or Proc' unless filter.nil? || filter.is_a?(Regexp) || filter.is_a?(Proc)
264
264
 
265
265
  @filter = filter.is_a?(Regexp) ? filter.freeze : filter
266
266
  @name = klass.is_a?(String) ? klass : klass.name
@@ -269,7 +269,7 @@ module SemanticLogger
269
269
 
270
270
  # Write log data to underlying data storage
271
271
  def log(log_)
272
- raise NotImplementedError.new("Logging Appender must implement #log(log)")
272
+ raise NotImplementedError.new('Logging Appender must implement #log(log)')
273
273
  end
274
274
 
275
275
  # Return the level index for fast comparisons
@@ -334,7 +334,7 @@ module SemanticLogger
334
334
  message = exception.inspect
335
335
  elsif exception.nil? && payload && payload.is_a?(Exception)
336
336
  exception = payload
337
- payload = nil
337
+ payload = nil
338
338
  end
339
339
 
340
340
  if block && (result = block.call)
@@ -357,34 +357,36 @@ module SemanticLogger
357
357
 
358
358
  # Measure the supplied block and log the message
359
359
  def benchmark_internal(level, index, message, params, &block)
360
- start = Time.now
360
+ start = Time.now
361
361
  begin
362
362
  if block
363
- result = if silence_level = params[:silence]
364
- # In case someone accidentally sets `silence: true` instead of `silence: :error`
365
- silence_level = :error if silence_level == true
366
- silence(silence_level) { block.call(params) }
367
- else
368
- block.call(params)
369
- end
363
+ result =
364
+ if silence_level = params[:silence]
365
+ # In case someone accidentally sets `silence: true` instead of `silence: :error`
366
+ silence_level = :error if silence_level == true
367
+ silence(silence_level) { block.call(params) }
368
+ else
369
+ block.call(params)
370
+ end
370
371
  exception = params[:exception]
371
372
  result
372
373
  end
373
374
  rescue Exception => exc
374
375
  exception = exc
375
376
  ensure
376
- end_time = Time.now
377
+ end_time = Time.now
377
378
  # Extract options after block completes so that block can modify any of the options
378
379
  log_exception = params[:log_exception] || :partial
379
380
  on_exception_level = params[:on_exception_level]
380
381
  min_duration = params[:min_duration] || 0.0
381
382
  payload = params[:payload]
382
383
  metric = params[:metric]
383
- duration = if block_given?
384
- 1000.0 * (end_time - start)
385
- else
386
- params[:duration] || raise("Mandatory block missing when :duration option is not supplied")
387
- end
384
+ duration =
385
+ if block_given?
386
+ 1000.0 * (end_time - start)
387
+ else
388
+ params[:duration] || raise('Mandatory block missing when :duration option is not supplied')
389
+ end
388
390
 
389
391
  # Add scoped payload
390
392
  if self.payload
@@ -405,7 +407,7 @@ module SemanticLogger
405
407
  level = on_exception_level
406
408
  index = SemanticLogger.level_to_index(level)
407
409
  end
408
- message = "#{message} -- Exception: #{exception.class}: #{exception.message}"
410
+ message = "#{message} -- Exception: #{exception.class}: #{exception.message}"
409
411
  logged_exception = nil
410
412
  else
411
413
  # Log the message with its duration but leave out the exception that was raised
@@ -14,8 +14,8 @@ module SemanticLogger
14
14
  # Only care about GARBAGE_COLLECTION_NOTIFICATION notifications
15
15
  return unless notification.get_type == Java::ComSunManagement::GarbageCollectionNotificationInfo::GARBAGE_COLLECTION_NOTIFICATION
16
16
 
17
- info = Java::ComSunManagement::GarbageCollectionNotificationInfo.from(notification.user_data)
18
- gc_info = info.gc_info
17
+ info = Java::ComSunManagement::GarbageCollectionNotificationInfo.from(notification.user_data)
18
+ gc_info = info.gc_info
19
19
  duration = gc_info.duration
20
20
  if duration >= @min_microseconds
21
21
  SemanticLogger['GarbageCollector'].benchmark_warn "Garbage Collection completed: #{info.gc_name} ##{gc_info.id}", duration: duration.to_f / 1000
@@ -18,7 +18,7 @@ require 'sync_attr'
18
18
  # include SemanticLogger::Loggable
19
19
  #
20
20
  # def call_supplier(amount, name)
21
- # logger.debug "Calculating with amount", { :amount => amount, :name => name }
21
+ # logger.debug "Calculating with amount", { amount: amount, name: name }
22
22
  #
23
23
  # # Measure and log on completion how long the call took to the external supplier
24
24
  # logger.benchmark_info "Calling external interface" do
@@ -94,4 +94,4 @@ module SemanticLogger
94
94
  end
95
95
 
96
96
  end
97
- end
97
+ end
@@ -60,12 +60,12 @@ module SemanticLogger
60
60
 
61
61
  logger.debug "Flushing appenders with #{queue_size} log messages on the queue"
62
62
  reply_queue = Queue.new
63
- queue << { :command => :flush, :reply_queue => reply_queue }
63
+ queue << {command: :flush, reply_queue: reply_queue}
64
64
  reply_queue.pop
65
65
  end
66
66
 
67
67
  @@lag_check_interval = 5000
68
- @@lag_threshold_s = 30
68
+ @@lag_threshold_s = 30
69
69
 
70
70
  # Returns the check_interval which is the number of messages between checks
71
71
  # to determine if the appender thread is falling behind
@@ -99,13 +99,13 @@ module SemanticLogger
99
99
 
100
100
  # DEPRECATED See SemanticLogger.add_appender
101
101
  def self.appenders
102
- warn "[DEPRECATION] `SemanticLogger::Logger.appenders` is deprecated. Please use `SemanticLogger.add_appender` instead."
102
+ warn '[DEPRECATION] SemanticLogger::Logger.appenders is deprecated. Please use SemanticLogger.add_appender instead.'
103
103
  SemanticLogger.appenders
104
104
  end
105
105
 
106
106
  # DEPRECATED: Please use queue_size instead.
107
107
  def self.cache_count
108
- warn "[DEPRECATION] 'SemanticLogger::Logger.cache_count' is deprecated. Please use 'SemanticLogger::Logger.queue_size' instead."
108
+ warn '[DEPRECATION] SemanticLogger::Logger.cache_count is deprecated. Please use SemanticLogger::Logger.queue_size instead.'
109
109
  queue_size
110
110
  end
111
111
 
@@ -120,7 +120,7 @@ module SemanticLogger
120
120
  # puts "#{log_struct.metric} was received. Log Struct: #{log_struct.inspect}"
121
121
  # end
122
122
  def self.on_metric(&block)
123
- (@@metric_subscribers ||= ThreadSafe::Array.new) << block
123
+ (@@metric_subscribers ||= ThreadSafe::Array.new) << block
124
124
  end
125
125
 
126
126
  ############################################################################
@@ -146,7 +146,7 @@ module SemanticLogger
146
146
  # By default logs to STDERR
147
147
  def self.logger
148
148
  @@logger ||= begin
149
- l = SemanticLogger::Appender::File.new(STDERR, :warn)
149
+ l = SemanticLogger::Appender::File.new(STDERR, :warn)
150
150
  l.name = name
151
151
  l
152
152
  end
@@ -156,7 +156,7 @@ module SemanticLogger
156
156
  def self.start_appender_thread
157
157
  return false if appender_thread_active?
158
158
  @@appender_thread = Thread.new { appender_thread }
159
- raise "Failed to start Appender Thread" unless @@appender_thread
159
+ raise 'Failed to start Appender Thread' unless @@appender_thread
160
160
  true
161
161
  end
162
162
 
@@ -173,7 +173,7 @@ module SemanticLogger
173
173
  #
174
174
  # Should any appender fail to log or flush, the exception is logged and
175
175
  # other appenders will still be called
176
- Thread.current.name = "SemanticLogger::AppenderThread"
176
+ Thread.current.name = 'SemanticLogger::AppenderThread'
177
177
  logger.debug "V#{VERSION} Appender thread active"
178
178
  begin
179
179
  count = 0
@@ -208,7 +208,7 @@ module SemanticLogger
208
208
  end
209
209
 
210
210
  message[:reply_queue] << true if message[:reply_queue]
211
- logger.debug "Appender thread: All appenders flushed"
211
+ logger.debug 'Appender thread: All appenders flushed'
212
212
  else
213
213
  logger.warn "Appender thread: Ignoring unknown command: #{message[:command]}"
214
214
  end
@@ -217,7 +217,7 @@ module SemanticLogger
217
217
  rescue Exception => exception
218
218
  # This block may be called after the file handles have been released by Ruby
219
219
  begin
220
- logger.error "Appender thread restarting due to exception", exception
220
+ logger.error 'Appender thread restarting due to exception', exception
221
221
  rescue Exception
222
222
  nil
223
223
  end
@@ -226,7 +226,7 @@ module SemanticLogger
226
226
  @@appender_thread = nil
227
227
  # This block may be called after the file handles have been released by Ruby
228
228
  begin
229
- logger.debug "Appender thread has stopped"
229
+ logger.debug 'Appender thread has stopped'
230
230
  rescue Exception
231
231
  nil
232
232
  end
@@ -242,7 +242,7 @@ module SemanticLogger
242
242
  begin
243
243
  subscriber.call(log_struct)
244
244
  rescue Exception => exc
245
- logger.error "Exception calling subscriber", exc
245
+ logger.error 'Exception calling subscriber', exc
246
246
  end
247
247
  end
248
248
  end
@@ -8,16 +8,14 @@ module SemanticLogger
8
8
  SemanticLogger::Logger.new(klass)
9
9
  end
10
10
 
11
- # Allow for setting the global default log level
12
- # This change only applies to _new_ loggers, existing logger levels
13
- # will not be changed in any way
11
+ # Sets the global default log level
14
12
  def self.default_level=(level)
15
- @@default_level = level
13
+ @@default_level = level
16
14
  # For performance reasons pre-calculate the level index
17
15
  @@default_level_index = level_to_index(level)
18
16
  end
19
17
 
20
- # Returns the global default log level for new Logger instances
18
+ # Returns the global default log level
21
19
  def self.default_level
22
20
  @@default_level
23
21
  end
@@ -80,26 +78,27 @@ module SemanticLogger
80
78
  #
81
79
  # logger = SemanticLogger['Example']
82
80
  # logger.info "Hello World"
83
- # logger.debug("Login time", :user => 'Joe', :duration => 100, :ip_address=>'127.0.0.1')
81
+ # logger.debug("Login time", user: 'Joe', duration: 100, ip_address: '127.0.0.1')
84
82
  #
85
83
  def self.add_appender(appender, level=nil, &block)
86
- appender_instance = if appender.is_a?(String) || appender.is_a?(IO)
87
- # $stderr, STDOUT, other IO, or a filename
88
- SemanticLogger::Appender::File.new(appender, level, &block)
89
- elsif appender.is_a? Appender::Base
90
- # Already an instance of an appender
91
- appender.level = level if level
92
- appender.formatter = block if block
93
- appender
94
- else
95
- # Check if the custom appender responds to all the log levels. For example Ruby ::Logger
96
- if does_not_implement = LEVELS[1..-1].find{|i| !appender.respond_to?(i)}
97
- raise "Supplied appender does not implement:#{does_not_implement}. It must implement all of #{LEVELS[1..-1].inspect}"
98
- end
84
+ appender_instance =
85
+ if appender.is_a?(String) || appender.is_a?(IO)
86
+ # $stderr, STDOUT, other IO, or a filename
87
+ SemanticLogger::Appender::File.new(appender, level, &block)
88
+ elsif appender.is_a? Appender::Base
89
+ # Already an instance of an appender
90
+ appender.level = level if level
91
+ appender.formatter = block if block
92
+ appender
93
+ else
94
+ # Check if the custom appender responds to all the log levels. For example Ruby ::Logger
95
+ if does_not_implement = LEVELS[1..-1].find { |i| !appender.respond_to?(i) }
96
+ raise "Supplied appender does not implement:#{does_not_implement}. It must implement all of #{LEVELS[1..-1].inspect}"
97
+ end
99
98
 
100
- raise "Change the log level to #{level}, update the log level directly against the supplied appender" if level
101
- SemanticLogger::Appender::Wrapper.new(appender, &block)
102
- end
99
+ raise "Change the log level to #{level}, update the log level directly against the supplied appender" if level
100
+ SemanticLogger::Appender::Wrapper.new(appender, &block)
101
+ end
103
102
  @@appenders << appender_instance
104
103
 
105
104
  # Start appender thread if it is not already running
@@ -133,7 +132,7 @@ module SemanticLogger
133
132
  #
134
133
  # Note: Only appenders that implement the reopen method will be called
135
134
  def self.reopen
136
- @@appenders.each {|appender| appender.reopen if appender.respond_to?(:reopen)}
135
+ @@appenders.each { |appender| appender.reopen if appender.respond_to?(:reopen) }
137
136
  # After a fork the appender thread is not running, start it if it is not running
138
137
  SemanticLogger::Logger.start_appender_thread
139
138
  end
@@ -189,7 +188,7 @@ module SemanticLogger
189
188
  # Set gc_log_microseconds to nil to not enable JRuby Garbage collections
190
189
  def self.add_signal_handler(log_level_signal='USR2', thread_dump_signal='TTIN', gc_log_microseconds=100000)
191
190
  Signal.trap(log_level_signal) do
192
- index = (default_level == :trace) ? LEVELS.find_index(:error) : LEVELS.find_index(default_level)
191
+ index = (default_level == :trace) ? LEVELS.find_index(:error) : LEVELS.find_index(default_level)
193
192
  new_level = LEVELS[index-1]
194
193
  self['SemanticLogger'].warn "Changed global default log level to #{new_level.inspect}"
195
194
  self.default_level = new_level
@@ -242,27 +241,28 @@ module SemanticLogger
242
241
  def self.level_to_index(level)
243
242
  return if level.nil?
244
243
 
245
- index = if level.is_a?(Symbol)
246
- LEVELS.index(level)
247
- elsif level.is_a?(String)
248
- level = level.downcase.to_sym
249
- LEVELS.index(level)
250
- elsif level.is_a?(Integer) && defined?(::Logger::Severity)
251
- # Mapping of Rails and Ruby Logger levels to SemanticLogger levels
252
- @@map_levels ||= begin
253
- levels = []
254
- ::Logger::Severity.constants.each do |constant|
255
- levels[::Logger::Severity.const_get(constant)] = LEVELS.find_index(constant.downcase.to_sym) || LEVELS.find_index(:error)
244
+ index =
245
+ if level.is_a?(Symbol)
246
+ LEVELS.index(level)
247
+ elsif level.is_a?(String)
248
+ level = level.downcase.to_sym
249
+ LEVELS.index(level)
250
+ elsif level.is_a?(Integer) && defined?(::Logger::Severity)
251
+ # Mapping of Rails and Ruby Logger levels to SemanticLogger levels
252
+ @@map_levels ||= begin
253
+ levels = []
254
+ ::Logger::Severity.constants.each do |constant|
255
+ levels[::Logger::Severity.const_get(constant)] = LEVELS.find_index(constant.downcase.to_sym) || LEVELS.find_index(:error)
256
+ end
257
+ levels
256
258
  end
257
- levels
259
+ @@map_levels[level]
258
260
  end
259
- @@map_levels[level]
260
- end
261
261
  raise "Invalid level:#{level.inspect} being requested. Must be one of #{LEVELS.inspect}" unless index
262
262
  index
263
263
  end
264
264
 
265
265
  # Initial default Level for all new instances of SemanticLogger::Logger
266
- @@default_level = :info
266
+ @@default_level = :info
267
267
  @@default_level_index = level_to_index(@@default_level)
268
268
  end
@@ -1,3 +1,3 @@
1
1
  module SemanticLogger #:nodoc
2
- VERSION = "2.15.0"
2
+ VERSION = '2.16.0'
3
3
  end
@@ -3,14 +3,14 @@ require_relative 'test_helper'
3
3
  # Unit Test for SemanticLogger::Appender::Bugsnag
4
4
  #
5
5
  class AppenderBugsnagTest < Minitest::Test
6
- context SemanticLogger::Appender::Bugsnag do
7
- setup do
6
+ describe SemanticLogger::Appender::Bugsnag do
7
+ before do
8
8
  @appender = SemanticLogger::Appender::Bugsnag.new(:warn)
9
9
  @message = 'AppenderBugsnagTest log message'
10
10
  end
11
11
 
12
12
  (SemanticLogger::LEVELS - [:warn, :error]).each do |level|
13
- should "not send :#{level} notifications to Bugsnag" do
13
+ it "not send :#{level} notifications to Bugsnag" do
14
14
  message = hash = nil
15
15
  Bugsnag.stub(:notify, -> msg, h { message = msg; hash = h }) do
16
16
  @appender.send(level, "AppenderBugsnagTest #{level.to_s} message")
@@ -20,7 +20,7 @@ class AppenderBugsnagTest < Minitest::Test
20
20
  end
21
21
  end
22
22
 
23
- should "send error notifications to Bugsnag with severity" do
23
+ it "send error notifications to Bugsnag with severity" do
24
24
  message = hash = nil
25
25
  Bugsnag.stub(:notify, -> msg, h { message = msg; hash = h }) do
26
26
  @appender.error @message
@@ -29,7 +29,7 @@ class AppenderBugsnagTest < Minitest::Test
29
29
  assert_equal 'error', hash[:severity]
30
30
  end
31
31
 
32
- should 'send warn notifications to Bugsnag replace warn severity with warning' do
32
+ it 'send warn notifications to Bugsnag replace warn severity with warning' do
33
33
  message = hash = nil
34
34
  Bugsnag.stub(:notify, -> msg, h { message = msg; hash = h }) do
35
35
  @appender.warn @message
@@ -38,19 +38,19 @@ class AppenderBugsnagTest < Minitest::Test
38
38
  assert_equal 'warning', hash[:severity]
39
39
  end
40
40
 
41
- should 'send notification to Bugsnag with custom attributes' do
41
+ it 'send notification to Bugsnag with custom attributes' do
42
42
  message = hash = nil
43
43
  Bugsnag.stub(:notify, -> msg, h { message = msg; hash = h }) do
44
- @appender.error @message, {:key1 => 1, :key2 => 'a'}
44
+ @appender.error @message, {key1: 1, key2: 'a'}
45
45
  end
46
46
  assert_equal RuntimeError.new(@message), message
47
47
  assert_equal(1, hash[:key1], hash)
48
48
  assert_equal('a', hash[:key2], hash)
49
49
  end
50
50
 
51
- should 'send notification to Bugsnag with exception' do
51
+ it 'send notification to Bugsnag with exception' do
52
52
  message = hash = nil
53
- error = RuntimeError.new('Hello World')
53
+ error = RuntimeError.new('Hello World')
54
54
  Bugsnag.stub(:notify, -> msg, h { message = msg; hash = h }) do
55
55
  @appender.error error
56
56
  end