Class: BufferedLogger

Inherits:
Object
  • Object
show all
Includes:
Buffering, Formatting, Indentation, Severity
Defined in:
lib/buffered_logger.rb,
lib/buffered_logger/logger.rb

Defined Under Namespace

Modules: Buffering, Formatting, Indentation, Severity Classes: Formatter, Padding, ThreadHash

Constant Summary collapse

SEVERITY_LEVELS =
Severity.constants.map { |c| c.downcase.to_sym }.freeze
SEVERITY_MAP =
Severity.constants.inject({}) do |h,c| 
  h[c.downcase.to_sym] = Severity.const_get(c); h
end.freeze

Constants included from Severity

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

Constants included from Buffering

Buffering::MAX_BUFFER_SIZE

Constants included from Formatting

Formatting::DEFAULT_LEVEL

Constants included from Indentation

Indentation::INDENT_LEVEL

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Buffering

#auto_flushing, #auto_flushing=, #buffer_text, #close, #flush

Methods included from Formatting

#color?, #disable_color, #enable_color, #toggle_color

Methods included from Indentation

#indent, #padding

Constructor Details

#initialize(log, level = DEBUG, params = {}) ⇒ BufferedLogger

Returns a new instance of BufferedLogger.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/buffered_logger/logger.rb', line 35

def initialize(log, level = DEBUG, params = {})
  @master_thread = Thread.current
  @level = severity_to_const(level)
  if log.respond_to?(:write)
    @log = log
  elsif File.exist?(log)
    @log = open_log(log, (File::WRONLY | File::APPEND))
  else
    FileUtils.mkdir_p(File.dirname(log))
    @log = open_log(log, (File::WRONLY | File::APPEND | File::CREAT))
  end
  super()
  params.each { |k,v| SEVERITY_LEVELS.include?(k) ? set_formatter(k, v) : next }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class BufferedLogger::Formatting

Instance Attribute Details

#levelObject

Returns the value of attribute level.



31
32
33
# File 'lib/buffered_logger/logger.rb', line 31

def level
  @level
end

Instance Method Details

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



57
58
59
60
61
62
63
64
65
66
# File 'lib/buffered_logger/logger.rb', line 57

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

#open_log(log, mode) ⇒ Object



50
51
52
53
54
55
# File 'lib/buffered_logger/logger.rb', line 50

def open_log(log, mode)
  open(log, mode).tap do |open_log|
    open_log.set_encoding(Encoding::BINARY) if open_log.respond_to?(:set_encoding)
    open_log.sync = true
  end
end


81
82
83
# File 'lib/buffered_logger/logger.rb', line 81

def print_blank_line
  add(0, "\n")
end

#silence(temporary_level = ERROR) ⇒ Object

Silences the logger for the duration of the block.



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/buffered_logger/logger.rb', line 86

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



28
# File 'lib/buffered_logger/logger.rb', line 28

cattr_accessor :silencer