Inspired by the buffered logger idea by Ezra
Methods
Included Modules
Classes and Modules
Module ActiveSupport::BufferedLogger::SeverityConstants
MAX_BUFFER_SIZE | = | 1000 |
Attributes
[R] | auto_flushing | |
[RW] | level |
Public Class methods
[ show source ]
# File activesupport/lib/active_support/buffered_logger.rb, line 39 39: def initialize(log, level = DEBUG) 40: @level = level 41: @buffer = {} 42: @auto_flushing = 1 43: @guard = Mutex.new 44: 45: if log.respond_to?(:write) 46: @log = log 47: elsif File.exist?(log) 48: @log = open(log, (File::WRONLY | File::APPEND)) 49: @log.sync = true 50: else 51: FileUtils.mkdir_p(File.dirname(log)) 52: @log = open(log, (File::WRONLY | File::APPEND | File::CREAT)) 53: @log.sync = true 54: @log.write("# Logfile created on %s" % [Time.now.to_s]) 55: end 56: end
Public Instance methods
[ show source ]
# File activesupport/lib/active_support/buffered_logger.rb, line 58 58: def add(severity, message = nil, progname = nil, &block) 59: return if @level > severity 60: message = (message || (block && block.call) || progname).to_s 61: # If a newline is necessary then create a new message ending with a newline. 62: # Ensures that the original message is not mutated. 63: message = "#{message}\n" unless message[-1] == ?\n 64: buffer << message 65: auto_flush 66: message 67: 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 86 86: def auto_flushing=(period) 87: @auto_flushing = 88: case period 89: when true; 1 90: when false, nil, 0; MAX_BUFFER_SIZE 91: when Integer; period 92: else raise ArgumentError, "Unrecognized auto_flushing period: #{period.inspect}" 93: end 94: end
[ show source ]
# File activesupport/lib/active_support/buffered_logger.rb, line 109 109: def close 110: flush 111: @log.close if @log.respond_to?(:close) 112: @log = nil 113: end
[ show source ]
# File activesupport/lib/active_support/buffered_logger.rb, line 96 96: def flush 97: @guard.synchronize do 98: unless buffer.empty? 99: old_buffer = buffer 100: @log.write(old_buffer.join) 101: end 102: 103: # Important to do this even if buffer was empty or else @buffer will 104: # accumulate empty arrays for each request where nothing was logged. 105: clear_buffer 106: end 107: end
Silences the logger for the duration of the block.
[ show source ]
# File activesupport/lib/active_support/buffered_logger.rb, line 23 23: def silence(temporary_level = ERROR) 24: if silencer 25: begin 26: old_logger_level, self.level = level, temporary_level 27: yield self 28: ensure 29: self.level = old_logger_level 30: end 31: else 32: yield self 33: end 34: end
Protected Instance methods
[ show source ]
# File activesupport/lib/active_support/buffered_logger.rb, line 116 116: def auto_flush 117: flush if buffer.size >= @auto_flushing 118: end
[ show source ]
# File activesupport/lib/active_support/buffered_logger.rb, line 120 120: def buffer 121: @buffer[Thread.current] ||= [] 122: end
[ show source ]
# File activesupport/lib/active_support/buffered_logger.rb, line 124 124: def clear_buffer 125: @buffer.delete(Thread.current) 126: end