class TingYun::Logger::AgentLogger

Constants

LOG_LEVELS

Attributes

file_path[R]

Public Class Methods

new(root = "", override_logger=nil) click to toggle source
# File lib/ting_yun/logger/agent_logger.rb, line 18
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

Private Class Methods

log_level_for(level) click to toggle source
# File lib/ting_yun/logger/agent_logger.rb, line 122
def self.log_level_for(level)
  LOG_LEVELS.fetch(level.to_s.downcase, ::Logger::INFO)
end

Public Instance Methods

debug(*msgs, &blk) click to toggle source
# File lib/ting_yun/logger/agent_logger.rb, line 44
def debug(*msgs, &blk)
  format_and_send(:debug, msgs, &blk)
end
error(*msgs, &blk) click to toggle source
# File lib/ting_yun/logger/agent_logger.rb, line 32
def error(*msgs, &blk)
  format_and_send(:error, msgs, &blk)
end
fatal(*msgs, &blk) click to toggle source
# File lib/ting_yun/logger/agent_logger.rb, line 28
def fatal(*msgs, &blk)
  format_and_send(:fatal, msgs, &blk)
end
info(*msgs, &blk) click to toggle source
# File lib/ting_yun/logger/agent_logger.rb, line 40
def info(*msgs, &blk)
  format_and_send(:info, msgs, &blk)
end
is_startup_logger?() click to toggle source
# File lib/ting_yun/logger/agent_logger.rb, line 48
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/ting_yun/logger/agent_logger.rb, line 56
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
warn(*msgs, &blk) click to toggle source
# File lib/ting_yun/logger/agent_logger.rb, line 36
def warn(*msgs, &blk)
  format_and_send(:warn, msgs, &blk)
end

Private Instance Methods

backtrace_from_exception(e) click to toggle source
# File lib/ting_yun/logger/agent_logger.rb, line 71
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
format_and_send(level, *msgs, &block) click to toggle source

Allows for passing exception.rb in explicitly, which format with backtrace

# File lib/ting_yun/logger/agent_logger.rb, line 82
def format_and_send(level, *msgs, &block)
  check_log_file
  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
gather_startup_logs() click to toggle source

send the statup log info from memory to the agent log

# File lib/ting_yun/logger/agent_logger.rb, line 135
def gather_startup_logs
  StartupLogger.instance.dump(self)
end
set_log_format() click to toggle source
# File lib/ting_yun/logger/agent_logger.rb, line 126
def set_log_format
  hostname = TingYun::Support::Hostname.get
  prefix = wants_stdout? ? '** [TingYun]' : ''
  @log.formatter = Proc.new do |severity, timestamp, progname, msg|
    "#{prefix}[#{timestamp.strftime("%m/%d/%y %H:%M:%S %z")} #{hostname} (#{$$})] #{severity} : #{msg}\n"
  end
end
set_log_level() click to toggle source
# File lib/ting_yun/logger/agent_logger.rb, line 110
def set_log_level
  @log.level = AgentLogger.log_level_for(::TingYun::Agent.config[:agent_log_level])
end
wants_stdout?() click to toggle source
# File lib/ting_yun/logger/agent_logger.rb, line 104
def wants_stdout?
  ::TingYun::Agent.config[:agent_log_file_name].upcase == "STDOUT"
end