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

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.

Parameters:

  • klass (String)

    Name of the class you are logging on behalf

  • obj (Object)

    Object to log, may be nil

  • exception (Exception) (defaults to: nil)

    Optional exception to include in the log message

Returns:

  • (String)

    to log



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.build_log_message(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.message} "\
          "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.

Parameters:

  • exception (Exception)

    The Exception you want to create a hash for.

  • lines (Integer) (defaults to: 20)

    How many lines of the backtrace to keep.

Returns:

  • (Hash)

    Hash with exception details in it.



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.message,
    :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.

Parameters:

  • exception (Exception)

    The Exception you want to convert to a string

  • lines (Integer) (defaults to: 20)

    How many lines of the backtrace to keep.

Returns:

  • (String)

    String of the backtrace.



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?

Parameters:

  • hash (Hash)

    Hash to serialize into a key=value string

Returns:

  • (String)

    string representation of hash



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.

Parameters:

  • str (String)

    string to quote if it has spaces within it

Returns:

  • (String)

    original string, or quoted version if necessary



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