Class: NewRelic::Agent::AgentLogger

Inherits:
Object
  • Object
show all
Defined in:
lib/new_relic/agent/agent_logger.rb

Constant Summary collapse

LOG_LEVELS =
{
  "debug" => Logger::DEBUG,
  "info"  => Logger::INFO,
  "warn"  => Logger::WARN,
  "error" => Logger::ERROR,
  "fatal" => Logger::FATAL,
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, root = "", override_logger = nil) ⇒ AgentLogger

Returns a new instance of AgentLogger.



7
8
9
10
11
12
13
# File 'lib/new_relic/agent/agent_logger.rb', line 7

def initialize(config, root = "", override_logger=nil)
  create_log(config, root, override_logger)
  set_log_level!(config)
  set_log_format!

  gather_startup_logs
end

Class Method Details

.format_message(severity, timestamp, progname, msg) ⇒ Object



116
117
118
119
# File 'lib/new_relic/agent/agent_logger.rb', line 116

def @log.format_message(severity, timestamp, progname, msg)
  prefix = @logdev.dev == STDOUT ? '** [NewRelic]' : ''
  prefix + "[#{timestamp.strftime("%m/%d/%y %H:%M:%S %z")} #{Socket.gethostname} (#{$$})] #{severity} : #{msg}\n"
end

.log_level_for(level) ⇒ Object



111
112
113
# File 'lib/new_relic/agent/agent_logger.rb', line 111

def self.log_level_for(level)
  LOG_LEVELS.fetch(level.to_s.downcase, Logger::INFO)
end

Instance Method Details

#create_log(config, root, override_logger) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/new_relic/agent/agent_logger.rb', line 50

def create_log(config, root, override_logger)
  if !override_logger.nil?
    @log = override_logger
  elsif config[:agent_enabled] == false
    create_null_logger
  else
    if wants_stdout(config)
      @log = ::Logger.new(STDOUT)
    else
      create_log_to_file(config, root)
    end
  end
end

#create_log_to_file(config, root) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/new_relic/agent/agent_logger.rb', line 64

def create_log_to_file(config, root)
  path = find_or_create_file_path(config[:log_file_path], root)
  if path.nil?
    @log = ::Logger.new(STDOUT)
    warn("Error creating log directory #{config[:log_file_path]}, using standard out for logging.")
  else
    file_path = "#{path}/#{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_loggerObject



80
81
82
83
# File 'lib/new_relic/agent/agent_logger.rb', line 80

def create_null_logger
  null_path = ["/dev/null", "NUL"].detect{|f| File.exists?(f)}
  @log = ::Logger.new(null_path)
end

#debug(*msgs) ⇒ Object



31
32
33
# File 'lib/new_relic/agent/agent_logger.rb', line 31

def debug(*msgs)
  @log.debug(format_messages(msgs))
end

#error(*msgs) ⇒ Object



19
20
21
# File 'lib/new_relic/agent/agent_logger.rb', line 19

def error(*msgs)
  @log.error(format_messages(msgs))
end

#fatal(*msgs) ⇒ Object



15
16
17
# File 'lib/new_relic/agent/agent_logger.rb', line 15

def fatal(*msgs)
  @log.fatal(format_messages(msgs))
end

#find_or_create_file_path(path_setting, root) ⇒ Object



89
90
91
92
93
94
95
96
97
# File 'lib/new_relic/agent/agent_logger.rb', line 89

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
  nil
end

#format_messages(msgs) ⇒ Object

Allows for passing exceptions in explicitly, which format with backtrace



40
41
42
43
44
45
46
47
48
# File 'lib/new_relic/agent/agent_logger.rb', line 40

def format_messages(msgs)
  msgs.map do |msg|
    if msg.respond_to?(:backtrace)
      "#{msg.class}: #{msg}\n\t#{(msg.backtrace || []).join("\n\t")}"
    else
      msg
    end
  end.join("\n")
end

#gather_startup_logsObject



122
123
124
# File 'lib/new_relic/agent/agent_logger.rb', line 122

def gather_startup_logs
  StartupLogger.instance.dump(self)
end

#info(*msgs) ⇒ Object



27
28
29
# File 'lib/new_relic/agent/agent_logger.rb', line 27

def info(*msgs)
  @log.info(format_messages(msgs))
end

#is_startup_logger?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/new_relic/agent/agent_logger.rb', line 35

def is_startup_logger?
  false
end

#set_log_format!Object



115
116
117
118
119
120
# File 'lib/new_relic/agent/agent_logger.rb', line 115

def set_log_format!
  def @log.format_message(severity, timestamp, progname, msg)
    prefix = @logdev.dev == STDOUT ? '** [NewRelic]' : ''
    prefix + "[#{timestamp.strftime("%m/%d/%y %H:%M:%S %z")} #{Socket.gethostname} (#{$$})] #{severity} : #{msg}\n"
  end
end

#set_log_level!(config) ⇒ Object



99
100
101
# File 'lib/new_relic/agent/agent_logger.rb', line 99

def set_log_level!(config)
  @log.level = AgentLogger.log_level_for(config.fetch(:log_level))
end

#wants_stdout(config) ⇒ Object



85
86
87
# File 'lib/new_relic/agent/agent_logger.rb', line 85

def wants_stdout(config)
  config[:log_file_path].upcase == "STDOUT"
end

#warn(*msgs) ⇒ Object



23
24
25
# File 'lib/new_relic/agent/agent_logger.rb', line 23

def warn(*msgs)
  @log.warn(format_messages(msgs))
end