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 vendor/rails/activesupport/lib/active_support/buffered_logger.rb, line 37
37: def initialize(log, level = DEBUG)
38: @level = level
39: @buffer = {}
40: @auto_flushing = 1
41: @guard = Mutex.new
42:
43: if log.respond_to?(:write)
44: @log = log
45: elsif File.exist?(log)
46: @log = open(log, (File::WRONLY | File::APPEND))
47: @log.sync = true
48: else
49: FileUtils.mkdir_p(File.dirname(log))
50: @log = open(log, (File::WRONLY | File::APPEND | File::CREAT))
51: @log.sync = true
52: @log.write("# Logfile created on %s" % [Time.now.to_s])
53: end
54: end
Public Instance methods
[ show source ]
# File vendor/rails/activesupport/lib/active_support/buffered_logger.rb, line 56
56: def add(severity, message = nil, progname = nil, &block)
57: return if @level > severity
58: message = (message || (block && block.call) || progname).to_s
59: # If a newline is necessary then create a new message ending with a newline.
60: # Ensures that the original message is not mutated.
61: message = "#{message}\n" unless message[-1] == ?\n
62: buffer << message
63: auto_flush
64: message
65: 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 vendor/rails/activesupport/lib/active_support/buffered_logger.rb, line 84
84: def auto_flushing=(period)
85: @auto_flushing =
86: case period
87: when true; 1
88: when false, nil, 0; MAX_BUFFER_SIZE
89: when Integer; period
90: else raise ArgumentError, "Unrecognized auto_flushing period: #{period.inspect}"
91: end
92: end
[ show source ]
# File vendor/rails/activesupport/lib/active_support/buffered_logger.rb, line 104
104: def close
105: flush
106: @log.close if @log.respond_to?(:close)
107: @log = nil
108: end
[ show source ]
# File vendor/rails/activesupport/lib/active_support/buffered_logger.rb, line 94
94: def flush
95: @guard.synchronize do
96: unless buffer.empty?
97: old_buffer = buffer
98: clear_buffer
99: @log.write(old_buffer.join)
100: end
101: end
102: end
Silences the logger for the duration of the block.
[ show source ]
# File vendor/rails/activesupport/lib/active_support/buffered_logger.rb, line 21
21: def silence(temporary_level = ERROR)
22: if silencer
23: begin
24: old_logger_level, self.level = level, temporary_level
25: yield self
26: ensure
27: self.level = old_logger_level
28: end
29: else
30: yield self
31: end
32: end
Protected Instance methods
[ show source ]
# File vendor/rails/activesupport/lib/active_support/buffered_logger.rb, line 111
111: def auto_flush
112: flush if buffer.size >= @auto_flushing
113: end
[ show source ]
# File vendor/rails/activesupport/lib/active_support/buffered_logger.rb, line 115
115: def buffer
116: @buffer[Thread.current] ||= []
117: end
[ show source ]
# File vendor/rails/activesupport/lib/active_support/buffered_logger.rb, line 119
119: def clear_buffer
120: @buffer.delete(Thread.current)
121: end