Class: Chalk::Log::Layout

Inherits:
Logging::Layout
  • Object
show all
Defined in:
lib/chalk-log/layout.rb

Overview

The layout backend for the Logging::Logger.

Accepts a message and/or an exception and/or an info hash (if multiple are passed, they must be provided in that order)

Instance Method Summary collapse

Instance Method Details

#format(event) ⇒ Object

Formats an event, and makes a heroic effort to tell you if something went wrong. (Logging will otherwise silently swallow any exceptions that get thrown.)



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/chalk-log/layout.rb', line 16

def format(event)
  begin
    begin
      begin
        do_format(event)
      rescue StandardError => e
        # Single fault!
        error!('[Chalk::Log fault: Could not format message] ', e)
      end
    rescue StandardError => e
      # Double fault!
      "[Chalk::Log fault: Double fault while formatting message. This means we couldn't even report the error we got while formatting.] #{e.message}\n"
    end
  rescue StandardError => e
    # Triple fault!
    "[Chalk::Log fault: Triple fault while formatting message. This means we couldn't even report the error we got while reporting the original error.]\n"
  end
end

#format_hash(hash) ⇒ Object

Formats a hash for logging. This is provided for (rare) use outside of log methods; you can pass a hash directly to log methods and this formatting will automatically be applied.



40
41
42
# File 'lib/chalk-log/layout.rb', line 40

def format_hash(hash)
  hash.map {|k, v| display(k, v)}.join(' ')
end