Class: FileFormatter

Inherits:
Log4r::BasicFormatter
  • Object
show all
Defined in:
lib/maestro/log4r/file_formatter.rb

Overview

Custom Log4r formatter for files

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ FileFormatter

Returns a new instance of FileFormatter.



6
7
8
# File 'lib/maestro/log4r/file_formatter.rb', line 6

def initialize(hash={})
  super(hash)
end

Instance Method Details

#format(logevent) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/maestro/log4r/file_formatter.rb', line 10

def format(logevent)
  if Log4r::LNAMES[logevent.level].eql? "PROGRESS"
    # Formats the data as is with no newline, to allow progress bars to be logged.
    sprintf("%s", logevent.data.to_s)
  else
    if logevent.data.kind_of? String
      # remove ^M characters
      logevent.data.gsub!(/\r/, "")
      # Prevent two newlines in the log file
      logevent.data.chop! if logevent.data =~ /\n$/
    end
    sprintf("[%s %s] %s\n", Log4r::LNAMES[logevent.level], Time.now.strftime("%m/%d/%Y %I:%M:%S %p"), format_object(logevent.data))
  end
end