Class: ActiveSupport::BufferedLogger
- Inherits:
-
Object
- Object
- ActiveSupport::BufferedLogger
- Includes:
- Severity
- Defined in:
- activesupport/lib/active_support/buffered_logger.rb
Overview
Inspired by the buffered logger idea by Ezra
Defined Under Namespace
Modules: Severity
Constant Summary
- MAX_BUFFER_SIZE =
1000
Constants included from Severity
Severity::DEBUG, Severity::ERROR, Severity::FATAL, Severity::INFO, Severity::UNKNOWN, Severity::WARN
Instance Attribute Summary (collapse)
-
- (Object) auto_flushing
Returns the value of attribute auto_flushing.
-
- (Object) level
Returns the value of attribute level.
Instance Method Summary (collapse)
- - (Object) add(severity, message = nil, progname = nil, &block)
- - (Object) close
- - (Object) flush
-
- (BufferedLogger) initialize(log, level = DEBUG)
constructor
A new instance of BufferedLogger.
- - (Object) open_log(log, mode)
-
- (Object) silence(temporary_level = ERROR)
Silences the logger for the duration of the block.
-
- (Object) silencer
:singleton-method: Set to false to disable the silencer.
Constructor Details
- (BufferedLogger) initialize(log, level = DEBUG)
A new instance of BufferedLogger
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'activesupport/lib/active_support/buffered_logger.rb', line 42 def initialize(log, level = DEBUG) @level = level @buffer = Hash.new { |h,k| h[k] = [] } @auto_flushing = 1 @guard = Mutex.new if log.respond_to?(:write) @log = log elsif File.exist?(log) @log = open_log(log, (File::WRONLY | File::APPEND)) else FileUtils.mkdir_p(File.dirname(log)) @log = open_log(log, (File::WRONLY | File::APPEND | File::CREAT)) end end |
Instance Attribute Details
- (Object) auto_flushing
Returns the value of attribute auto_flushing
40 41 42 |
# File 'activesupport/lib/active_support/buffered_logger.rb', line 40 def auto_flushing @auto_flushing end |
- (Object) level
Returns the value of attribute level
39 40 41 |
# File 'activesupport/lib/active_support/buffered_logger.rb', line 39 def level @level end |
Instance Method Details
- (Object) add(severity, message = nil, progname = nil, &block)
65 66 67 68 69 70 71 72 73 74 |
# File 'activesupport/lib/active_support/buffered_logger.rb', line 65 def add(severity, = nil, progname = nil, &block) return if @level > severity = ( || (block && block.call) || progname).to_s # If a newline is necessary then create a new message ending with a newline. # Ensures that the original message is not mutated. = "#{}\n" unless [-1] == ?\n buffer << auto_flush end |
- (Object) close
118 119 120 121 122 |
# File 'activesupport/lib/active_support/buffered_logger.rb', line 118 def close flush @log.close if @log.respond_to?(:close) @log = nil end |
- (Object) flush
106 107 108 109 110 111 112 113 114 115 116 |
# File 'activesupport/lib/active_support/buffered_logger.rb', line 106 def flush @guard.synchronize do buffer.each do |content| @log.write(content) end # Important to do this even if buffer was empty or else @buffer will # accumulate empty arrays for each request where nothing was logged. clear_buffer end end |
- (Object) open_log(log, mode)
58 59 60 61 62 63 |
# File 'activesupport/lib/active_support/buffered_logger.rb', line 58 def open_log(log, mode) open(log, mode).tap do |open_log| open_log.set_encoding(Encoding::BINARY) if open_log.respond_to?(:set_encoding) open_log.sync = true end end |
- (Object) silence(temporary_level = ERROR)
Silences the logger for the duration of the block.
26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'activesupport/lib/active_support/buffered_logger.rb', line 26 def silence(temporary_level = ERROR) if silencer begin old_logger_level, self.level = level, temporary_level yield self ensure self.level = old_logger_level end else yield self end end |
- (Object) silencer
:singleton-method: Set to false to disable the silencer
22 |
# File 'activesupport/lib/active_support/buffered_logger.rb', line 22 cattr_accessor :silencer |