class NewRelic::Agent::AgentLogger

Public Class Methods

new(root = "", override_logger=nil) click to toggle source
# File lib/new_relic/agent/agent_logger.rb, line 14
def initialize(root = "", override_logger=nil)
  @already_logged_lock = Mutex.new
  clear_already_logged
  create_log(root, override_logger)
  set_log_level!
  set_log_format!

  gather_startup_logs
end

Public Instance Methods

debug(*msgs, &blk) click to toggle source
# File lib/new_relic/agent/agent_logger.rb, line 40
def debug(*msgs, &blk)
  format_and_send(:debug, msgs, &blk)
end
error(*msgs, &blk) click to toggle source
# File lib/new_relic/agent/agent_logger.rb, line 28
def error(*msgs, &blk)
  format_and_send(:error, msgs, &blk)
end
fatal(*msgs, &blk) click to toggle source
# File lib/new_relic/agent/agent_logger.rb, line 24
def fatal(*msgs, &blk)
  format_and_send(:fatal, msgs, &blk)
end
info(*msgs, &blk) click to toggle source
# File lib/new_relic/agent/agent_logger.rb, line 36
def info(*msgs, &blk)
  format_and_send(:info, msgs, &blk)
end
is_startup_logger?() click to toggle source
# File lib/new_relic/agent/agent_logger.rb, line 44
def is_startup_logger?
  @log.is_a?(NullLogger)
end
log_exception(level, e, backtrace_level=level) click to toggle source

Use this when you want to log an exception with explicit control over the log level that the backtrace is logged at. If you just want the default behavior of backtraces logged at debug, use one of the methods above and pass an Exception as one of the args.

# File lib/new_relic/agent/agent_logger.rb, line 52
def log_exception(level, e, backtrace_level=level)
  @log.send(level, "%p: %s" % [ e.class, e.message ])
  @log.send(backtrace_level) do
    backtrace = backtrace_from_exception(e)
    if backtrace
      "Debugging backtrace:\n" + backtrace.join("\n  ")
    else
      "No backtrace available."
    end
  end
end
log_formatter=(formatter) click to toggle source
# File lib/new_relic/agent/agent_logger.rb, line 64
def log_formatter=(formatter)
  @log.formatter = formatter
end
warn(*msgs, &blk) click to toggle source
# File lib/new_relic/agent/agent_logger.rb, line 32
def warn(*msgs, &blk)
  format_and_send(:warn, msgs, &blk)
end

Private Instance Methods

backtrace_from_exception(e) click to toggle source
# File lib/new_relic/agent/agent_logger.rb, line 70
def backtrace_from_exception(e)
  # We've seen that often the backtrace on a SystemStackError is bunk
  # so massage the caller instead at a known depth.
  #
  # Tests keep us honest about minmum method depth our log calls add.
  return caller.drop(5) if e.is_a?(SystemStackError)

  e.backtrace
end
create_log(root, override_logger) click to toggle source
# File lib/new_relic/agent/agent_logger.rb, line 99
def create_log(root, override_logger)
  if !override_logger.nil?
    @log = override_logger
  elsif ::NewRelic::Agent.config[:agent_enabled] == false
    create_null_logger
  else
    if wants_stdout?
      @log = ::Logger.new(STDOUT)
    else
      create_log_to_file(root)
    end
  end
end
create_log_to_file(root) click to toggle source
# File lib/new_relic/agent/agent_logger.rb, line 113
def create_log_to_file(root)
  path = find_or_create_file_path(::NewRelic::Agent.config[:log_file_path], root)
  if path.nil?
    @log = ::Logger.new(STDOUT)
    warn("Error creating log directory #{::NewRelic::Agent.config[:log_file_path]}, using standard out for logging.")
  else
    file_path = "#{path}/#{::NewRelic::Agent.config[:log_file_name]}"
    begin
      @log = ::Logger.new(file_path)
    rescue => e
      @log = ::Logger.new(STDOUT)
      warn("Failed creating logger for file #{file_path}, using standard out for logging.", e)
    end
  end
end
create_null_logger() click to toggle source
# File lib/new_relic/agent/agent_logger.rb, line 129
def create_null_logger
  @log = ::NewRelic::Agent::NullLogger.new
end
find_or_create_file_path(path_setting, root) click to toggle source
# File lib/new_relic/agent/agent_logger.rb, line 137
def find_or_create_file_path(path_setting, root)
  for abs_path in [ File.expand_path(path_setting),
                    File.expand_path(File.join(root, path_setting)) ] do
    if File.directory?(abs_path) || (Dir.mkdir(abs_path) rescue nil)
      return abs_path[%r{^(.*?)/?$}]
    end
  end
format_and_send(level, *msgs, &block) click to toggle source

Allows for passing exceptions in explicitly, which format with backtrace

# File lib/new_relic/agent/agent_logger.rb, line 81
def format_and_send(level, *msgs, &block)
  if block
    if @log.send("#{level}?")
      msgs = Array(block.call)
    else
      msgs = []
    end
  end

  msgs.flatten.each do |item|
    case item
    when Exception then log_exception(level, item, :debug)
    else @log.send(level, item)
    end
  end
  nil
end
wants_stdout?() click to toggle source
# File lib/new_relic/agent/agent_logger.rb, line 133
def wants_stdout?
  ::NewRelic::Agent.config[:log_file_path].upcase == "STDOUT"
end