Class: Hatchet::StructuredFormatter
- Inherits:
-
Object
- Object
- Hatchet::StructuredFormatter
- Includes:
- BacktraceFormatter
- Defined in:
- lib/hatchet/structured_formatter.rb
Overview
Public: Structured formatter class. Outputs messages as JSON strings.
Instance Attribute Summary
Attributes included from BacktraceFormatter
Instance Method Summary collapse
-
#format(level, context, message) ⇒ Object
Public: Returns the formatted message.
-
#initialize ⇒ StructuredFormatter
constructor
Public: Creates a new instance.
Methods included from BacktraceFormatter
Constructor Details
#initialize ⇒ StructuredFormatter
Public: Creates a new instance.
14 15 16 17 18 19 |
# File 'lib/hatchet/structured_formatter.rb', line 14 def initialize @backtrace = true @secs = 0 @millis = -1 @level_cache = {} 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:
%Y-%m-%d %H:%M:%S.%L [THREAD] LEVEL CONTEXT - MESSAGE
BACKTRACE
The backtrace is only present if the message contains an error.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/hatchet/structured_formatter.rb', line 34 def format(level, context, ) msg = . case msg when Hash # Assume caller is following conventions log = msg.dup else # Otherwise treat as String log = { :message => msg.to_s.strip } end log[:timestamp] = log[:level] = format_level(level) log[:pid] = Process.pid unless Thread.current == Thread.main log[:thread] = Thread.current.object_id end log[:context] = context if .ndc.any? log[:ndc] = .ndc.to_a end if .error error = .error log[:error_class] = error.class.to_s log[:error_message] = error. log[:error_backtrace] if error.respond_to?(:backtrace) backtrace = error.backtrace backtrace = backtrace.take(backtrace_limit) if backtrace_limit log[:error_backtrace] = backtrace.join("\n") end end JSON.generate(log.to_h) end |