Inspired by the buffered logger idea by Ezra
- Severity
MAX_BUFFER_SIZE | = | 1000 |
[RW] | level | |
[R] | auto_flushing |
[ show source ]
# File activesupport/lib/active_support/buffered_logger.rb, line 41 41: def initialize(log, level = DEBUG) 42: @level = level 43: @buffer = {} 44: @auto_flushing = 1 45: @guard = Mutex.new 46: 47: if log.respond_to?(:write) 48: @log = log 49: elsif File.exist?(log) 50: @log = open(log, (File::WRONLY | File::APPEND)) 51: @log.sync = true 52: else 53: FileUtils.mkdir_p(File.dirname(log)) 54: @log = open(log, (File::WRONLY | File::APPEND | File::CREAT)) 55: @log.sync = true 56: end 57: end
Set to false to disable the silencer
[ show source ]
# File activesupport/lib/active_support/buffered_logger.rb, line 21 21: cattr_accessor :silencer
[ show source ]
# File activesupport/lib/active_support/buffered_logger.rb, line 59 59: def add(severity, message = nil, progname = nil, &block) 60: return if @level > severity 61: message = (message || (block && block.call) || progname).to_s 62: # If a newline is necessary then create a new message ending with a newline. 63: # Ensures that the original message is not mutated. 64: message = "#{message}\n" unless message[1] == \n\ 65: buffer << message 66: auto_flush 67: message 68: end
Set the auto-flush period. Set to true to flush after every log message, to an integer to flush every N messages, or to false, nil, or zero to never auto-flush. If you turn auto-flushing off, be sure to regularly flush the log yourself — it will eat up memory until you do.
[ show source ]
# File activesupport/lib/active_support/buffered_logger.rb, line 90 90: def auto_flushing=(period) 91: @auto_flushing = 92: case period 93: when true; 1 94: when false, nil, 0; MAX_BUFFER_SIZE 95: when Integer; period 96: else raise ArgumentError, "Unrecognized auto_flushing period: #{period.inspect}" 97: end 98: end
[ show source ]
# File activesupport/lib/active_support/buffered_logger.rb, line 117 117: def close 118: flush 119: @log.close if @log.respond_to?(:close) 120: @log = nil 121: end
[ show source ]
# File activesupport/lib/active_support/buffered_logger.rb, line 100 100: def flush 101: @guard.synchronize do 102: unless buffer.empty? 103: old_buffer = buffer 104: all_content = StringIO.new 105: old_buffer.each do |content| 106: all_content << content 107: end 108: @log.write(all_content.string) 109: end 110: 111: # Important to do this even if buffer was empty or else @buffer will 112: # accumulate empty arrays for each request where nothing was logged. 113: clear_buffer 114: end 115: end
Silences the logger for the duration of the block.
[ show source ]
# File activesupport/lib/active_support/buffered_logger.rb, line 25 25: def silence(temporary_level = ERROR) 26: if silencer 27: begin 28: old_logger_level, self.level = level, temporary_level 29: yield self 30: ensure 31: self.level = old_logger_level 32: end 33: else 34: yield self 35: end 36: end
[ show source ]
# File activesupport/lib/active_support/buffered_logger.rb, line 124 124: def auto_flush 125: flush if buffer.size >= @auto_flushing 126: end
[ show source ]
# File activesupport/lib/active_support/buffered_logger.rb, line 128 128: def buffer 129: @buffer[Thread.current] ||= [] 130: end
[ show source ]
# File activesupport/lib/active_support/buffered_logger.rb, line 132 132: def clear_buffer 133: @buffer.delete(Thread.current) 134: end