semantic_logger 4.7.0 → 4.7.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: 0d3b45427759ee16967074aada7eb8dade063d1705172fd8a082de9bb12e1aaf
4
- data.tar.gz: 4db9de710bf8b713a61903a16bde0e436f603de1fa017a410cfebaac00f8a245
3
+ metadata.gz: 53de576d3dea3ae2099d237648a027026469632dda0f8c3840557c648ddb0626
4
+ data.tar.gz: a2fe41e466fd682cc536f33331698a74b19d9026cde7604db93f859e70686d25
5
5
  SHA512:
6
- metadata.gz: 9a7e25591485879924fe5f438a6d0266bd5b590dfcd45cc7a6810e3b679a94eef066e23b3ebc1fe51f2f91f3dbbbdbb681ebeef8292188e7917872202cd178b6
7
- data.tar.gz: 1e74f739dc87c03a41cd1e1a3df7d1d62c36ea175ee93817b21fc7f3039ddde1c4a25dfff8caecfa20a182f2e7c222c76f52d5dbf7a0dc10dfce009206d55726
6
+ metadata.gz: c9c53adbda24e55a427945f9c5953c6e53c244af0c00b4f81ba59c92c3e052d9f7306fc8941bbf0960d665757c56b4089bbc1214255fdfc5ec085fdda24b601e
7
+ data.tar.gz: 700143b4c0b34e347539b7b2fccd363683b45577a5520accd55a308175f92975ca041152ca03353f7e23ec71eb9189e6de54ff8f985eecf414d51b8900b2ad96
@@ -42,9 +42,9 @@ module SemanticLogger
42
42
  lag_threshold_s: lag_threshold_s
43
43
  )
44
44
 
45
- unless appender.respond_to?(:batch)
46
- raise(ArgumentError, "#{appender.class.name} does not support batching. It must implement #batch")
47
- end
45
+ return if appender.respond_to?(:batch)
46
+
47
+ raise(ArgumentError, "#{appender.class.name} does not support batching. It must implement #batch")
48
48
  end
49
49
 
50
50
  # Add log message for processing.
@@ -87,7 +87,7 @@ module SemanticLogger
87
87
  # Returns [String] JSON to send to Splunk.
88
88
  #
89
89
  # For splunk format requirements see:
90
- # http://dev.splunk.com/view/event-collector/SP-CAAAE6P
90
+ # https://docs.splunk.com/Documentation/Splunk/latest/Data/FormateventsforHTTPEventCollector
91
91
  def call(log, logger)
92
92
  h = SemanticLogger::Formatters::Raw.new(time_format: :seconds).call(log, logger)
93
93
  message = {
@@ -96,7 +96,6 @@ module SemanticLogger
96
96
  time: h.delete(:time),
97
97
  event: h
98
98
  }
99
- message[:environment] = logger.environment if logger.environment
100
99
  message[:sourcetype] = source_type if source_type
101
100
  message[:index] = index if index
102
101
  message.to_json
@@ -303,23 +303,42 @@ module SemanticLogger
303
303
  end
304
304
 
305
305
  # Log message at the specified level
306
- def log_internal(level, index, message = nil, payload = nil, exception = nil, &block)
307
- log = Log.new(name, level, index)
306
+ def log_internal(level, index, message = nil, payload = nil, exception = nil)
307
+ # Handle variable number of arguments by detecting exception object and payload hash.
308
+ if exception.nil? && payload.nil? && message.respond_to?(:backtrace) && message.respond_to?(:message)
309
+ exception = message
310
+ message = nil
311
+ elsif exception.nil? && payload && payload.respond_to?(:backtrace) && payload.respond_to?(:message)
312
+ exception = payload
313
+ payload = nil
314
+ elsif payload && !payload.is_a?(Hash)
315
+ message = message.nil? ? payload : "#{message} -- #{payload}"
316
+ payload = nil
317
+ end
318
+
319
+ log = Log.new(name, level, index)
308
320
  should_log =
309
321
  if payload.nil? && exception.nil? && message.is_a?(Hash)
310
- # Check if someone just logged a hash payload instead of meaning to call semantic logger
311
- if message.key?(:message) || message.key?(:payload) || message.key?(:exception) || message.key?(:metric)
312
- log.assign(**message)
313
- else
314
- log.assign_positional(nil, message, nil, &block)
315
- end
316
- elsif exception.nil? && message && payload && payload.is_a?(Hash) &&
317
- (payload.key?(:payload) || payload.key?(:exception) || payload.key?(:metric))
318
- log.assign(message: message, **payload)
322
+ # Everything as keyword arguments.
323
+ log.assign(**log.extract_arguments(message))
324
+ elsif exception.nil? && message && payload && payload.is_a?(Hash)
325
+ # Message with keyword arguments as the rest.
326
+ log.assign(message: message, **log.extract_arguments(payload))
319
327
  else
320
- log.assign_positional(message, payload, exception, &block)
328
+ # No keyword arguments.
329
+ log.assign(message: message, payload: payload, exception: exception)
321
330
  end
322
331
 
332
+ # Add result of block to message or payload if not nil
333
+ if block_given?
334
+ result = yield(log)
335
+ if result.is_a?(String)
336
+ log.message = log.message.nil? ? result : "#{log.message} -- #{result}"
337
+ elsif result.is_a?(Hash)
338
+ log.assign_hash(result)
339
+ end
340
+ end
341
+
323
342
  # Log level may change during assign due to :on_exception_level
324
343
  self.log(log) if should_log && should_log?(log)
325
344
  end
@@ -349,10 +368,10 @@ module SemanticLogger
349
368
  exception = e
350
369
  ensure
351
370
  # Must use ensure block otherwise a `return` in the yield above will skip the log entry
352
- log = Log.new(name, level, index)
371
+ log = Log.new(name, level, index)
353
372
  exception ||= params[:exception]
354
- message = params[:message] if params[:message]
355
- duration =
373
+ message = params[:message] if params[:message]
374
+ duration =
356
375
  if block_given?
357
376
  1_000.0 * (Process.clock_gettime(Process::CLOCK_MONOTONIC) - start)
358
377
  else
@@ -1,8 +1,12 @@
1
- # Load AwesomePrint if available
1
+ # Load Amazing Print, or Awesome Print if available
2
2
  begin
3
- require "awesome_print"
3
+ require "amazing_print"
4
4
  rescue LoadError
5
- nil
5
+ begin
6
+ require "awesome_print"
7
+ rescue LoadError
8
+ nil
9
+ end
6
10
  end
7
11
 
8
12
  module SemanticLogger
@@ -61,9 +65,9 @@ module SemanticLogger
61
65
  #
62
66
  # Parameters:
63
67
  # ap: [Hash]
64
- # Any valid AwesomePrint option for rendering data.
68
+ # Any valid Amazing Print option for rendering data.
65
69
  # These options can also be changed be creating a `~/.aprc` file.
66
- # See: https://github.com/michaeldv/awesome_print
70
+ # See: https://github.com/amazing-print/amazing_print
67
71
  #
68
72
  # Note: The option :multiline is set to false if not supplied.
69
73
  # Note: Has no effect if Awesome Print is not installed.
@@ -105,7 +109,7 @@ module SemanticLogger
105
109
  def payload
106
110
  return unless log.payload?
107
111
 
108
- if !defined?(AwesomePrint) || !log.payload.respond_to?(:ai)
112
+ if !log.payload.respond_to?(:ai)
109
113
  super
110
114
  else
111
115
  begin
@@ -101,7 +101,7 @@ module SemanticLogger
101
101
  message: exception.message,
102
102
  stack_trace: exception.backtrace
103
103
  }
104
- root = root[name]
104
+ root = root[name]
105
105
  end
106
106
  end
107
107
 
@@ -47,6 +47,12 @@ module SemanticLogger
47
47
  # context [Hash]
48
48
  # Named contexts that were captured when the log entry was created.
49
49
  class Log
50
+ # Keys passed in without a payload that will be extracted and the remainder passed into the payload.
51
+ NON_PAYLOAD_KEYS = %i[message exception backtrace exception
52
+ duration min_duration
53
+ log_exception on_exception_level
54
+ metric metric_amount dimensions].freeze
55
+
50
56
  attr_accessor :level, :level_index, :name, :message, :time, :duration,
51
57
  :payload, :exception, :thread_name, :backtrace,
52
58
  :tags, :named_tags, :context,
@@ -79,20 +85,13 @@ module SemanticLogger
79
85
  log_exception: :full,
80
86
  on_exception_level: nil,
81
87
  dimensions: nil)
82
- # Elastic logging: Log when :duration exceeds :min_duration
83
- # Except if there is an exception when it will always be logged
84
- if duration
85
- self.duration = duration
86
- return false if (duration < min_duration) && exception.nil?
87
- end
88
88
 
89
- self.message = message
90
- if payload&.is_a?(Hash)
91
- self.payload = payload
92
- elsif payload
93
- self.message = message.nil? ? payload.to_s : "#{message} -- #{payload}"
94
- self.payload = nil
95
- end
89
+ self.message = message
90
+ self.payload = payload
91
+ self.duration = duration
92
+ self.metric = metric
93
+ self.metric_amount = metric_amount
94
+ self.dimensions = dimensions
96
95
 
97
96
  if exception
98
97
  case log_exception
@@ -113,57 +112,45 @@ module SemanticLogger
113
112
  end
114
113
  end
115
114
 
115
+ # Elastic logging: Log when :duration exceeds :min_duration
116
+ # Except if there is an exception when it will always be logged
117
+ if duration
118
+ return false if (duration < min_duration) && exception.nil?
119
+ end
120
+
116
121
  if backtrace
117
122
  self.backtrace = Utils.extract_backtrace(backtrace)
118
123
  elsif level_index >= SemanticLogger.backtrace_level_index
119
124
  self.backtrace = Utils.extract_backtrace
120
125
  end
121
126
 
122
- if metric
123
- self.metric = metric
124
- self.metric_amount = metric_amount
125
- self.dimensions = dimensions
126
- end
127
-
128
127
  true
129
128
  end
130
129
 
131
- # Assign positional arguments to this log entry, supplying defaults where applicable
132
- #
133
- # Returns [true|false] whether this log entry should be logged
134
- #
135
- # Example:
136
- # logger.info('value', :debug, 0, "hello world")
137
- def assign_positional(message = nil, payload = nil, exception = nil)
138
- # Exception being logged?
139
- # Under JRuby a java exception is not a Ruby Exception
140
- # Java::JavaLang::ClassCastException.new.is_a?(Exception) => false
141
- if exception.nil? && payload.nil? && message.respond_to?(:backtrace) && message.respond_to?(:message)
142
- exception = message
143
- message = nil
144
- elsif exception.nil? && payload && payload.respond_to?(:backtrace) && payload.respond_to?(:message)
145
- exception = payload
146
- payload = nil
147
- elsif payload && !payload.is_a?(Hash)
148
- message = message.nil? ? payload : "#{message} -- #{payload}"
149
- payload = nil
150
- end
151
-
152
- # Add result of block as message or payload if not nil
153
- if block_given? && (result = yield)
154
- if result.is_a?(String)
155
- message = message.nil? ? result : "#{message} -- #{result}"
156
- assign(message: message, payload: payload, exception: exception)
157
- elsif message.nil? && result.is_a?(Hash) && %i[message payload exception].any? { |k| result.key? k }
158
- assign(**result)
159
- elsif payload&.respond_to?(:merge)
160
- assign(message: message, payload: payload.merge(result), exception: exception)
130
+ # Assign known keys to self, all other keys to the payload.
131
+ def assign_hash(hash)
132
+ self.payload ||= {}
133
+ hash.each_pair do |key, value|
134
+ if respond_to?("#{key}=".to_sym)
135
+ public_send("#{key}=".to_sym, value)
161
136
  else
162
- assign(message: message, payload: result, exception: exception)
137
+ payload[key] = value
163
138
  end
164
- else
165
- assign(message: message, payload: payload, exception: exception)
166
139
  end
140
+ self.payload = nil if payload.empty?
141
+ self
142
+ end
143
+
144
+ # Extract the arguments from a Hash Payload
145
+ def extract_arguments(payload)
146
+ raise(ArgumentError, "payload must be a Hash") unless payload.is_a?(Hash)
147
+
148
+ return payload if payload.key?(:payload)
149
+
150
+ args = {}
151
+ payload.each_key { |key| args[key] = payload.delete(key) if NON_PAYLOAD_KEYS.include?(key) }
152
+ args[:payload] = payload unless payload.empty?
153
+ args
167
154
  end
168
155
 
169
156
  MAX_EXCEPTIONS_TO_UNWRAP = 5
@@ -178,7 +165,7 @@ module SemanticLogger
178
165
  yield(ex, depth)
179
166
 
180
167
  depth += 1
181
- ex =
168
+ ex =
182
169
  if ex.respond_to?(:cause) && ex.cause
183
170
  ex.cause
184
171
  elsif ex.respond_to?(:continued_exception) && ex.continued_exception
@@ -280,8 +267,7 @@ module SemanticLogger
280
267
  end
281
268
 
282
269
  def to_h(host = SemanticLogger.host, application = SemanticLogger.application, environment = SemanticLogger.environment)
283
- logger = Struct.new(:host, :application, :environment)
284
- .new(host, application, environment)
270
+ logger = Struct.new(:host, :application, :environment).new(host, application, environment)
285
271
  SemanticLogger::Formatters::Raw.new.call(self, logger)
286
272
  end
287
273
 
@@ -63,8 +63,6 @@ module SemanticLogger
63
63
  Logger.processor.log(log)
64
64
  end
65
65
 
66
- private
67
-
68
66
  @processor = nil
69
67
  @subscribers = nil
70
68
 
@@ -75,7 +73,7 @@ module SemanticLogger
75
73
  begin
76
74
  subscriber.call(log)
77
75
  rescue Exception => e
78
- self.class.processor.logger.error("Exception calling :on_log subscriber", e)
76
+ processor.logger.error("Exception calling :on_log subscriber", e)
79
77
  end
80
78
  end
81
79
  end
@@ -502,12 +502,10 @@ module SemanticLogger
502
502
  @sync
503
503
  end
504
504
 
505
- private
506
-
507
505
  # Initial default Level for all new instances of SemanticLogger::Logger
508
506
  @default_level = :info
509
507
  @default_level_index = Levels.index(@default_level)
510
508
  @backtrace_level = :error
511
509
  @backtrace_level_index = Levels.index(@backtrace_level)
512
- @synchronous = false
510
+ @sync = false
513
511
  end
@@ -39,6 +39,5 @@ module SemanticLogger
39
39
  def start
40
40
  # NOP
41
41
  end
42
-
43
42
  end
44
43
  end
@@ -1,3 +1,3 @@
1
1
  module SemanticLogger
2
- VERSION = "4.7.0".freeze
2
+ VERSION = "4.7.1".freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: semantic_logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.7.0
4
+ version: 4.7.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: 2020-05-16 00:00:00.000000000 Z
11
+ date: 2020-06-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: concurrent-ruby
@@ -108,7 +108,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
108
108
  - !ruby/object:Gem::Version
109
109
  version: '0'
110
110
  requirements: []
111
- rubygems_version: 3.0.6
111
+ rubygems_version: 3.1.2
112
112
  signing_key:
113
113
  specification_version: 4
114
114
  summary: Feature rich logging framework, and replacement for existing Ruby & Rails