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 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: @log.write("# Logfile created on %s" % [Time.now.to_s])
57: end
58: end
Public Instance methods
[ show source ]
# File activesupport/lib/active_support/buffered_logger.rb, line 60
60: def add(severity, message = nil, progname = nil, &block)
61: return if @level > severity
62: message = (message || (block && block.call) || progname).to_s
63: # If a newline is necessary then create a new message ending with a newline.
64: # Ensures that the original message is not mutated.
65: message = "#{message}\n" unless message[-1] == ?\n
66: buffer << message
67: auto_flush
68: message
69: 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 88
88: def auto_flushing=(period)
89: @auto_flushing =
90: case period
91: when true; 1
92: when false, nil, 0; MAX_BUFFER_SIZE
93: when Integer; period
94: else raise ArgumentError, "Unrecognized auto_flushing period: #{period.inspect}"
95: end
96: end
[ show source ]
# File activesupport/lib/active_support/buffered_logger.rb, line 111
111: def close
112: flush
113: @log.close if @log.respond_to?(:close)
114: @log = nil
115: end
[ show source ]
# File activesupport/lib/active_support/buffered_logger.rb, line 98
98: def flush
99: @guard.synchronize do
100: unless buffer.empty?
101: old_buffer = buffer
102: @log.write(old_buffer.join)
103: end
104:
105: # Important to do this even if buffer was empty or else @buffer will
106: # accumulate empty arrays for each request where nothing was logged.
107: clear_buffer
108: end
109: 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
Protected Instance methods
[ show source ]
# File activesupport/lib/active_support/buffered_logger.rb, line 118
118: def auto_flush
119: flush if buffer.size >= @auto_flushing
120: end
[ show source ]
# File activesupport/lib/active_support/buffered_logger.rb, line 122
122: def buffer
123: @buffer[Thread.current] ||= []
124: end
[ show source ]
# File activesupport/lib/active_support/buffered_logger.rb, line 126
126: def clear_buffer
127: @buffer.delete(Thread.current)
128: end