class StackifyRubyAPM::Spies::Log4rSpy

Constants

EXCEPTION

Public Instance Methods

canonical_log(logevent) click to toggle source

Log message formatted to a logevent object

# File lib/stackify_apm/spies/log4r.rb, line 13
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
install() click to toggle source
# 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