Class: OodAppkit::LogFormatter

Inherits:
Object
  • Object
show all
Includes:
ActiveSupport::TaggedLogging::Formatter
Defined in:
lib/ood_appkit/log_formatter.rb

Overview

format log messages with timestamp severity and app token e.g.:

[2016-06-17 15:31:01 -0400 sys/dashboard]  INFO  GET...

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.setupObject

make the Rails logger use this class for the formatter and set the progname to be the app token



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ood_appkit/log_formatter.rb', line 22

def self.setup
  ::Rails.logger.formatter = LogFormatter.new

  # ActiveSupport::TaggedLogging.new calls
  #
  #     logger.formatter.extend(Formatter)
  #
  # in an undocumented submodule ActiveSupport::TaggedLogging::Formatter.
  # So to modify a TaggedLogging logger with another formatter we must
  # extend our formatter in the same way.
  if defined?( ActiveSupport::TaggedLogging  ) && ::Rails.logger.kind_of?( ActiveSupport::TaggedLogging )
    ::Rails.logger.formatter.extend(ActiveSupport::TaggedLogging::Formatter)
  end

  ::Rails.logger.progname = ENV['APP_TOKEN'] if ENV['APP_TOKEN']
end

Instance Method Details

#call(severity, timestamp, progname, msg) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/ood_appkit/log_formatter.rb', line 12

def call(severity, timestamp, progname, msg)
  severity_d = severity ? severity[0,5].rjust(5).upcase : "UNKNO"
  timestamp_d = timestamp ? timestamp.localtime : Time.now.localtime
  msg_d = (String === msg ? msg.encode("UTF-8", :invalid => :replace, :undef => :replace).strip.inspect : msg.inspect)

  "[#{timestamp_d} #{progname}] #{severity_d} #{msg_d}\n"
end