Class: ProLogger
- Inherits:
-
Logger
- Object
- Logger
- ProLogger
- Defined in:
- lib/sixarm_ruby_pro_logger.rb
Instance Attribute Summary collapse
-
#backtrace_separator ⇒ Object
Returns the value of attribute backtrace_separator.
-
#hostname ⇒ Object
Returns the value of attribute hostname.
-
#line_separator ⇒ Object
Returns the value of attribute line_separator.
-
#message_separator ⇒ Object
Returns the value of attribute message_separator.
-
#pid ⇒ Object
Returns the value of attribute pid.
-
#progname ⇒ Object
Returns the value of attribute progname.
-
#time_format ⇒ Object
Returns the value of attribute time_format.
Instance Method Summary collapse
-
#call(severity, time, progname, msg) ⇒ Object
Call the formatter.
-
#initialize(options = {}) ⇒ ProLogger
constructor
Initialize the Rails logger formatter.
- #message_string(msg) ⇒ Object
- #message_string_when_array(msg) ⇒ Object
- #message_string_when_exception(msg) ⇒ Object
- #message_string_when_object(msg) ⇒ Object
- #message_string_when_string(msg) ⇒ Object
- #progname_string(progname) ⇒ Object
- #severity_string(severity) ⇒ Object
- #time_string(time) ⇒ Object
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: Print this between mutiple .
Default is "\n".
* backtrace_separator: Print this between exception backtrace lines.
Default is "\n".
* line_separator: Print this between lines.
Default is "\n", which is the same as no change.
45 46 47 48 49 50 51 52 53 |
# File 'lib/sixarm_ruby_pro_logger.rb', line 45 def initialize(={}) @time_format = ([:time_format] || "%Y-%m-%dT%H:%M:%SZ").to_s @progname = ([:progname] || $PROGRAM_NAME).to_s @hostname = ([:hostname] || (require('socket') && Socket.gethostname)).to_s @pid = ([:pid] || Process.pid).to_s = ([:message_separator] || "\n").to_s @backtrace_separator = ([:backtrace_separator] || "\n").to_s @line_separator = ([:line_separator] || "\n").to_s end |
Instance Attribute Details
#backtrace_separator ⇒ Object
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 |
#hostname ⇒ Object
Returns the value of attribute hostname.
12 13 14 |
# File 'lib/sixarm_ruby_pro_logger.rb', line 12 def hostname @hostname end |
#line_separator ⇒ Object
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_separator ⇒ Object
Returns the value of attribute message_separator.
14 15 16 |
# File 'lib/sixarm_ruby_pro_logger.rb', line 14 def end |
#pid ⇒ Object
Returns the value of attribute pid.
13 14 15 |
# File 'lib/sixarm_ruby_pro_logger.rb', line 13 def pid @pid end |
#progname ⇒ Object
Returns the value of attribute progname.
11 12 13 |
# File 'lib/sixarm_ruby_pro_logger.rb', line 11 def progname @progname end |
#time_format ⇒ Object
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.
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 99 100 101 |
# File 'lib/sixarm_ruby_pro_logger.rb', line 85 def (msg) s = \ case msg when ::String (msg) when ::Exception (msg) when ::Array (msg) else (msg) end if line_separator && line_separator != "\n" s = s.gsub!(/\n/, line_separator) end return s.lstrip end |
#message_string_when_array(msg) ⇒ Object
107 108 109 |
# File 'lib/sixarm_ruby_pro_logger.rb', line 107 def (msg) msg.map{|item| (item)}.join() end |
#message_string_when_exception(msg) ⇒ Object
111 112 113 |
# File 'lib/sixarm_ruby_pro_logger.rb', line 111 def (msg) "#{msg.class} #{msg.message}: " + (msg.backtrace || []).join(backtrace_separator) end |
#message_string_when_object(msg) ⇒ Object
115 116 117 |
# File 'lib/sixarm_ruby_pro_logger.rb', line 115 def (msg) msg.inspect end |
#message_string_when_string(msg) ⇒ Object
103 104 105 |
# File 'lib/sixarm_ruby_pro_logger.rb', line 103 def (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 |