class ProLogger

Attributes

backtrace_separator[RW]
hostname[RW]
line_separator[RW]
message_separator[RW]
pid[RW]
progname[RW]
time_format[RW]

Public Class Methods

new(options={}) click to toggle source

Initialize the Rails logger formatter.

Options:

* time_format: A format string for the `time.strftime` method.
    Default is `"%Y-%m-%dT%H:%M:%SZ"` which is ISO 8601 format.

* progname: The running program name.
    Default is `$PROGRAM_NAME`.

* hostname: The server host name.
    Default is `Socket.gethostname`.

* pid: The process id number.
    Default is `Process.pid`.

* message_separator: Text to use to join mutiple messages.
    Default is " ... ".

* backtrace_separator: Print this between exception backtrace lines.
    Default is " ... ".

* line_separator: Change any message newlines to this text.
    Default is " ... ".

@param [Hash] options

# File lib/sixarm_ruby_pro_logger/pro_logger.rb, line 44
def initialize(options={})
  @time_format = (options[:time_format] || "%Y-%m-%dT%H:%M:%SZ").to_s
  @progname = (options[:progname] || $PROGRAM_NAME).to_s
  @hostname = (options[:hostname] || (require('socket') && Socket.gethostname)).to_s
  @pid = (options[:pid] || Process.pid).to_s
  @message_separator = (options[:message_separator] || " ... ").to_s
  @backtrace_separator = (options[:backtrace_separator] || " ... ").to_s
  @line_separator = (options[:line_separator] || " ... ").to_s
end

Public Instance Methods

call(severity, time, progname, msg) click to toggle source

Call the formatter.

All of the params will be converted to strings; it's fine to send objects instead of strings.

We strip the message of extraneous whitespace.

See initialize for the defaults.

@param severity [String] the severity object, such as `“INFO”`. @param time [Time] the time, typically `Time.now.utc`. @param progname [String] the program name, which is anything you like. @param msg [String] the message.

# File lib/sixarm_ruby_pro_logger/pro_logger.rb, line 68
def call(severity, time, progname, msg)
  "#{time_string(time)} #{progname_string(progname)} #{hostname} #{pid} #{severity_string(severity)} #{message_string(msg)}\n"
end
message_string(msg) click to toggle source
# File lib/sixarm_ruby_pro_logger/pro_logger.rb, line 84
def message_string(msg)
  s = \
  case msg
  when ::String
    message_string_when_string(msg)
  when ::Exception
    message_string_when_exception(msg)
  when ::Array
    message_string_when_array(msg)
  else
    message_string_when_object(msg)
  end
  return s.gsub(/\n/, line_separator).lstrip
end
message_string_when_array(msg) click to toggle source
# File lib/sixarm_ruby_pro_logger/pro_logger.rb, line 103
def message_string_when_array(msg)
  msg.map{|item| message_string(item)}.join(message_separator)
end
message_string_when_exception(msg) click to toggle source
# File lib/sixarm_ruby_pro_logger/pro_logger.rb, line 107
def message_string_when_exception(msg)
  "#{msg.class} #{msg.message}: " + (msg.backtrace || []).join(backtrace_separator)
end
message_string_when_object(msg) click to toggle source
# File lib/sixarm_ruby_pro_logger/pro_logger.rb, line 111
def message_string_when_object(msg)
  msg.inspect
end
message_string_when_string(msg) click to toggle source
# File lib/sixarm_ruby_pro_logger/pro_logger.rb, line 99
def message_string_when_string(msg)
  msg
end
progname_string(progname) click to toggle source
# File lib/sixarm_ruby_pro_logger/pro_logger.rb, line 76
def progname_string(progname)
  (progname || self.progname).to_s
end
severity_string(severity) click to toggle source
# File lib/sixarm_ruby_pro_logger/pro_logger.rb, line 80
def severity_string(severity)
  (severity || self.severity).to_s
end
time_string(time) click to toggle source
# File lib/sixarm_ruby_pro_logger/pro_logger.rb, line 72
def time_string(time)
  time.utc.strftime(time_format)
end