Class: OodAppkit::LogFormatter

Inherits:
Object
  • Object
show all
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



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ood_appkit/log_formatter.rb', line 17

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



7
8
9
10
11
12
13
# File 'lib/ood_appkit/log_formatter.rb', line 7

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.strip.inspect : msg.inspect)

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