Class: Datadog::Logger

Inherits:
Logger
  • Object
show all
Defined in:
lib/ddtrace/logger.rb

Overview

A custom logger with minor enhancements:

  • progname defaults to ddtrace to clearly identify Datadog dd-trace-rb related messages

  • adds last caller stack-trace info to know where the message comes from

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ Logger

Returns a new instance of Logger.



52
53
54
55
# File 'lib/ddtrace/logger.rb', line 52

def initialize(*args, &block)
  super
  self.progname = LOG_PREFIX
end

Class Method Details

.debug_loggingObject

Return if the debug mode is activated or not



47
48
49
# File 'lib/ddtrace/logger.rb', line 47

def debug_logging
  log.level == Logger::DEBUG
end

.debug_logging=(value) ⇒ Object

Activate the debug mode providing more information related to tracer usage Default to Warn level unless using custom logger



38
39
40
41
42
43
44
# File 'lib/ddtrace/logger.rb', line 38

def debug_logging=(value)
  if value
    log.level = Logger::DEBUG
  elsif log.is_a?(Datadog::Logger)
    log.level = Logger::WARN
  end
end

.logObject



13
14
15
16
17
18
19
# File 'lib/ddtrace/logger.rb', line 13

def log
  unless defined? @logger
    @logger = Datadog::Logger.new(STDOUT)
    @logger.level = Logger::WARN
  end
  @logger
end

.log=(logger) ⇒ Object

Override the default logger with a custom one.



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ddtrace/logger.rb', line 22

def log=(logger)
  return unless logger
  return unless logger.respond_to? :methods
  return unless logger.respond_to? :error
  if logger.respond_to? :methods
    unimplemented = new(STDOUT).methods - logger.methods
    unless unimplemented.empty?
      logger.error("logger #{logger} does not implement #{unimplemented}")
      return
    end
  end
  @logger = logger
end

Instance Method Details

#add(severity, message = nil, progname = nil, &block) ⇒ Object Also known as: log



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/ddtrace/logger.rb', line 57

def add(severity, message = nil, progname = nil, &block)
  where = ''

  # We are in debug mode, or this is an error, add stack trace to help debugging
  if debug? || severity >= ::Logger::ERROR
    c = caller
    where = "(#{c[1]}) " if c.length > 1
  end

  if message.nil?
    if block_given?
      super(severity, message, progname) do
        "[#{self.progname}] #{where}#{yield}"
      end
    else
      super(severity, message, "[#{self.progname}] #{where}#{progname}")
    end
  else
    super(severity, "[#{self.progname}] #{where}#{message}")
  end
end