semantic_logger 4.6.1 → 4.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. checksums.yaml +4 -4
  2. data/Rakefile +7 -7
  3. data/lib/semantic_logger.rb +23 -22
  4. data/lib/semantic_logger/appender.rb +32 -33
  5. data/lib/semantic_logger/appender/async.rb +9 -8
  6. data/lib/semantic_logger/appender/async_batch.rb +4 -2
  7. data/lib/semantic_logger/appender/bugsnag.rb +7 -7
  8. data/lib/semantic_logger/appender/elasticsearch.rb +10 -10
  9. data/lib/semantic_logger/appender/elasticsearch_http.rb +4 -4
  10. data/lib/semantic_logger/appender/file.rb +2 -1
  11. data/lib/semantic_logger/appender/graylog.rb +12 -10
  12. data/lib/semantic_logger/appender/honeybadger.rb +3 -3
  13. data/lib/semantic_logger/appender/http.rb +20 -18
  14. data/lib/semantic_logger/appender/kafka.rb +5 -5
  15. data/lib/semantic_logger/appender/mongodb.rb +6 -6
  16. data/lib/semantic_logger/appender/new_relic.rb +2 -2
  17. data/lib/semantic_logger/appender/rabbitmq.rb +5 -5
  18. data/lib/semantic_logger/appender/sentry.rb +7 -7
  19. data/lib/semantic_logger/appender/splunk.rb +5 -5
  20. data/lib/semantic_logger/appender/splunk_http.rb +2 -2
  21. data/lib/semantic_logger/appender/syslog.rb +12 -12
  22. data/lib/semantic_logger/appender/tcp.rb +5 -5
  23. data/lib/semantic_logger/appender/udp.rb +2 -2
  24. data/lib/semantic_logger/appenders.rb +11 -11
  25. data/lib/semantic_logger/base.rb +16 -14
  26. data/lib/semantic_logger/formatters.rb +11 -11
  27. data/lib/semantic_logger/formatters/base.rb +8 -3
  28. data/lib/semantic_logger/formatters/color.rb +1 -1
  29. data/lib/semantic_logger/formatters/default.rb +18 -5
  30. data/lib/semantic_logger/formatters/fluentd.rb +3 -3
  31. data/lib/semantic_logger/formatters/json.rb +1 -1
  32. data/lib/semantic_logger/formatters/raw.rb +30 -6
  33. data/lib/semantic_logger/formatters/signalfx.rb +10 -9
  34. data/lib/semantic_logger/formatters/syslog.rb +3 -3
  35. data/lib/semantic_logger/formatters/syslog_cee.rb +3 -3
  36. data/lib/semantic_logger/jruby/garbage_collection_logger.rb +4 -2
  37. data/lib/semantic_logger/levels.rb +9 -7
  38. data/lib/semantic_logger/log.rb +17 -11
  39. data/lib/semantic_logger/logger.rb +6 -6
  40. data/lib/semantic_logger/metric/new_relic.rb +3 -3
  41. data/lib/semantic_logger/metric/signalfx.rb +3 -3
  42. data/lib/semantic_logger/metric/statsd.rb +7 -7
  43. data/lib/semantic_logger/processor.rb +7 -5
  44. data/lib/semantic_logger/reporters/minitest.rb +4 -4
  45. data/lib/semantic_logger/semantic_logger.rb +25 -10
  46. data/lib/semantic_logger/subscriber.rb +6 -5
  47. data/lib/semantic_logger/sync.rb +12 -0
  48. data/lib/semantic_logger/sync_processor.rb +44 -0
  49. data/lib/semantic_logger/utils.rb +6 -6
  50. data/lib/semantic_logger/version.rb +1 -1
  51. metadata +4 -2
@@ -1,7 +1,7 @@
1
1
  begin
2
- require 'honeybadger'
2
+ require "honeybadger"
3
3
  rescue LoadError
4
- raise LoadError.new('Gem honeybadger is required for logging purposes. Please add the gem "honeybadger" to your Gemfile.')
4
+ raise LoadError, 'Gem honeybadger is required for logging purposes. Please add the gem "honeybadger" to your Gemfile.'
5
5
  end
6
6
 
7
7
  # Send log messages to honeybadger
@@ -48,7 +48,7 @@ module SemanticLogger
48
48
  context.delete(:exception)
49
49
  ::Honeybadger.notify(log.exception, context)
50
50
  else
51
- message = {
51
+ message = {
52
52
  error_class: context.delete(:name),
53
53
  error_message: context.delete(:message),
54
54
  context: context
@@ -1,8 +1,8 @@
1
- require 'net/http'
2
- require 'uri'
3
- require 'socket'
4
- require 'json'
5
- require 'openssl'
1
+ require "net/http"
2
+ require "uri"
3
+ require "socket"
4
+ require "json"
5
+ require "openssl"
6
6
 
7
7
  # Log to any HTTP(S) server that accepts log messages in JSON form
8
8
  #
@@ -101,27 +101,29 @@ module SemanticLogger
101
101
  @continue_timeout = continue_timeout
102
102
 
103
103
  # On Ruby v2.0 and greater, Net::HTTP.new already uses a persistent connection if the server allows it
104
- @header = {
105
- 'Accept' => 'application/json',
106
- 'Content-Type' => 'application/json',
107
- 'Connection' => 'keep-alive',
108
- 'Keep-Alive' => '300'
104
+ @header = {
105
+ "Accept" => "application/json",
106
+ "Content-Type" => "application/json",
107
+ "Connection" => "keep-alive",
108
+ "Keep-Alive" => "300"
109
109
  }
110
- @header['Content-Encoding'] = 'gzip' if @compress
110
+ @header["Content-Encoding"] = "gzip" if @compress
111
111
 
112
112
  uri = URI.parse(@url)
113
113
  @server = uri.host
114
- raise(ArgumentError, "Invalid format for :url: #{@url.inspect}. Should be similar to: 'http://hostname:port/path'") unless @server
114
+ unless @server
115
+ raise(ArgumentError, "Invalid format for :url: #{@url.inspect}. Should be similar to: 'http://hostname:port/path'")
116
+ end
115
117
 
116
118
  @port = uri.port
117
119
  @username = uri.user if !@username && uri.user
118
120
  @password = uri.password if !@password && uri.password
119
121
  @path = uri.path
120
122
  # Path cannot be empty
121
- @path = '/' if @path == ''
123
+ @path = "/" if @path == ""
122
124
 
123
- if uri.scheme == 'https'
124
- @ssl_options[:use_ssl] = true
125
+ if uri.scheme == "https"
126
+ @ssl_options[:use_ssl] = true
125
127
  @ssl_options[:verify_mode] ||= OpenSSL::SSL::VERIFY_PEER
126
128
  @port ||= HTTP.https_default_port
127
129
  else
@@ -205,16 +207,16 @@ module SemanticLogger
205
207
  end
206
208
  request.basic_auth(@username, @password) if @username
207
209
  response = @http.request(request)
208
- if response.code == '200' || response.code == '201'
210
+ if response.code == "200" || response.code == "201"
209
211
  true
210
212
  else
211
213
  # Failures are logged to the global semantic logger failsafe logger (Usually stderr or file)
212
214
  logger.error("Bad HTTP response from: #{url} code: #{response.code}, #{response.body}")
213
215
  false
214
216
  end
215
- rescue RuntimeError => exc
217
+ rescue RuntimeError => e
216
218
  reopen
217
- raise exc
219
+ raise e
218
220
  end
219
221
  end
220
222
  end
@@ -1,10 +1,10 @@
1
1
  begin
2
- require 'kafka'
2
+ require "kafka"
3
3
  rescue LoadError
4
- raise LoadError.new('Gem ruby-kafka is required for logging to Elasticsearch. Please add the gem "ruby-kafka" to your Gemfile.')
4
+ raise LoadError, 'Gem ruby-kafka is required for logging to Elasticsearch. Please add the gem "ruby-kafka" to your Gemfile.'
5
5
  end
6
6
 
7
- require 'date'
7
+ require "date"
8
8
 
9
9
  # Forward all log messages to Apache Kafka.
10
10
  #
@@ -115,9 +115,9 @@ module SemanticLogger
115
115
  # metrics: [Boolean]
116
116
  # Send metrics only events to kafka.
117
117
  # Default: true
118
- def initialize(seed_brokers:, client_id: 'semantic-logger', connect_timeout: nil, socket_timeout: nil,
118
+ def initialize(seed_brokers:, client_id: "semantic-logger", connect_timeout: nil, socket_timeout: nil,
119
119
  ssl_ca_cert: nil, ssl_client_cert: nil, ssl_client_cert_key: nil,
120
- topic: 'log_messages', partition: nil, partition_key: nil, key: nil,
120
+ topic: "log_messages", partition: nil, partition_key: nil, key: nil,
121
121
  delivery_threshold: 100, delivery_interval: 10,
122
122
  metrics: true, **args, &block)
123
123
 
@@ -1,8 +1,8 @@
1
- require 'socket'
1
+ require "socket"
2
2
  begin
3
- require 'mongo'
3
+ require "mongo"
4
4
  rescue LoadError
5
- raise LoadError.new('Gem mongo is required for logging to MongoDB. Please add the gem "mongo" v2.0 or greater to your Gemfile.')
5
+ raise LoadError, 'Gem mongo is required for logging to MongoDB. Please add the gem "mongo" v2.0 or greater to your Gemfile.'
6
6
  end
7
7
 
8
8
  module SemanticLogger
@@ -104,9 +104,9 @@ module SemanticLogger
104
104
  # Name of this application to appear in log messages.
105
105
  # Default: SemanticLogger.application
106
106
  def initialize(uri:,
107
- collection_name: 'semantic_logger',
107
+ collection_name: "semantic_logger",
108
108
  write_concern: 0,
109
- collection_size: 1024 ** 3,
109
+ collection_size: 1024**3,
110
110
  collection_max: nil,
111
111
  **args,
112
112
  &block)
@@ -118,7 +118,7 @@ module SemanticLogger
118
118
  size: collection_size,
119
119
  write: {w: write_concern}
120
120
  }
121
- @options[:max] = collection_max if collection_max
121
+ @options[:max] = collection_max if collection_max
122
122
 
123
123
  reopen
124
124
 
@@ -1,7 +1,7 @@
1
1
  begin
2
- require 'newrelic_rpm'
2
+ require "newrelic_rpm"
3
3
  rescue LoadError
4
- raise LoadError.new('Gem newrelic_rpm is required for logging to New Relic. Please add the gem "newrelic_rpm" to your Gemfile.')
4
+ raise LoadError, 'Gem newrelic_rpm is required for logging to New Relic. Please add the gem "newrelic_rpm" to your Gemfile.'
5
5
  end
6
6
 
7
7
  # Send log messages to NewRelic
@@ -1,7 +1,7 @@
1
1
  begin
2
- require 'bunny'
2
+ require "bunny"
3
3
  rescue LoadError
4
- raise LoadError.new('Gem bunny is required for logging to RabbitMQ. Please add the gem "bunny" to your Gemfile.')
4
+ raise LoadError, 'Gem bunny is required for logging to RabbitMQ. Please add the gem "bunny" to your Gemfile.'
5
5
  end
6
6
 
7
7
  # Forward all log messages to RabbitMQ.
@@ -14,7 +14,7 @@ end
14
14
  # # Name of the queue in RabbitMQ where to publish the logs. This queue will be bound to "amqp.direct" exchange.
15
15
  # queue: 'semantic_logger',
16
16
  #
17
- # # This host will be used for RabbitMQ connection.
17
+ # # This host will be used for RabbitMQ connection.
18
18
  # # NOTE this is different than :host option which is used by the logger directly.
19
19
  # rabbitmq_host: '127.0.0.1',
20
20
  #
@@ -63,7 +63,7 @@ module SemanticLogger
63
63
  # RabbitMQ Parameters:
64
64
  #
65
65
  # rabbitmq_host: [String]
66
- # Host for AMQP connection. in Bunny this is called :host but here it has
66
+ # Host for AMQP connection. in Bunny this is called :host but here it has
67
67
  # been remapped to avoid conflicting with SemanticLogger's :host param.
68
68
  # Default: localhost
69
69
  #
@@ -76,7 +76,7 @@ module SemanticLogger
76
76
  # Default: nil
77
77
  #
78
78
  # more parameters supported by Bunny: http://rubybunny.info/articles/connecting.html
79
- def initialize(queue_name: 'semantic_logger', rabbitmq_host: nil, metrics: false, **args, &block)
79
+ def initialize(queue_name: "semantic_logger", rabbitmq_host: nil, metrics: false, **args, &block)
80
80
  @queue_name = queue_name
81
81
  @rabbitmq_args = args.dup
82
82
  @rabbitmq_args[:host] = rabbitmq_host
@@ -1,7 +1,7 @@
1
1
  begin
2
- require 'sentry-raven'
2
+ require "sentry-raven"
3
3
  rescue LoadError
4
- raise LoadError.new('Gem sentry-raven is required for logging purposes. Please add the gem "sentry-raven" to your Gemfile.')
4
+ raise LoadError, 'Gem sentry-raven is required for logging purposes. Please add the gem "sentry-raven" to your Gemfile.'
5
5
  end
6
6
 
7
7
  # Send log messages to sentry
@@ -46,12 +46,12 @@ module SemanticLogger
46
46
  # Send an error notification to sentry
47
47
  def log(log)
48
48
  # Ignore logs coming from Raven itself
49
- return false if log.name == 'Raven'
49
+ return false if log.name == "Raven"
50
50
 
51
- context = formatter.call(log, self)
52
- user = context.delete(:user)
53
- tags = context.delete(:tags)
54
- attrs = {
51
+ context = formatter.call(log, self)
52
+ user = context.delete(:user)
53
+ tags = context.delete(:tags)
54
+ attrs = {
55
55
  level: context.delete(:level),
56
56
  extra: context
57
57
  }
@@ -1,7 +1,7 @@
1
1
  begin
2
- require 'splunk-sdk-ruby'
2
+ require "splunk-sdk-ruby"
3
3
  rescue LoadError
4
- raise LoadError.new('Gem splunk-sdk-ruby is required for logging to Splunk. Please add the gem "splunk-sdk-ruby" to your Gemfile.')
4
+ raise LoadError, 'Gem splunk-sdk-ruby is required for logging to Splunk. Please add the gem "splunk-sdk-ruby" to your Gemfile.'
5
5
  end
6
6
 
7
7
  # Splunk log appender.
@@ -88,7 +88,7 @@ module SemanticLogger
88
88
  # regular expression. All other messages will be ignored.
89
89
  # Proc: Only include log messages where the supplied Proc returns true
90
90
  # The Proc must return true or false.
91
- def initialize(index: 'main', source_type: nil, **args, &block)
91
+ def initialize(index: "main", source_type: nil, **args, &block)
92
92
  @index = index
93
93
  @source_type = source_type
94
94
 
@@ -120,7 +120,7 @@ module SemanticLogger
120
120
  def call(log, logger)
121
121
  h = SemanticLogger::Formatters::Raw.new.call(log, logger)
122
122
  h.delete(:time)
123
- message = {
123
+ message = {
124
124
  source: logger.application,
125
125
  host: logger.host,
126
126
  time: log.time.utc.to_f,
@@ -128,7 +128,7 @@ module SemanticLogger
128
128
  event: h
129
129
  }
130
130
  message[:environment] = logger.environment if logger.environment
131
- message[:sourcetype] = source_type if source_type
131
+ message[:sourcetype] = source_type if source_type
132
132
  message
133
133
  end
134
134
  end
@@ -1,4 +1,4 @@
1
- require 'json'
1
+ require "json"
2
2
  # Splunk log appender using the Splunk HTTP(S) listener.
3
3
  #
4
4
  # Use the newer, faster and more complete JSON over HTTP interface for Splunk.
@@ -81,7 +81,7 @@ module SemanticLogger
81
81
  super(compress: compress, **args, &block)
82
82
 
83
83
  # Put splunk auth token in the header of every HTTP post.
84
- @header['Authorization'] = "Splunk #{token}"
84
+ @header["Authorization"] = "Splunk #{token}"
85
85
  end
86
86
 
87
87
  # Returns [String] JSON to send to Splunk.
@@ -1,6 +1,6 @@
1
- require 'syslog'
2
- require 'uri'
3
- require 'socket'
1
+ require "syslog"
2
+ require "uri"
3
+ require "socket"
4
4
  # Send log messages to local syslog, or remote syslog servers over TCP or UDP.
5
5
  #
6
6
  # Example:
@@ -119,7 +119,7 @@ module SemanticLogger
119
119
  # Example:
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
- def initialize(url: 'syslog://localhost',
122
+ def initialize(url: "syslog://localhost",
123
123
  facility: ::Syslog::LOG_USER,
124
124
  level_map: SemanticLogger::Formatters::Syslog::LevelMap.new,
125
125
  options: ::Syslog::LOG_PID | ::Syslog::LOG_CONS,
@@ -132,10 +132,10 @@ module SemanticLogger
132
132
  @level_map = level_map
133
133
  @url = url
134
134
  uri = URI(@url)
135
- @server = uri.host || 'localhost'
135
+ @server = uri.host || "localhost"
136
136
  @protocol = (uri.scheme || :syslog).to_sym
137
137
  @port = uri.port || 514
138
- @server = 'localhost' if @protocol == :syslog
138
+ @server = "localhost" if @protocol == :syslog
139
139
  @tcp_client_options = tcp_client
140
140
 
141
141
  raise "Unknown protocol #{@protocol}!" unless %i[syslog tcp udp].include?(@protocol)
@@ -143,17 +143,17 @@ module SemanticLogger
143
143
  # The syslog_protocol gem is required when logging over TCP or UDP.
144
144
  if %i[tcp udp].include?(@protocol)
145
145
  begin
146
- require 'syslog_protocol'
146
+ require "syslog_protocol"
147
147
  rescue LoadError
148
- raise LoadError.new('Missing gem: syslog_protocol. This gem is required when logging over TCP or UDP. To fix this error: gem install syslog_protocol')
148
+ raise LoadError, "Missing gem: syslog_protocol. This gem is required when logging over TCP or UDP. To fix this error: gem install syslog_protocol"
149
149
  end
150
150
 
151
151
  # The net_tcp_client gem is required when logging over TCP.
152
152
  if protocol == :tcp
153
153
  begin
154
- require 'net/tcp_client'
154
+ require "net/tcp_client"
155
155
  rescue LoadError
156
- raise LoadError.new('Missing gem: net_tcp_client. This gem is required when logging over TCP. To fix this error: gem install net_tcp_client')
156
+ raise LoadError, "Missing gem: net_tcp_client. This gem is required when logging over TCP. To fix this error: gem install net_tcp_client"
157
157
  end
158
158
  end
159
159
  end
@@ -171,7 +171,7 @@ module SemanticLogger
171
171
  ::Syslog.send(method, application, options, facility)
172
172
  when :tcp
173
173
  @tcp_client_options[:server] = "#{@server}:#{@port}"
174
- @remote_syslog = Net::TCPClient.new(@tcp_client_options)
174
+ @remote_syslog = Net::TCPClient.new(**@tcp_client_options)
175
175
  # Use the local logger for @remote_syslog so errors with the remote logger can be recorded locally.
176
176
  @remote_syslog.logger = logger
177
177
  when :udp
@@ -186,7 +186,7 @@ module SemanticLogger
186
186
  case @protocol
187
187
  when :syslog
188
188
  # Since the Ruby Syslog API supports sprintf format strings, double up all existing '%'
189
- message = formatter.call(log, self).gsub '%', '%%'
189
+ message = formatter.call(log, self).gsub "%", "%%"
190
190
  ::Syslog.log @level_map[log.level], message
191
191
  when :tcp
192
192
  @remote_syslog.retry_on_connection_failure { @remote_syslog.write("#{formatter.call(log, self)}\r\n") }
@@ -1,10 +1,10 @@
1
1
  begin
2
- require 'net/tcp_client'
2
+ require "net/tcp_client"
3
3
  rescue LoadError
4
- raise LoadError.new('Gem net_tcp_client is required for logging over TCP. Please add the gem "net_tcp_client" to your Gemfile.')
4
+ raise LoadError, 'Gem net_tcp_client is required for logging over TCP. Please add the gem "net_tcp_client" to your Gemfile.'
5
5
  end
6
6
 
7
- raise 'Net::TCPClient v2.0 or greater is required to log over TCP' unless Net::TCPClient::VERSION.to_f >= 2.0
7
+ raise "Net::TCPClient v2.0 or greater is required to log over TCP" unless Net::TCPClient::VERSION.to_f >= 2.0
8
8
 
9
9
  module SemanticLogger
10
10
  module Appender
@@ -189,7 +189,7 @@ module SemanticLogger
189
189
 
190
190
  # Use the internal logger so that errors with remote logging are only written locally.
191
191
  Net::TCPClient.logger = logger
192
- Net::TCPClient.logger.name = 'Net::TCPClient'
192
+ Net::TCPClient.logger.name = "Net::TCPClient"
193
193
 
194
194
  super(level: level, formatter: formatter, filter: filter, application: application, environment: environment, host: host, &block)
195
195
  reopen
@@ -198,7 +198,7 @@ module SemanticLogger
198
198
  # After forking an active process call #reopen to re-open the handles to resources.
199
199
  def reopen
200
200
  close
201
- @tcp_client = Net::TCPClient.new(@tcp_client_args)
201
+ @tcp_client = Net::TCPClient.new(**@tcp_client_args)
202
202
  end
203
203
 
204
204
  # Write the log using the specified protocol and server.
@@ -1,4 +1,4 @@
1
- require 'socket'
1
+ require "socket"
2
2
  module SemanticLogger
3
3
  module Appender
4
4
  # UDP log appender.
@@ -74,7 +74,7 @@ module SemanticLogger
74
74
  def reopen
75
75
  close
76
76
  @socket = UDPSocket.new
77
- host, port = server.split(':')
77
+ host, port = server.split(":")
78
78
  @socket.connect(host, port.to_i)
79
79
  end
80
80
 
@@ -18,8 +18,8 @@ module SemanticLogger
18
18
  each do |appender|
19
19
  begin
20
20
  appender.log(log) if appender.should_log?(log)
21
- rescue Exception => exc
22
- logger.error "Failed to log to appender: #{appender.name}", exc
21
+ rescue Exception => e
22
+ logger.error "Failed to log to appender: #{appender.name}", e
23
23
  end
24
24
  end
25
25
  end
@@ -29,11 +29,11 @@ module SemanticLogger
29
29
  begin
30
30
  logger.trace "Flushing appender: #{appender.name}"
31
31
  appender.flush
32
- rescue Exception => exc
33
- logger.error "Failed to flush appender: #{appender.name}", exc
32
+ rescue Exception => e
33
+ logger.error "Failed to flush appender: #{appender.name}", e
34
34
  end
35
35
  end
36
- logger.trace 'All appenders flushed'
36
+ logger.trace "All appenders flushed"
37
37
  end
38
38
 
39
39
  def close
@@ -43,11 +43,11 @@ module SemanticLogger
43
43
  appender.flush
44
44
  appender.close
45
45
  delete(appender)
46
- rescue Exception => exc
47
- logger.error "Failed to close appender: #{appender.name}", exc
46
+ rescue Exception => e
47
+ logger.error "Failed to close appender: #{appender.name}", e
48
48
  end
49
49
  end
50
- logger.trace 'All appenders closed and removed from appender list'
50
+ logger.trace "All appenders closed and removed from appender list"
51
51
  end
52
52
 
53
53
  # After a fork the appender thread is not running, start it if it is not running.
@@ -58,11 +58,11 @@ module SemanticLogger
58
58
 
59
59
  logger.trace "Reopening appender: #{appender.name}"
60
60
  appender.reopen
61
- rescue Exception => exc
62
- logger.error "Failed to re-open appender: #{appender.name}", exc
61
+ rescue Exception => e
62
+ logger.error "Failed to re-open appender: #{appender.name}", e
63
63
  end
64
64
  end
65
- logger.trace 'All appenders re-opened'
65
+ logger.trace "All appenders re-opened"
66
66
  end
67
67
  end
68
68
  end