Class: Hatchet::SimpleFormatter

Inherits:
Object
  • Object
show all
Includes:
BacktraceFormatter, ThreadNameFormatter
Defined in:
lib/hatchet/simple_formatter.rb

Overview

Public: Simple formatter class. Outputs messages with just level, context, and message.

Instance Attribute Summary collapse

Attributes included from BacktraceFormatter

#backtrace_limit

Instance Method Summary collapse

Methods included from BacktraceFormatter

#backtrace, #backtrace=

Constructor Details

#initializeSimpleFormatter

Public: Initialize a new instance.



19
20
21
# File 'lib/hatchet/simple_formatter.rb', line 19

def initialize
  @backtrace = true
end

Instance Attribute Details

#thread_contextObject

Public: Gets or sets whether the context of the thread (pid and thread ID) should be included into the output messages.



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

def thread_context
  @thread_context
end

Instance Method Details

#format(level, context, message) ⇒ Object

Public: Returns the formatted message.

level - The severity of the log message. context - The context of the log message. message - The message provided by the log caller.

Returns messages in the format:

[THREAD] - LEVEL - CONTEXT - MESSAGE
    BACKTRACE

The backtrace is only present if the message contains an error and the presence of the context of the thread context is managed via the #thread_context attribute.



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/hatchet/simple_formatter.rb', line 38

def format(level, context, message)
  msg = message.to_s.strip
  thread = thread_context ? "[#{thread_name}] - " : nil

  if message.ndc.any?
    msg = "#{thread}#{level.to_s.upcase} - #{context} #{message.ndc.join(' ')} - #{msg}"
  else
    msg = "#{thread}#{level.to_s.upcase} - #{context} - #{msg}"
  end

  with_backtrace(message, msg)
end