semantic_logger 4.2.0 → 4.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (88) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +3 -3
  3. data/Rakefile +1 -1
  4. data/lib/semantic_logger/ansi_colors.rb +11 -12
  5. data/lib/semantic_logger/appender.rb +4 -5
  6. data/lib/semantic_logger/appender/async.rb +24 -16
  7. data/lib/semantic_logger/appender/async_batch.rb +1 -4
  8. data/lib/semantic_logger/appender/bugsnag.rb +67 -63
  9. data/lib/semantic_logger/appender/elasticsearch.rb +154 -157
  10. data/lib/semantic_logger/appender/elasticsearch_http.rb +59 -55
  11. data/lib/semantic_logger/appender/file.rb +1 -3
  12. data/lib/semantic_logger/appender/graylog.rb +114 -110
  13. data/lib/semantic_logger/appender/honeybadger.rb +54 -51
  14. data/lib/semantic_logger/appender/http.rb +194 -190
  15. data/lib/semantic_logger/appender/kafka.rb +152 -149
  16. data/lib/semantic_logger/appender/mongodb.rb +3 -3
  17. data/lib/semantic_logger/appender/new_relic.rb +52 -49
  18. data/lib/semantic_logger/appender/sentry.rb +59 -54
  19. data/lib/semantic_logger/appender/splunk.rb +108 -103
  20. data/lib/semantic_logger/appender/splunk_http.rb +82 -79
  21. data/lib/semantic_logger/appender/syslog.rb +4 -5
  22. data/lib/semantic_logger/appender/tcp.rb +8 -29
  23. data/lib/semantic_logger/appender/udp.rb +2 -3
  24. data/lib/semantic_logger/appender/wrapper.rb +2 -2
  25. data/lib/semantic_logger/base.rb +18 -16
  26. data/lib/semantic_logger/concerns/compatibility.rb +0 -1
  27. data/lib/semantic_logger/core_ext/thread.rb +0 -1
  28. data/lib/semantic_logger/formatters.rb +3 -5
  29. data/lib/semantic_logger/formatters/base.rb +2 -3
  30. data/lib/semantic_logger/formatters/color.rb +29 -12
  31. data/lib/semantic_logger/formatters/default.rb +10 -10
  32. data/lib/semantic_logger/formatters/json.rb +0 -2
  33. data/lib/semantic_logger/formatters/one_line.rb +2 -2
  34. data/lib/semantic_logger/formatters/raw.rb +7 -10
  35. data/lib/semantic_logger/formatters/signalfx.rb +3 -5
  36. data/lib/semantic_logger/formatters/syslog.rb +2 -3
  37. data/lib/semantic_logger/formatters/syslog_cee.rb +2 -3
  38. data/lib/semantic_logger/jruby/garbage_collection_logger.rb +8 -5
  39. data/lib/semantic_logger/log.rb +17 -17
  40. data/lib/semantic_logger/loggable.rb +6 -9
  41. data/lib/semantic_logger/logger.rb +0 -1
  42. data/lib/semantic_logger/metric/new_relic.rb +58 -55
  43. data/lib/semantic_logger/metric/signalfx.rb +108 -106
  44. data/lib/semantic_logger/metric/statsd.rb +2 -3
  45. data/lib/semantic_logger/processor.rb +9 -9
  46. data/lib/semantic_logger/semantic_logger.rb +50 -30
  47. data/lib/semantic_logger/subscriber.rb +0 -1
  48. data/lib/semantic_logger/utils.rb +37 -37
  49. data/lib/semantic_logger/version.rb +2 -2
  50. data/test/appender/async_batch_test.rb +0 -1
  51. data/test/appender/async_test.rb +0 -1
  52. data/test/appender/bugsnag_test.rb +7 -8
  53. data/test/appender/elasticsearch_http_test.rb +5 -6
  54. data/test/appender/elasticsearch_test.rb +14 -10
  55. data/test/appender/file_test.rb +5 -6
  56. data/test/appender/graylog_test.rb +8 -8
  57. data/test/appender/honeybadger_test.rb +6 -7
  58. data/test/appender/http_test.rb +4 -5
  59. data/test/appender/kafka_test.rb +5 -6
  60. data/test/appender/mongodb_test.rb +11 -13
  61. data/test/appender/new_relic_test.rb +8 -9
  62. data/test/appender/newrelic_rpm.rb +1 -1
  63. data/test/appender/sentry_test.rb +7 -8
  64. data/test/appender/splunk_http_test.rb +4 -4
  65. data/test/appender/splunk_test.rb +1 -3
  66. data/test/appender/syslog_test.rb +3 -5
  67. data/test/appender/tcp_test.rb +4 -5
  68. data/test/appender/udp_test.rb +4 -5
  69. data/test/appender/wrapper_test.rb +2 -3
  70. data/test/concerns/compatibility_test.rb +0 -1
  71. data/test/debug_as_trace_logger_test.rb +0 -1
  72. data/test/formatters/color_test.rb +5 -6
  73. data/test/formatters/default_test.rb +16 -17
  74. data/test/formatters/one_line_test.rb +1 -2
  75. data/test/formatters/signalfx_test.rb +8 -11
  76. data/test/formatters_test.rb +3 -3
  77. data/test/in_memory_appender.rb +0 -1
  78. data/test/in_memory_appender_helper.rb +1 -1
  79. data/test/in_memory_batch_appender.rb +0 -1
  80. data/test/in_memory_metrics_appender.rb +0 -1
  81. data/test/loggable_test.rb +2 -3
  82. data/test/logger_test.rb +11 -14
  83. data/test/measure_test.rb +13 -15
  84. data/test/metric/new_relic_test.rb +2 -3
  85. data/test/metric/signalfx_test.rb +4 -5
  86. data/test/semantic_logger_test.rb +28 -3
  87. data/test/test_helper.rb +6 -7
  88. metadata +34 -34
@@ -12,89 +12,92 @@ require 'json'
12
12
  # url: 'http://localhost:8080',
13
13
  # token: '70CA900C-3D7E-42A4-9C79-7975D1C422A8'
14
14
  # )
15
- class SemanticLogger::Appender::SplunkHttp < SemanticLogger::Appender::Http
16
- attr_accessor :source_type, :index
15
+ module SemanticLogger
16
+ module Appender
17
+ class SplunkHttp < SemanticLogger::Appender::Http
18
+ attr_accessor :source_type, :index
17
19
 
18
- # Create Splunk appender over persistent HTTP(S)
19
- #
20
- # Parameters:
21
- # token: [String]
22
- # Token created in Splunk for this HTTP Appender
23
- # Mandatory.
24
- #
25
- # source_type: [String]
26
- # Optional: Source type to display in Splunk
27
- #
28
- # index: [String]
29
- # Optional: Name of a valid index for this message in Splunk.
30
- #
31
- # url: [String]
32
- # Valid URL to post to.
33
- # Example: http://example.com
34
- # To enable SSL include https in the URL.
35
- # Example: https://example.com
36
- # verify_mode will default: OpenSSL::SSL::VERIFY_PEER
37
- #
38
- # application: [String]
39
- # Name of this application to appear in log messages.
40
- # Default: SemanticLogger.application
41
- #
42
- # host: [String]
43
- # Name of this host to appear in log messages.
44
- # Default: SemanticLogger.host
45
- #
46
- # compress: [true|false]
47
- # Splunk supports HTTP Compression, enable by default.
48
- # Default: true
49
- #
50
- # ssl: [Hash]
51
- # Specific SSL options: For more details see NET::HTTP.start
52
- # ca_file, ca_path, cert, cert_store, ciphers, key, open_timeout, read_timeout, ssl_timeout,
53
- # ssl_version, use_ssl, verify_callback, verify_depth and verify_mode.
54
- #
55
- # level: [:trace | :debug | :info | :warn | :error | :fatal]
56
- # Override the log level for this appender.
57
- # Default: SemanticLogger.default_level
58
- #
59
- # formatter: [Object|Proc]
60
- # An instance of a class that implements #call, or a Proc to be used to format
61
- # the output from this appender
62
- # Default: Use the built-in formatter (See: #call)
63
- #
64
- # filter: [Regexp|Proc]
65
- # RegExp: Only include log messages where the class name matches the supplied.
66
- # regular expression. All other messages will be ignored.
67
- # Proc: Only include log messages where the supplied Proc returns true
68
- # The Proc must return true or false.
69
- def initialize(token: nil, source_type: nil, index: nil,
70
- url:, compress: true, ssl: {}, open_timeout: 2.0, read_timeout: 1.0, continue_timeout: 1.0,
71
- level: nil, formatter: nil, filter: nil, application: nil, host: nil, &block)
20
+ # Create Splunk appender over persistent HTTP(S)
21
+ #
22
+ # Parameters:
23
+ # token: [String]
24
+ # Token created in Splunk for this HTTP Appender
25
+ # Mandatory.
26
+ #
27
+ # source_type: [String]
28
+ # Optional: Source type to display in Splunk
29
+ #
30
+ # index: [String]
31
+ # Optional: Name of a valid index for this message in Splunk.
32
+ #
33
+ # url: [String]
34
+ # Valid URL to post to.
35
+ # Example: http://example.com
36
+ # To enable SSL include https in the URL.
37
+ # Example: https://example.com
38
+ # verify_mode will default: OpenSSL::SSL::VERIFY_PEER
39
+ #
40
+ # application: [String]
41
+ # Name of this application to appear in log messages.
42
+ # Default: SemanticLogger.application
43
+ #
44
+ # host: [String]
45
+ # Name of this host to appear in log messages.
46
+ # Default: SemanticLogger.host
47
+ #
48
+ # compress: [true|false]
49
+ # Splunk supports HTTP Compression, enable by default.
50
+ # Default: true
51
+ #
52
+ # ssl: [Hash]
53
+ # Specific SSL options: For more details see NET::HTTP.start
54
+ # ca_file, ca_path, cert, cert_store, ciphers, key, open_timeout, read_timeout, ssl_timeout,
55
+ # ssl_version, use_ssl, verify_callback, verify_depth and verify_mode.
56
+ #
57
+ # level: [:trace | :debug | :info | :warn | :error | :fatal]
58
+ # Override the log level for this appender.
59
+ # Default: SemanticLogger.default_level
60
+ #
61
+ # formatter: [Object|Proc]
62
+ # An instance of a class that implements #call, or a Proc to be used to format
63
+ # the output from this appender
64
+ # Default: Use the built-in formatter (See: #call)
65
+ #
66
+ # filter: [Regexp|Proc]
67
+ # RegExp: Only include log messages where the class name matches the supplied.
68
+ # regular expression. All other messages will be ignored.
69
+ # Proc: Only include log messages where the supplied Proc returns true
70
+ # The Proc must return true or false.
71
+ def initialize(token: nil, source_type: nil, index: nil,
72
+ url:, compress: true, ssl: {}, open_timeout: 2.0, read_timeout: 1.0, continue_timeout: 1.0,
73
+ level: nil, formatter: nil, filter: nil, application: nil, host: nil, &block)
72
74
 
73
- @source_type = source_type
74
- @index = index
75
+ @source_type = source_type
76
+ @index = index
75
77
 
76
- super(url: url, compress: compress, ssl: ssl, read_timeout: read_timeout, open_timeout: open_timeout, continue_timeout: continue_timeout,
77
- level: level, formatter: formatter, filter: filter, application: application, host: host, &block)
78
+ super(url: url, compress: compress, ssl: ssl, read_timeout: read_timeout, open_timeout: open_timeout, continue_timeout: continue_timeout,
79
+ level: level, formatter: formatter, filter: filter, application: application, host: host, &block)
78
80
 
79
- # Put splunk auth token in the header of every HTTP post.
80
- @header['Authorization'] = "Splunk #{token}"
81
- end
81
+ # Put splunk auth token in the header of every HTTP post.
82
+ @header['Authorization'] = "Splunk #{token}"
83
+ end
82
84
 
83
- # Returns [String] JSON to send to Splunk.
84
- #
85
- # For splunk format requirements see:
86
- # http://dev.splunk.com/view/event-collector/SP-CAAAE6P
87
- def call(log, logger)
88
- h = SemanticLogger::Formatters::Raw.new(time_format: :seconds).call(log, logger)
89
- message = {
90
- source: logger.application,
91
- host: logger.host,
92
- time: h.delete(:time),
93
- event: h
94
- }
95
- message[:source_type] = source_type if source_type
96
- message[:index] = index if index
97
- message.to_json
85
+ # Returns [String] JSON to send to Splunk.
86
+ #
87
+ # For splunk format requirements see:
88
+ # http://dev.splunk.com/view/event-collector/SP-CAAAE6P
89
+ def call(log, logger)
90
+ h = SemanticLogger::Formatters::Raw.new(time_format: :seconds).call(log, logger)
91
+ message = {
92
+ source: logger.application,
93
+ host: logger.host,
94
+ time: h.delete(:time),
95
+ event: h
96
+ }
97
+ message[:source_type] = source_type if source_type
98
+ message[:index] = index if index
99
+ message.to_json
100
+ end
101
+ end
98
102
  end
99
-
100
103
  end
@@ -120,7 +120,7 @@ module SemanticLogger
120
120
  # # Change the warn level to LOG_NOTICE level instead of a the default of LOG_WARNING.
121
121
  # SemanticLogger.add_appender(appender: :syslog, level_map: {warn: ::Syslog::LOG_NOTICE})
122
122
  def initialize(url: 'syslog://localhost',
123
- facility: ::Syslog::LOG_USER, level_map: SemanticLogger::Formatters::Syslog::LevelMap.new, options: ::Syslog::LOG_PID|::Syslog::LOG_CONS,
123
+ facility: ::Syslog::LOG_USER, level_map: SemanticLogger::Formatters::Syslog::LevelMap.new, options: ::Syslog::LOG_PID| ::Syslog::LOG_CONS,
124
124
  tcp_client: {},
125
125
  level: nil, formatter: nil, filter: nil, application: nil, host: nil, &block)
126
126
 
@@ -135,10 +135,10 @@ module SemanticLogger
135
135
  @server = 'localhost' if @protocol == :syslog
136
136
  @tcp_client_options = tcp_client
137
137
 
138
- raise "Unknown protocol #{@protocol}!" unless [:syslog, :tcp, :udp].include?(@protocol)
138
+ raise "Unknown protocol #{@protocol}!" unless %i[syslog tcp udp].include?(@protocol)
139
139
 
140
140
  # The syslog_protocol gem is required when logging over TCP or UDP.
141
- if [:tcp, :udp].include?(@protocol)
141
+ if %i[tcp udp].include?(@protocol)
142
142
  begin
143
143
  require 'syslog_protocol'
144
144
  rescue LoadError
@@ -196,7 +196,7 @@ module SemanticLogger
196
196
 
197
197
  # Flush is called by the semantic_logger during shutdown.
198
198
  def flush
199
- @remote_syslog.flush if @remote_syslog && @remote_syslog.respond_to?(:flush)
199
+ @remote_syslog.flush if @remote_syslog&.respond_to?(:flush)
200
200
  end
201
201
 
202
202
  # Returns [SemanticLogger::Formatters::Base] default formatter for this Appender depending on the protocal selected
@@ -208,7 +208,6 @@ module SemanticLogger
208
208
  SemanticLogger::Formatters::Syslog.new(facility: facility, level_map: level_map)
209
209
  end
210
210
  end
211
-
212
211
  end
213
212
  end
214
213
  end
@@ -181,31 +181,11 @@ module SemanticLogger
181
181
  # connect_retry_interval: 0.1,
182
182
  # connect_retry_count: 5
183
183
  # )
184
- def initialize(server: nil, servers: nil, separator: "\n",
185
- policy: :ordered, buffered: true, #keepalive: true,
186
- connect_timeout: 10.0, read_timeout: 60.0, write_timeout: 60.0,
187
- connect_retry_count: 10, retry_count: 3, connect_retry_interval: 0.5, close_on_error: true,
188
- on_connect: nil, proxy_server: nil, ssl: nil,
189
- level: nil, formatter: nil, filter: nil, application: nil, host: nil, &block
190
- )
191
- @separator = separator
192
- @options = {
193
- server: server,
194
- servers: servers,
195
- policy: policy,
196
- buffered: buffered,
197
- #keepalive: keepalive,
198
- connect_timeout: connect_timeout,
199
- read_timeout: read_timeout,
200
- write_timeout: write_timeout,
201
- connect_retry_count: connect_retry_count,
202
- retry_count: retry_count,
203
- connect_retry_interval: connect_retry_interval,
204
- close_on_error: close_on_error,
205
- on_connect: on_connect,
206
- proxy_server: proxy_server,
207
- ssl: ssl
208
- }
184
+ def initialize(separator: "\n",
185
+ level: nil, formatter: nil, filter: nil, application: nil, host: nil,
186
+ **tcp_client_args, &block)
187
+ @separator = separator
188
+ @tcp_client_args = tcp_client_args
209
189
 
210
190
  # Use the internal logger so that errors with remote logging are only written locally.
211
191
  Net::TCPClient.logger = logger
@@ -218,7 +198,7 @@ module SemanticLogger
218
198
  # After forking an active process call #reopen to re-open the handles to resources.
219
199
  def reopen
220
200
  close
221
- @tcp_client = Net::TCPClient.new(@options)
201
+ @tcp_client = Net::TCPClient.new(@tcp_client_args)
222
202
  end
223
203
 
224
204
  # Write the log using the specified protocol and server.
@@ -232,12 +212,12 @@ module SemanticLogger
232
212
 
233
213
  # Flush is called by the semantic_logger during shutdown.
234
214
  def flush
235
- @tcp_client.flush if @tcp_client
215
+ @tcp_client&.flush
236
216
  end
237
217
 
238
218
  # Close is called during shutdown, or with reopen
239
219
  def close
240
- @tcp_client.close if @tcp_client
220
+ @tcp_client&.close
241
221
  end
242
222
 
243
223
  private
@@ -246,7 +226,6 @@ module SemanticLogger
246
226
  def default_formatter
247
227
  SemanticLogger::Formatters::Json.new
248
228
  end
249
-
250
229
  end
251
230
  end
252
231
  end
@@ -82,12 +82,12 @@ module SemanticLogger
82
82
 
83
83
  # Flush is called by the semantic_logger during shutdown.
84
84
  def flush
85
- @socket.flush if @socket
85
+ @socket&.flush
86
86
  end
87
87
 
88
88
  # Close is called during shutdown, or with reopen
89
89
  def close
90
- @socket.close if @socket
90
+ @socket&.close
91
91
  end
92
92
 
93
93
  private
@@ -96,7 +96,6 @@ module SemanticLogger
96
96
  def default_formatter
97
97
  SemanticLogger::Formatters::Json.new
98
98
  end
99
-
100
99
  end
101
100
  end
102
101
  end
@@ -43,7 +43,8 @@ module SemanticLogger
43
43
  @logger = logger
44
44
 
45
45
  # Check if the custom appender responds to all the log levels. For example Ruby ::Logger
46
- if does_not_implement = LEVELS[1..-1].find { |i| !@logger.respond_to?(i) }
46
+ does_not_implement = LEVELS[1..-1].find { |i| !@logger.respond_to?(i) }
47
+ if does_not_implement
47
48
  raise(ArgumentError, "Supplied logger does not implement:#{does_not_implement}. It must implement all of #{LEVELS[1..-1].inspect}")
48
49
  end
49
50
 
@@ -63,7 +64,6 @@ module SemanticLogger
63
64
  def flush
64
65
  @logger.flush if @logger.respond_to?(:flush)
65
66
  end
66
-
67
67
  end
68
68
  end
69
69
  end
@@ -23,7 +23,7 @@ module SemanticLogger
23
23
  @level_index = nil
24
24
  @level = nil
25
25
  else
26
- @level_index = SemanticLogger.level_to_index(level)
26
+ @level_index = SemanticLogger.send(:level_to_index, level)
27
27
  @level = SemanticLogger.send(:index_to_level, @level_index)
28
28
  end
29
29
  end
@@ -78,7 +78,7 @@ module SemanticLogger
78
78
  # logger.info("Parsing received XML", exc)
79
79
  #
80
80
  SemanticLogger::LEVELS.each_with_index do |level, index|
81
- class_eval <<-EOT, __FILE__, __LINE__ + 1
81
+ class_eval <<~METHODS, __FILE__, __LINE__ + 1
82
82
  def #{level}(message=nil, payload=nil, exception=nil, &block)
83
83
  if level_index <= #{index}
84
84
  log_internal(:#{level}, #{index}, message, payload, exception, &block)
@@ -107,7 +107,7 @@ module SemanticLogger
107
107
  block.call(params) if block
108
108
  end
109
109
  end
110
- EOT
110
+ METHODS
111
111
  end
112
112
 
113
113
  # Dynamically supply the log level with every measurement call
@@ -115,13 +115,13 @@ module SemanticLogger
115
115
  index = SemanticLogger.level_to_index(level)
116
116
  if level_index <= index
117
117
  measure_internal(level, index, message, params, &block)
118
- else
119
- block.call(params) if block
118
+ elsif block
119
+ yield(params)
120
120
  end
121
121
  end
122
122
 
123
123
  # Backward compatibility
124
- alias_method :benchmark, :measure
124
+ alias benchmark measure
125
125
 
126
126
  # Log a thread backtrace
127
127
  def backtrace(thread: Thread.current,
@@ -150,7 +150,11 @@ module SemanticLogger
150
150
  message << backtrace.join("\n")
151
151
  end
152
152
 
153
- if log.assign(message: message, backtrace: backtrace, payload: payload, metric: metric, metric_amount: metric_amount) && !filtered?(log)
153
+ if log.assign(message: message,
154
+ backtrace: backtrace,
155
+ payload: payload,
156
+ metric: metric,
157
+ metric_amount: metric_amount) && !filtered?(log)
154
158
  self.log(log)
155
159
  else
156
160
  false
@@ -196,7 +200,7 @@ module SemanticLogger
196
200
  end
197
201
 
198
202
  # :nodoc:
199
- alias_method :with_tags, :tagged
203
+ alias with_tags tagged
200
204
 
201
205
  # :nodoc:
202
206
  def tags
@@ -243,8 +247,8 @@ module SemanticLogger
243
247
  end
244
248
 
245
249
  # Write log data to underlying data storage
246
- def log(log_)
247
- raise NotImplementedError.new('Logging Appender must implement #log(log)')
250
+ def log(_log_)
251
+ raise NotImplementedError, 'Logging Appender must implement #log(log)'
248
252
  end
249
253
 
250
254
  # Whether this log entry meets the criteria to be logged by this appender.
@@ -298,7 +302,7 @@ module SemanticLogger
298
302
  return false if @filter.nil?
299
303
 
300
304
  if @filter.is_a?(Regexp)
301
- (@filter =~ log.name) == nil
305
+ (@filter =~ log.name).nil?
302
306
  elsif @filter.is_a?(Proc)
303
307
  @filter.call(log) != true
304
308
  else
@@ -317,7 +321,7 @@ module SemanticLogger
317
321
  should_log =
318
322
  if payload.nil? && exception.nil? && message.is_a?(Hash)
319
323
  # Check if someone just logged a hash payload instead of meaning to call semantic logger
320
- if message.has_key?(:message) || message.has_key?(:payload) || message.has_key?(:exception) || message.has_key?(:metric)
324
+ if message.key?(:message) || message.key?(:payload) || message.key?(:exception) || message.key?(:metric)
321
325
  log.assign(message)
322
326
  else
323
327
  log.assign_positional(nil, message, nil, &block)
@@ -343,7 +347,7 @@ module SemanticLogger
343
347
  begin
344
348
  if block_given?
345
349
  result =
346
- if silence_level = params[:silence]
350
+ if (silence_level = params[:silence])
347
351
  # In case someone accidentally sets `silence: true` instead of `silence: :error`
348
352
  silence_level = :error if silence_level == true
349
353
  silence(silence_level) { yield(params) }
@@ -395,8 +399,7 @@ module SemanticLogger
395
399
  min_duration:,
396
400
  metric:,
397
401
  log_exception:,
398
- on_exception_level:,
399
- &block)
402
+ on_exception_level:)
400
403
 
401
404
  # Ignores filter, silence, payload
402
405
  exception = nil
@@ -423,6 +426,5 @@ module SemanticLogger
423
426
  raise exception if exception
424
427
  end
425
428
  end
426
-
427
429
  end
428
430
  end
@@ -47,7 +47,6 @@ module SemanticLogger
47
47
  false
48
48
  end
49
49
  end
50
-
51
50
  end
52
51
  end
53
52
  end
@@ -1,4 +1,3 @@
1
- require 'thread'
2
1
  class Thread
3
2
  undef :name if method_defined? :name
4
3
  undef :name= if method_defined? :name=