Class: ExtendedLogger::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/extended_logger/formatter.rb

Direct Known Subclasses

ColoredFormatter

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#logger_formatterObject



35
36
37
# File 'lib/extended_logger/formatter.rb', line 35

def logger_formatter
  @logger_formatter ||= Logger::Formatter.new
end

Instance Method Details

#call(*arguments) ⇒ Object



5
6
7
# File 'lib/extended_logger/formatter.rb', line 5

def call *arguments
  delegate *arguments
end

#delegate(*arguments, message) ⇒ Object



9
10
11
12
# File 'lib/extended_logger/formatter.rb', line 9

def delegate *arguments, message
  message = format_message message, arguments
  logger_formatter.(*arguments, message)
end

#format_message(message, arguments) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/extended_logger/formatter.rb', line 14

def format_message message, arguments
  output = ''

  message.each_line "\n".freeze do |line|
    if line == "\n".freeze
      line = "\\n"
    else
      line.gsub! "\r".freeze, "\\r".freeze
    end

    if output.empty?
      output.concat line
    else
      formatted_line = logger_formatter.(*arguments, line.chomp("\n".freeze))
      output.concat formatted_line
    end
  end

  output.chomp "\n".freeze
end