Class: BufferedLogger

Inherits:
Object
  • Object
show all
Includes:
Severity
Defined in:
lib/yore/BufferedLogger.rb

Overview

Inspired by the buffered logger idea by Ezra

Defined Under Namespace

Modules: Severity

Constant Summary collapse

MAX_BUFFER_SIZE =
1000

Constants included from Severity

Severity::DEBUG, Severity::ERROR, Severity::FATAL, Severity::INFO, Severity::UNKNOWN, Severity::WARN

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(log, level = DEBUG) ⇒ BufferedLogger

Returns a new instance of BufferedLogger.



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/yore/BufferedLogger.rb', line 121

def initialize(log, level = DEBUG)
  @level         = level
  @buffer        = {}
  @auto_flushing = 1
  @guard = Mutex.new

  if log.respond_to?(:write)
    @log = log
  elsif File.exist?(log)
    @log = open(log, (File::WRONLY | File::APPEND))
    @log.sync = true
  else
    FileUtils.mkdir_p(File.dirname(log))
    @log = open(log, (File::WRONLY | File::APPEND | File::CREAT))
    @log.sync = true
    @log.write("# Logfile created on %s" % [Time.now.to_s])
  end
end

Instance Attribute Details

#auto_flushingObject

Returns the value of attribute auto_flushing.



119
120
121
# File 'lib/yore/BufferedLogger.rb', line 119

def auto_flushing
  @auto_flushing
end

#levelObject

Returns the value of attribute level.



118
119
120
# File 'lib/yore/BufferedLogger.rb', line 118

def level
  @level
end

Instance Method Details

#add(severity, message = nil, progname = nil, &block) ⇒ Object



140
141
142
143
144
145
146
147
148
149
# File 'lib/yore/BufferedLogger.rb', line 140

def add(severity, message = nil, progname = nil, &block)
  return if @level > severity
  message = (message || (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.
  message = "#{message}\n" unless message[-1] == ?\n
  buffer << message
  auto_flush
  message
end

#closeObject



190
191
192
193
194
# File 'lib/yore/BufferedLogger.rb', line 190

def close
  flush
  @log.close if @log.respond_to?(:close)
  @log = nil
end

#flushObject



177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/yore/BufferedLogger.rb', line 177

def flush
  @guard.synchronize do
    unless buffer.empty?
      old_buffer = buffer
      @log.write(old_buffer.join)
    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

#silence(temporary_level = ERROR) ⇒ Object

Silences the logger for the duration of the block.



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/yore/BufferedLogger.rb', line 105

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

#silencerObject

:singleton-method: Set to false to disable the silencer



101
# File 'lib/yore/BufferedLogger.rb', line 101

cattr_accessor :silencer