logstash-logger 0.16.0 → 0.17.0

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
  SHA1:
3
- metadata.gz: c7f6fe8282858937026c5c9747835e68bd16390f
4
- data.tar.gz: 8de048a5dab4c3a46e1f74fd7b192cc7e82ac58f
3
+ metadata.gz: 1de31545fcd13dd8e1d6a5c3bcd4d69b29e4579a
4
+ data.tar.gz: c45861aa001389601e4ef8f4352879d58d0758fb
5
5
  SHA512:
6
- metadata.gz: c6e11150819e39e4ffb061160e332570a8d461974b88d6a933b6a9370c26bccde71a4f53942ddfc29b12e40f2f952df659c291a682dabd7a1a9f944430e02d37
7
- data.tar.gz: 62077f3594cf26f2e92af9576bbd392e6059f7c141f5acc7d61e6ea850491f37eaaf642138d343c7b9d22436f8608ca3b9b3163941e9db0b7ce562dc4bae918a
6
+ metadata.gz: 308d6259229cc7aa957cdf5390ac3f7f244b61f6eb0c181498dce44eec7dad74326efb213561824e98349d120c5f39dbe4ef0a3a32919bea292b622a44643833
7
+ data.tar.gz: 8a746761ac4c20717bb73f21117569c038438433c6fab5200175e6d0e9b71a752e7f7cecf3e44148f7e1962e344948b97485ff6be53548bbc8bf6d3376bb1a8a
@@ -29,7 +29,9 @@ matrix:
29
29
  - rvm: 2.3.1
30
30
  gemfile: gemfiles/rails_3.2.gemfile
31
31
  allow_failures:
32
- -rvm: rbx-3.9
32
+ - rvm: rbx-3.9
33
33
  sudo: false
34
34
  cache: bundler
35
-
35
+ addons:
36
+ code_climate:
37
+ repo_token: 4d712355fa2863c0f33f413eeede4e52cc221c4bc989a692d97574b1f6010b69
@@ -1,3 +1,8 @@
1
+ ## 0.17.0
2
+ - Support for logger silencing. [#87](https://github.com/dwbutler/logstash-logger/pull/87)
3
+ - Fixes Rails 5 support. [#86](https://github.com/dwbutler/logstash-logger/issues/86)
4
+ - Fixes support for tagged logging in MultiLogger.
5
+
1
6
  ## 0.16.0
2
7
 
3
8
  This release is focused on improving the reliability of LogStashLogger.
data/Gemfile CHANGED
@@ -3,3 +3,4 @@ source 'https://rubygems.org'
3
3
  gemspec
4
4
 
5
5
  gem 'codecov', require: false, group: :test
6
+ gem "codeclimate-test-reporter", group: :test, require: nil
data/README.md CHANGED
@@ -286,6 +286,18 @@ change the error logger by setting `LogStashLogger.configuration.default_error_l
286
286
  your own logger object in the `:error_logger` configuration key when
287
287
  instantiating a LogStashLogger.
288
288
 
289
+ ## Logger Silencing
290
+
291
+ LogStashLogger provides support for Rails-style logger silencing. The
292
+ implementation was extracted from Rails, but has no dependencies, so it can be
293
+ used outside of a Rails app. The interface is the same as in Rails:
294
+
295
+ ```ruby
296
+ logger.silence(temporary_level) do
297
+ ...
298
+ end
299
+ ```
300
+
289
301
  ## Rails Integration
290
302
 
291
303
  Verified to work with both Rails 3, 4, and 5.
@@ -583,7 +595,7 @@ of the logstash listener. There is no known fix at this time. See [#43](https://
583
595
  for more information.
584
596
 
585
597
  ### Errno::EMSGSIZE - Message too long
586
- A known drawback of using UDP is its limit on total message size. To workaround
598
+ A known drawback of using TCP or UDP is the 65535 byte limit on total message size. To workaround
587
599
  this issue, you will have to truncate the message by setting the max message size:
588
600
 
589
601
  ```ruby
@@ -592,6 +604,10 @@ LogStashLogger.configure do |config|
592
604
  end
593
605
  ```
594
606
 
607
+ This will truncate only the `message` field of the LogStash Event. So make sure
608
+ you set the max message size significantly less than 65535 bytes to make room
609
+ for other fields.
610
+
595
611
  ## Breaking changes
596
612
 
597
613
  ### Version 0.5+
@@ -3,6 +3,7 @@
3
3
  source "https://rubygems.org"
4
4
 
5
5
  gem "codecov", :require => false, :group => :test
6
+ gem "codeclimate-test-reporter", :group => :test, :require => nil
6
7
  gem "rails", "~> 3.2.18"
7
8
 
8
9
  gemspec :path => "../"
@@ -3,6 +3,7 @@
3
3
  source "https://rubygems.org"
4
4
 
5
5
  gem "codecov", :require => false, :group => :test
6
+ gem "codeclimate-test-reporter", :group => :test, :require => nil
6
7
  gem "rails", "~> 4.0.0"
7
8
 
8
9
  gemspec :path => "../"
@@ -3,6 +3,7 @@
3
3
  source "https://rubygems.org"
4
4
 
5
5
  gem "codecov", :require => false, :group => :test
6
+ gem "codeclimate-test-reporter", :group => :test, :require => nil
6
7
  gem "rails", "~> 4.1.1"
7
8
 
8
9
  gemspec :path => "../"
@@ -3,6 +3,7 @@
3
3
  source "https://rubygems.org"
4
4
 
5
5
  gem "codecov", :require => false, :group => :test
6
+ gem "codeclimate-test-reporter", :group => :test, :require => nil
6
7
  gem "rails", "~> 4.2.0"
7
8
 
8
9
  gemspec :path => "../"
@@ -3,6 +3,7 @@
3
3
  source "https://rubygems.org"
4
4
 
5
5
  gem "codecov", :require => false, :group => :test
6
+ gem "codeclimate-test-reporter", :group => :test, :require => nil
6
7
  gem "rails", "~> 5.0.0"
7
8
 
8
9
  gemspec :path => "../"
@@ -1,5 +1,6 @@
1
1
  require 'logger'
2
2
  require 'logstash-logger/tagged_logging'
3
+ require 'logstash-logger/silenced_logging'
3
4
 
4
5
  module LogStashLogger
5
6
  autoload :MultiLogger, 'logstash-logger/multi_logger'
@@ -68,6 +69,7 @@ module LogStashLogger
68
69
  logger.instance_variable_set(:@device, device)
69
70
  logger.extend(self)
70
71
  logger.extend(TaggedLogging)
72
+ logger.extend(SilencedLogging)
71
73
  end
72
74
  end
73
75
 
@@ -87,5 +89,6 @@ module LogStashLogger
87
89
 
88
90
  logger.extend(self)
89
91
  logger.extend(TaggedLogging)
92
+ logger.extend(SilencedLogging)
90
93
  end
91
94
  end
@@ -1,6 +1,9 @@
1
1
  # Adapted from https://github.com/ffmike/multilogger
2
2
  module LogStashLogger
3
3
  class MultiLogger < ::Logger
4
+ # Array of Loggers to be logged to. These can be anything that acts reasonably like a Logger.
5
+ attr_accessor :loggers
6
+
4
7
  def level=(value)
5
8
  super
6
9
  @loggers.each do |logger|
@@ -28,8 +31,39 @@ module LogStashLogger
28
31
  end
29
32
  end
30
33
 
31
- # Array of Loggers to be logged to. These can be anything that acts reasonably like a Logger.
32
- attr_accessor :loggers
34
+ def silence(temporary_level = ::Logger::ERROR, &block)
35
+ silenceable_loggers = @loggers.select do |logger|
36
+ logger.respond_to?(:silence)
37
+ end
38
+
39
+ silence_loggers(temporary_level, silenceable_loggers, &block)
40
+ end
41
+
42
+ def silence_loggers(temporary_level = ::Logger::ERROR, silenceable_loggers, &block)
43
+ return yield(self) if silenceable_loggers.empty?
44
+
45
+ silenceable_loggers.shift.silence(temporary_level) do
46
+ silence_loggers(temporary_level, silenceable_loggers, &block)
47
+ end
48
+ end
49
+ private :silence_loggers
50
+
51
+ def tagged(*tags, &block)
52
+ taggable_loggers = @loggers.select do |logger|
53
+ logger.respond_to?(:tagged)
54
+ end
55
+
56
+ tag_loggers(tags, taggable_loggers, &block)
57
+ end
58
+
59
+ def tag_loggers(tags, taggable_loggers, &block)
60
+ return yield(self) if taggable_loggers.empty?
61
+
62
+ taggable_loggers.shift.tagged(*tags) do
63
+ tag_loggers(tags, taggable_loggers, &block)
64
+ end
65
+ end
66
+ private :tag_loggers
33
67
 
34
68
  # Any method not defined on standard Logger class, just send it on to anyone who will listen
35
69
  def method_missing(name, *args, &block)
@@ -0,0 +1,83 @@
1
+ # Adapted from:
2
+ # https://github.com/rails/activerecord-session_store/blob/master/lib/active_record/session_store/extension/logger_silencer.rb
3
+ # https://github.com/rails/rails/pull/16885
4
+
5
+ require 'thread'
6
+
7
+ # Add support for Rails-style logger silencing. Thread-safe and no dependencies.
8
+ #
9
+ # Setup:
10
+ # logger = Logger.new(STDOUT)
11
+ # logger.extend(LogStashLogger::SilencedLogging)
12
+ #
13
+ # Usage:
14
+ #
15
+ # logger.silence do
16
+ # ...
17
+ # end
18
+ #
19
+ module LogStashLogger
20
+ module SilencedLogging
21
+ def self.extended(logger)
22
+ class << logger
23
+ attr_accessor :silencer
24
+ alias_method :level_without_threadsafety, :level
25
+ alias_method :level, :level_with_threadsafety
26
+ alias_method :add_without_threadsafety, :add
27
+ alias_method :add, :add_with_threadsafety
28
+
29
+ Logger::Severity.constants.each do |severity|
30
+ instance_eval <<-EOT, __FILE__, __LINE__ + 1
31
+ def #{severity.downcase}? # def debug?
32
+ Logger::#{severity} >= level # DEBUG >= level
33
+ end # end
34
+ EOT
35
+ end
36
+ end
37
+
38
+ logger.instance_eval do
39
+ self.silencer = true
40
+ end
41
+ end
42
+
43
+ def thread_level
44
+ Thread.current[thread_hash_level_key]
45
+ end
46
+
47
+ def thread_level=(level)
48
+ Thread.current[thread_hash_level_key] = level
49
+ end
50
+
51
+ def level_with_threadsafety
52
+ thread_level || level_without_threadsafety
53
+ end
54
+
55
+ def add_with_threadsafety(severity, message = nil, progname = nil, &block)
56
+ if (defined?(@logdev) && @logdev.nil?) || (severity || UNKNOWN) < level
57
+ true
58
+ else
59
+ add_without_threadsafety(severity, message, progname, &block)
60
+ end
61
+ end
62
+
63
+ # Silences the logger for the duration of the block.
64
+ def silence(temporary_level = Logger::ERROR)
65
+ if silencer
66
+ begin
67
+ self.thread_level = temporary_level
68
+ yield self
69
+ ensure
70
+ self.thread_level = nil
71
+ end
72
+ else
73
+ yield self
74
+ end
75
+ end
76
+
77
+ private
78
+
79
+ def thread_hash_level_key
80
+ @thread_hash_level_key ||= :"ThreadSafeLogger##{object_id}@level"
81
+ end
82
+ end
83
+ end
@@ -1,3 +1,3 @@
1
1
  module LogStashLogger
2
- VERSION = "0.16.0"
2
+ VERSION = "0.17.0"
3
3
  end
@@ -27,6 +27,33 @@ describe LogStashLogger::MultiLogger do
27
27
  expect(logger).to receive(:info).with("test")
28
28
  end
29
29
 
30
- multi_logger.info("test")
30
+ subject.info("test")
31
+ end
32
+
33
+ it "supports silenced logging" do
34
+ subject.loggers.each do |logger|
35
+ expect(logger).to receive(:silence).and_call_original
36
+ expect(logger.device).to receive(:write).once
37
+ end
38
+
39
+ subject.silence(::Logger::WARN) do |logger|
40
+ expect(logger).to eq(subject)
41
+ logger.info 'info'
42
+ logger.warn 'warning'
43
+ end
44
+ end
45
+
46
+ it "supports tagged logging" do
47
+ subject.loggers.each do |logger|
48
+ expect(logger).to receive(:tagged).with('tag').and_call_original
49
+ expect(logger.device).to receive(:write) do |event_string|
50
+ event = JSON.parse(event_string)
51
+ expect(event['tags']).to match_array(['tag'])
52
+ end
53
+ end
54
+
55
+ subject.tagged('tag') do |logger|
56
+ logger.info 'test'
57
+ end
31
58
  end
32
59
  end
@@ -0,0 +1,31 @@
1
+ require 'logstash-logger'
2
+
3
+ describe LogStashLogger do
4
+ include_context 'logger'
5
+
6
+ describe "silenced logging" do
7
+
8
+ it "yields the logger" do
9
+ logger.silence do |yielded|
10
+ expect(yielded).to eq(logger)
11
+ end
12
+ end
13
+
14
+ it "silences any message below ERROR level by default" do
15
+ logger.silence do
16
+ expect(logger.device).to receive(:write).once
17
+ logger.info("info")
18
+ logger.warn("warning")
19
+ logger.error("error")
20
+ end
21
+ end
22
+
23
+ it "takes a custom log level to silence to" do
24
+ logger.silence(::Logger::WARN) do
25
+ expect(logger.device).to receive(:write).once
26
+ logger.info("info")
27
+ logger.warn("warning")
28
+ end
29
+ end
30
+ end
31
+ end
@@ -7,6 +7,9 @@ if ENV['CI']=='true'
7
7
  SimpleCov.formatter = SimpleCov::Formatter::Codecov
8
8
  end
9
9
 
10
+ require "codeclimate-test-reporter"
11
+ CodeClimate::TestReporter.start
12
+
10
13
  RSpec.configure do |config|
11
14
  config.order = "random"
12
15
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logstash-logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.16.0
4
+ version: 0.17.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Butler
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-13 00:00:00.000000000 Z
11
+ date: 2016-07-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: logstash-event
@@ -198,6 +198,7 @@ files:
198
198
  - lib/logstash-logger/logger.rb
199
199
  - lib/logstash-logger/multi_logger.rb
200
200
  - lib/logstash-logger/railtie.rb
201
+ - lib/logstash-logger/silenced_logging.rb
201
202
  - lib/logstash-logger/tagged_logging.rb
202
203
  - lib/logstash-logger/version.rb
203
204
  - logstash-logger.gemspec
@@ -235,6 +236,7 @@ files:
235
236
  - spec/logger_spec.rb
236
237
  - spec/multi_logger_spec.rb
237
238
  - spec/rails_spec.rb
239
+ - spec/silenced_logging_spec.rb
238
240
  - spec/spec_helper.rb
239
241
  - spec/syslog_spec.rb
240
242
  - spec/tagged_logging_spec.rb
@@ -288,6 +290,7 @@ test_files:
288
290
  - spec/logger_spec.rb
289
291
  - spec/multi_logger_spec.rb
290
292
  - spec/rails_spec.rb
293
+ - spec/silenced_logging_spec.rb
291
294
  - spec/spec_helper.rb
292
295
  - spec/syslog_spec.rb
293
296
  - spec/tagged_logging_spec.rb