Class: StackifyRubyAPM::Spies::Log4rSpy Private

Inherits:
Object
  • Object
show all
Defined in:
lib/stackify_apm/spies/log4r.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Instance Method Summary collapse

Instance Method Details

#installObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/stackify_apm/spies/log4r.rb', line 8

def install
  Log4r::Outputter.module_eval do
    alias_method 'canonical_log_without_apm', 'canonical_log'

    # Log message formatted to a logevent object
    def canonical_log(logevent)
      return canonical_log_without_apm(logevent) unless StackifyRubyAPM.current_transaction

      begin
        name = 'log4r'
        type = 'ext.log4r'
        log_message = ''
        exception = nil
        msg = logevent.data

        case msg
        when ::String
          log_message = msg
        when ::Exception
          log_message = msg.message
          exception = "(#{ msg.class })\n#{ msg.backtrace.join("\n") if msg.backtrace }"
        else
          log_message = msg.inspect
        end

        ctx = Span::Context.new(
          CATEGORY: 'Log',
          SUBCATEGORY: 'Log4r',
          LEVEL: Log4r::LNAMES[logevent.level] || 'ANY',
          MESSAGE: log_message,
          PREFIX: 'TRUE'
        )

        if exception
          ctx.EXCEPTION = exception
        end
      rescue Exception => e
        StackifyRubyAPM.agent.error "[Log4rSpy] Error: creating span context."
        StackifyRubyAPM.agent.error "[Log4rSpy] #{e.inspect}"
        return canonical_log_without_apm(logevent)
      end

      StackifyRubyAPM.span name, type, context: ctx do
        canonical_log_without_apm(logevent)
      end
    end
  end
end