buffering_logger 2.0.5 → 2.0.6

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
- SHA1:
3
- metadata.gz: 243e3e793bcd45fdf09c8ae4697546f7ef1805c4
4
- data.tar.gz: 4ec124f406edcca8860cfc17e8d29c6e019bebf4
2
+ SHA256:
3
+ metadata.gz: 5564a270f8f8b903262a250478cfca69a7854b7943aed6e4353b7589c5bf4a3a
4
+ data.tar.gz: 74b1b8ac3d1d3a321c8064427966b47253c98968ba69f12fdd6e2b741989e924
5
5
  SHA512:
6
- metadata.gz: 7810a492bdd74a1468a30069ff61922893fe970522d907926385b413ce673452260a33d675703d0f505a1b115f4ee9ceeba079b009292dff056e914c3ce6fa17
7
- data.tar.gz: 294724ac15d92bd4337c75457fa1a72daee9202899c0fd93d86b73c921bb566db65ea9d9c84419183a8bbb9cec4e0a34a7bcebb7fde35dce04b28181da388663
6
+ metadata.gz: 29ed01af72ea3b3bc85446c3de71a48f2a48eed7716ff1a23c183fb8d5ba9ac9903fe969a04d8c337877562a9a0070ed1c245c27993228d05d9ae95cd876bea5
7
+ data.tar.gz: ef3dae1d2d911160ba538a34b03afff8a1101dbdfde68f02530547a09d4290742caa60b7b9bac874ad3c0163c830d21df1cf595ed8a0c4ee1e44c8636d4b1c16
@@ -1,6 +1,6 @@
1
+ module BufferingLogger
2
+ end
3
+
1
4
  require 'buffering_logger/logger'
2
5
  require 'buffering_logger/buffer'
3
6
  require 'buffering_logger/rack_buffer'
4
-
5
- module BufferingLogger
6
- end
@@ -1,66 +1,65 @@
1
1
  # Buffer is used to wrap the logger's logdev to accomplish buffering.
2
2
  # For the purposes of the Logger class a LogDevice only needs to implement
3
3
  # #write and #close. We add #buffered as well.
4
- module BufferingLogger
5
- class Buffer
6
- def initialize(logdev)
7
- @logdev = logdev
8
- @mutex = Mutex.new
9
- end
4
+ class BufferingLogger::Buffer
10
5
 
11
- # buffers during the block and then flushes.
12
- # returns the value of the block.
13
- def buffered(transform: nil)
14
- @buffering = true
15
- yield
16
- ensure
17
- @buffering = false
18
- flush(transform: transform)
19
- end
6
+ def initialize(logdev)
7
+ @logdev = logdev
8
+ @mutex = Mutex.new
9
+ end
20
10
 
21
- def write(msg)
22
- if @buffering
23
- (buffer || create_buffer).write(msg)
24
- else
25
- @logdev.write(msg)
26
- end
27
- end
11
+ # buffers during the block and then flushes.
12
+ # returns the value of the block.
13
+ def buffered(transform: nil)
14
+ @buffering = true
15
+ yield
16
+ ensure
17
+ @buffering = false
18
+ flush(transform: transform)
19
+ end
28
20
 
29
- def close
30
- @mutex.synchronize do
31
- @logdev.close
32
- end
21
+ def write(msg)
22
+ if @buffering
23
+ buffer.write(msg)
24
+ else
25
+ logdev_write(msg)
33
26
  end
27
+ end
34
28
 
35
- private
29
+ def close
30
+ logdev_close
31
+ end
36
32
 
37
- def flush(transform: nil)
38
- if buffer && buffer.length > 0
39
- @mutex.synchronize do
40
- msg = buffer.string
41
- msg = transform.call(msg) if transform
42
- @logdev.write(msg)
43
- end
44
- end
45
- ensure
46
- unset_buffer if buffer
47
- end
33
+ private
48
34
 
49
- def buffer
50
- Thread.current[buffer_id]
35
+ def flush(transform: nil)
36
+ if buffer && buffer.length > 0
37
+ msg = buffer.string
38
+ msg = transform.call(msg) if transform
39
+ logdev_write(msg)
51
40
  end
41
+ ensure
42
+ buffer.reopen('')
43
+ end
52
44
 
53
- def create_buffer
54
- Thread.current[buffer_id] = StringIO.new
45
+ def logdev_write(msg)
46
+ @mutex.synchronize do
47
+ @logdev.write(msg)
55
48
  end
49
+ end
56
50
 
57
- def unset_buffer
58
- Thread.current[buffer_id] = nil
51
+ def logdev_close
52
+ @mutex.synchronize do
53
+ @logdev.close
59
54
  end
55
+ end
60
56
 
61
- def buffer_id
62
- "buffering_logger_#{object_id}_buffer"
63
- end
57
+ def buffer
58
+ Thread.current[buffer_id] ||= StringIO.new
59
+ end
64
60
 
61
+ def buffer_id
62
+ "buffering_logger_#{object_id}_buffer"
65
63
  end
64
+
66
65
  end
@@ -4,36 +4,39 @@ require 'logger'
4
4
  # logger.buffered { logger.info 'hi'; logger.info 'goodbye' }
5
5
  # Buffering is implemented by wrapping the logger @logdev object with a Buffer.
6
6
 
7
- module BufferingLogger
8
- class Logger < ::Logger
7
+ class BufferingLogger::Logger < ::Logger
9
8
 
10
- attr_accessor :default_transform
11
- attr_reader :raw_log_device
9
+ attr_accessor :default_transform
10
+ attr_reader :raw_log_device
12
11
 
13
- def initialize(logdev, shift_age: 0, shift_size: 1048576)
14
- @shift_age, @shift_size = shift_age, shift_size
15
- @opened_logdev = false
16
- super(nil, shift_age, shift_size)
17
- self.logdev = logdev
18
- end
12
+ def initialize(logdev, shift_age: 0, shift_size: 1048576)
13
+ @shift_age, @shift_size = shift_age, shift_size
14
+ @opened_logdev = false
15
+ super(nil, shift_age, shift_size)
16
+ self.logdev = logdev
17
+ end
19
18
 
20
- # allow changing the log destination. e.g.: in Unicorn during after_fork to
21
- # set a separate log path for each worker.
22
- def logdev=(logdev)
23
- @logdev.close if @logdev && @opened_logdev
19
+ # allow changing the log destination. e.g.: in Unicorn during after_fork to
20
+ # set a separate log path for each worker.
21
+ def logdev=(logdev)
22
+ @logdev.close if @logdev && @opened_logdev
24
23
 
25
- @raw_log_device = LogDevice.new(logdev, shift_age: @shift_age, shift_size: @shift_size)
24
+ @raw_log_device = LogDevice.new(
25
+ logdev,
26
+ shift_age: @shift_age,
27
+ shift_size: @shift_size,
28
+ )
26
29
 
27
- # if we opened the logdev then we should close it when we're done
28
- @opened_logdev = @raw_log_device.dev != logdev
30
+ # if we opened the logdev then we should close it when we're done
31
+ @opened_logdev = @raw_log_device.dev != logdev
29
32
 
30
- @logdev = Buffer.new(@raw_log_device)
31
- end
33
+ @logdev = BufferingLogger::Buffer.new(@raw_log_device)
34
+ end
32
35
 
33
- def buffered(transform: default_transform)
34
- @logdev.buffered(transform: transform) do
35
- yield
36
- end
36
+ def buffered(transform: default_transform)
37
+ @logdev.buffered(transform: transform) do
38
+ yield
37
39
  end
38
40
  end
41
+
39
42
  end
@@ -1,11 +1,11 @@
1
- module BufferingLogger
2
- class RackBuffer
3
- def initialize(app, logger, transform: nil)
4
- @app, @logger, @transform = app, logger, transform
5
- end
1
+ class BufferingLogger::RackBuffer
6
2
 
7
- def call(env)
8
- @logger.buffered(transform: @transform) { @app.call(env) }
9
- end
3
+ def initialize(app, logger, transform: nil)
4
+ @app, @logger, @transform = app, logger, transform
10
5
  end
6
+
7
+ def call(env)
8
+ @logger.buffered(transform: @transform) { @app.call(env) }
9
+ end
10
+
11
11
  end
@@ -2,36 +2,39 @@ require 'buffering_logger'
2
2
  require 'buffering_logger/rack_buffer'
3
3
  require 'rails/railtie'
4
4
 
5
- module BufferingLogger
6
- class Railtie < Rails::Railtie
7
- def self.install(transform: nil, device: nil, sync: true)
8
- initializer :buffering_logger, :before => :initialize_logger do |app|
9
- device ||= begin
10
- # Does mostly the same things that Rails does. See http://git.io/2v9FxQ
11
-
12
- path = app.paths["log"].first
13
-
14
- unless File.exist? File.dirname path
15
- FileUtils.mkdir_p File.dirname path
16
- end
17
-
18
- file = File.open(path, 'a')
19
- file.binmode
20
- file
21
- end
5
+ class BufferingLogger::Railtie < Rails::Railtie
22
6
 
23
- device.sync = true if sync && device.respond_to?(:sync=)
7
+ def self.install(transform: nil, device: nil, sync: true)
8
+ initializer :buffering_logger, :before => :initialize_logger do |app|
9
+ device ||= begin
10
+ # Does mostly the same things that Rails does. See http://git.io/2v9FxQ
24
11
 
25
- logger = BufferingLogger::Logger.new(device)
26
- logger.formatter = app.config.log_formatter
27
- logger = ActiveSupport::TaggedLogging.new(logger)
12
+ path = app.paths["log"].first
28
13
 
29
- app.config.logger = logger
14
+ unless File.exist? File.dirname path
15
+ FileUtils.mkdir_p File.dirname path
16
+ end
30
17
 
31
- # Inserts at the very beginning so that all logs, even from other
32
- # middleware, get buffered together.
33
- app.config.middleware.insert(0, BufferingLogger::RackBuffer, logger, transform: transform)
18
+ file = File.open(path, 'a')
19
+ file.binmode
20
+ file
34
21
  end
22
+
23
+ device.sync = true if sync && device.respond_to?(:sync=)
24
+
25
+ logger = BufferingLogger::Logger.new(device)
26
+ logger.formatter = app.config.log_formatter
27
+ logger = ActiveSupport::TaggedLogging.new(logger)
28
+
29
+ app.config.logger = logger
30
+
31
+ # Inserts at the very beginning so that all logs, even from other
32
+ # middleware, get buffered together.
33
+ app.config.middleware.insert(
34
+ 0,
35
+ BufferingLogger::RackBuffer, logger, transform: transform,
36
+ )
35
37
  end
36
38
  end
39
+
37
40
  end
@@ -1,28 +1,28 @@
1
- module BufferingLogger
2
- # This transforms a multiline log into a single line log by replacing newlines
3
- # with a special string (a single space by default).
4
- #
5
- # This is useful for platforms like Heroku where multiline Rails request logs
6
- # of one process are interwoven with the request logs of other processes and
7
- # other log sources (like the Heroku router).
8
- #
9
- # If you want to convert newlines into a special string so that you can later
10
- # turn them back into newlines (e.g. in Splunk using a
11
- # [SEDCMD](http://docs.splunk.com/Documentation/Splunk/latest/admin/Propsconf))
12
- # then you can supply a `replacement` argument.
13
- class SingleLineTransform
14
- REPLACEMENT = ' '.freeze
15
- NEWLINE = /\r?\n/.freeze
1
+ # This transforms a multiline log into a single line log by replacing newlines
2
+ # with a special string (a single space by default).
3
+ #
4
+ # This is useful for platforms like Heroku where multiline Rails request logs
5
+ # of one process are interwoven with the request logs of other processes and
6
+ # other log sources (like the Heroku router).
7
+ #
8
+ # If you want to convert newlines into a special string so that you can later
9
+ # turn them back into newlines (e.g. in Splunk using a
10
+ # [SEDCMD](http://docs.splunk.com/Documentation/Splunk/latest/admin/Propsconf))
11
+ # then you can supply a `replacement` argument.
12
+ class BufferingLogger::SingleLineTransform
16
13
 
17
- def initialize(replacement: REPLACEMENT)
18
- @replacement = replacement
19
- end
14
+ REPLACEMENT = ' '.freeze
15
+ NEWLINE = /\r?\n/.freeze
20
16
 
21
- def call(msg)
22
- msg = msg.dup
23
- msg.strip!
24
- msg.gsub!(NEWLINE, @replacement)
25
- msg << "\n"
26
- end
17
+ def initialize(replacement: REPLACEMENT)
18
+ @replacement = replacement
27
19
  end
20
+
21
+ def call(msg)
22
+ msg = msg.dup
23
+ msg.strip!
24
+ msg.gsub!(NEWLINE, @replacement)
25
+ msg << "\n"
26
+ end
27
+
28
28
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: buffering_logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.5
4
+ version: 2.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jordan Brough
@@ -43,8 +43,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
43
43
  - !ruby/object:Gem::Version
44
44
  version: '0'
45
45
  requirements: []
46
- rubyforge_project:
47
- rubygems_version: 2.4.5.1
46
+ rubygems_version: 3.0.3
48
47
  signing_key:
49
48
  specification_version: 4
50
49
  summary: BufferingLogger is a logger that buffers log entries and then writes them