Module: YetiLogger::MessageFormatters
- Defined in:
- lib/yeti_logger/message_formatters.rb
Overview
Helper class used to format messages for logging. These can be used directly, but are more convenient when used via YetiLogger
Constant Summary collapse
- NUM_LINES_OF_EXCEPTIONS =
50
Class Method Summary collapse
-
.build_log_message(klass, obj, exception = nil, backtrace_lines = NUM_LINES_OF_EXCEPTIONS) ⇒ String
Helper method used to build up a single log message string you can pass to the underlying logger implementation.
-
.exception_hash(exception, lines = 20) ⇒ Hash
Create a hash with the exception message and backtrace.
-
.format_backtrace(exception, lines = 20) ⇒ String
Format a backtrace by joining all lines into a single line and quoting the results.
-
.format_hash(hash) ⇒ String
Format a Hash into key=value pairs, separated by whitespace.
-
.quote_unquoted(str) ⇒ String
Helper method to quote strings that need quoting (spaces in them, or embedded quotes) that have not already been quoted.
Class Method Details
.build_log_message(klass, obj, exception = nil, backtrace_lines = NUM_LINES_OF_EXCEPTIONS) ⇒ String
Helper method used to build up a single log message string you can pass to the underlying logger implementation.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/yeti_logger/message_formatters.rb', line 21 def self.(klass, obj, exception = nil, backtrace_lines = NUM_LINES_OF_EXCEPTIONS) msg = if obj.is_a?(Hash) if exception format_hash(obj.merge(exception_hash(exception, backtrace_lines))) else format_hash(obj) end elsif exception "#{obj} Exception: #{exception.} "\ "Error Class: #{exception.class.name} "\ "#{format_backtrace(exception, backtrace_lines)}" else obj end "#{klass}: #{msg}" end |
.exception_hash(exception, lines = 20) ⇒ Hash
Create a hash with the exception message and backtrace. You can merge this into an existing hash if you’re logging key=value pairs.
76 77 78 79 80 81 82 |
# File 'lib/yeti_logger/message_formatters.rb', line 76 def self.exception_hash(exception, lines = 20) { :error => exception., :error_class => exception.class.name, :backtrace => format_backtrace(exception, lines) } end |
.format_backtrace(exception, lines = 20) ⇒ String
Format a backtrace by joining all lines into a single line and quoting the results. You can optionally specify how many lines to include.
89 90 91 |
# File 'lib/yeti_logger/message_formatters.rb', line 89 def self.format_backtrace(exception, lines = 20) exception.try(:backtrace).try(:take, lines).try(:join, ', ').inspect end |
.format_hash(hash) ⇒ String
Format a Hash into key=value pairs, separated by whitespace. TODO: support nested hashes by serializing to JSON?
43 44 45 46 47 |
# File 'lib/yeti_logger/message_formatters.rb', line 43 def self.format_hash(hash) hash.map do |k, v| "#{k}=#{quote_unquoted(v.to_s)}" end.join(' ') end |
.quote_unquoted(str) ⇒ String
Helper method to quote strings that need quoting (spaces in them, or embedded quotes) that have not already been quoted.
53 54 55 56 57 58 59 |
# File 'lib/yeti_logger/message_formatters.rb', line 53 def self.quote_unquoted(str) if str && (!needs_quoting?(str) || quoted?(str)) str else str.inspect end end |