Class: ProLogger

Inherits:
Logger
  • Object
show all
Defined in:
lib/sixarm_ruby_pro_logger.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ProLogger

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 " ... ".

Parameters:

  • options (Hash) (defaults to: {})


45
46
47
48
49
50
51
52
53
# File 'lib/sixarm_ruby_pro_logger.rb', line 45

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

Instance Attribute Details

#backtrace_separatorObject

Returns the value of attribute backtrace_separator.



15
16
17
# File 'lib/sixarm_ruby_pro_logger.rb', line 15

def backtrace_separator
  @backtrace_separator
end

#hostnameObject

Returns the value of attribute hostname.



12
13
14
# File 'lib/sixarm_ruby_pro_logger.rb', line 12

def hostname
  @hostname
end

#line_separatorObject

Returns the value of attribute line_separator.



16
17
18
# File 'lib/sixarm_ruby_pro_logger.rb', line 16

def line_separator
  @line_separator
end

#message_separatorObject

Returns the value of attribute message_separator.



14
15
16
# File 'lib/sixarm_ruby_pro_logger.rb', line 14

def message_separator
  @message_separator
end

#pidObject

Returns the value of attribute pid.



13
14
15
# File 'lib/sixarm_ruby_pro_logger.rb', line 13

def pid
  @pid
end

#prognameObject

Returns the value of attribute progname.



11
12
13
# File 'lib/sixarm_ruby_pro_logger.rb', line 11

def progname
  @progname
end

#time_formatObject

Returns the value of attribute time_format.



10
11
12
# File 'lib/sixarm_ruby_pro_logger.rb', line 10

def time_format
  @time_format
end

Instance Method Details

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

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.

Parameters:

  • severity (String)

    the severity object, such as ‘“INFO”`.

  • time (Time)

    the time, typically Time.now.utc.

  • progname (String)

    the program name, which is anything you like.

  • msg (String)

    the message.



69
70
71
# File 'lib/sixarm_ruby_pro_logger.rb', line 69

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) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/sixarm_ruby_pro_logger.rb', line 85

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) ⇒ Object



104
105
106
# File 'lib/sixarm_ruby_pro_logger.rb', line 104

def message_string_when_array(msg)
  msg.map{|item| message_string(item)}.join(message_separator)
end

#message_string_when_exception(msg) ⇒ Object



108
109
110
# File 'lib/sixarm_ruby_pro_logger.rb', line 108

def message_string_when_exception(msg)
  "#{msg.class} #{msg.message}: " + (msg.backtrace || []).join(backtrace_separator)
end

#message_string_when_object(msg) ⇒ Object



112
113
114
# File 'lib/sixarm_ruby_pro_logger.rb', line 112

def message_string_when_object(msg)
  msg.inspect
end

#message_string_when_string(msg) ⇒ Object



100
101
102
# File 'lib/sixarm_ruby_pro_logger.rb', line 100

def message_string_when_string(msg)
  msg
end

#progname_string(progname) ⇒ Object



77
78
79
# File 'lib/sixarm_ruby_pro_logger.rb', line 77

def progname_string(progname)
  (progname || self.progname).to_s
end

#severity_string(severity) ⇒ Object



81
82
83
# File 'lib/sixarm_ruby_pro_logger.rb', line 81

def severity_string(severity)
  (severity || self.severity).to_s
end

#time_string(time) ⇒ Object



73
74
75
# File 'lib/sixarm_ruby_pro_logger.rb', line 73

def time_string(time)
  time.utc.strftime(time_format)
end