Class: LoggingElf::TracingLogger

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/logging_elf/tracing_logger.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger, &trace_hash) ⇒ TracingLogger

Returns a new instance of TracingLogger.



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/logging_elf/tracing_logger.rb', line 28

def initialize(logger, &trace_hash)
  @logger = Logging::Logger.new logger if logger.is_a? String
  self.trace_hash = if block_given?
                      trace_hash
                    elsif LoggingElf.config.trace_hash
                      LoggingElf.config.trace_hash
                    else
                      fail "TracingLogger cannot be created with no" \
                        " mechanism for appending trace data"
                    end
end

Instance Attribute Details

#trace_hashObject



41
42
43
44
45
46
47
48
49
# File 'lib/logging_elf/tracing_logger.rb', line 41

def trace_hash
  hash = if @trace_hash.respond_to?(:call)
           @trace_hash.call
         else
           @trace_hash
         end

  hash || {}
end

Instance Method Details

#append_trace_info(data) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/logging_elf/tracing_logger.rb', line 68

def append_trace_info(data)
  data = case data
         when Hash then data
         when Exception then exception_data(data)
         when String then { message: data }
         when nil then return
         else
           default_data(data)
         end
  data.merge(trace_hash)
end

#default_data(data) ⇒ Object



61
62
63
64
65
66
# File 'lib/logging_elf/tracing_logger.rb', line 61

def default_data(data)
  {
    short_message: "Unknown thing to log",
    message: data.inspect
  }
end

#exception_data(data) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/logging_elf/tracing_logger.rb', line 51

def exception_data(data)
  {
    error_object: data,
    backtrace: data.backtrace,
    short_message: "Exception: #{data.message}",
    message: data.message,
    is_exception: true
  }
end