Class: NewRelic::Agent::AgentLogger
- Inherits:
-
Object
- Object
- NewRelic::Agent::AgentLogger
- 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
- #create_log(config, root, override_logger) ⇒ Object
- #create_log_to_file(config, root) ⇒ Object
- #create_null_logger ⇒ Object
- #debug(*msgs) ⇒ Object
- #error(*msgs) ⇒ Object
- #fatal(*msgs) ⇒ Object
- #find_or_create_file_path(path_setting, root) ⇒ Object
-
#format_and_send(level, *msgs) ⇒ Object
Allows for passing exceptions in explicitly, which format with backtrace.
- #gather_startup_logs ⇒ Object
- #info(*msgs) ⇒ Object
-
#initialize(config, root = "", override_logger = nil) ⇒ AgentLogger
constructor
A new instance of AgentLogger.
- #is_startup_logger? ⇒ Boolean
- #set_log_format! ⇒ Object
- #set_log_level!(config) ⇒ Object
- #wants_stdout(config) ⇒ Object
- #warn(*msgs) ⇒ Object
Constructor Details
#initialize(config, root = "", override_logger = nil) ⇒ AgentLogger
Returns a new instance of AgentLogger.
11 12 13 14 15 16 17 |
# File 'lib/new_relic/agent/agent_logger.rb', line 11 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
130 131 132 133 |
# File 'lib/new_relic/agent/agent_logger.rb', line 130 def @log.(severity, , progname, msg) prefix = @logdev.dev == STDOUT ? '** [NewRelic]' : '' prefix + "[#{.strftime("%m/%d/%y %H:%M:%S %z")} #{Socket.gethostname} (#{$$})] #{severity} : #{msg}\n" end |
.log_level_for(level) ⇒ Object
125 126 127 |
# File 'lib/new_relic/agent/agent_logger.rb', line 125 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
65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/new_relic/agent/agent_logger.rb', line 65 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
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/new_relic/agent/agent_logger.rb', line 79 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_logger ⇒ Object
95 96 97 |
# File 'lib/new_relic/agent/agent_logger.rb', line 95 def create_null_logger @log = NewRelic::Agent::NullLogger.new end |
#debug(*msgs) ⇒ Object
36 37 38 |
# File 'lib/new_relic/agent/agent_logger.rb', line 36 def debug(*msgs) format_and_send(:debug, msgs) end |
#error(*msgs) ⇒ Object
24 25 26 |
# File 'lib/new_relic/agent/agent_logger.rb', line 24 def error(*msgs) format_and_send(:error, msgs) end |
#fatal(*msgs) ⇒ Object
20 21 22 |
# File 'lib/new_relic/agent/agent_logger.rb', line 20 def fatal(*msgs) format_and_send(:fatal, msgs) end |
#find_or_create_file_path(path_setting, root) ⇒ Object
103 104 105 106 107 108 109 110 111 |
# File 'lib/new_relic/agent/agent_logger.rb', line 103 def find_or_create_file_path(path_setting, root) for abs_path in [ File.(path_setting), File.(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_and_send(level, *msgs) ⇒ Object
Allows for passing exceptions in explicitly, which format with backtrace
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/new_relic/agent/agent_logger.rb', line 45 def format_and_send(level, *msgs) msgs.flatten! exceptions = msgs.find_all {|m| m.is_a?(Exception) } msgs.collect! do |msg| if msg.respond_to?(:message) "%p: %s" % [ msg.class, msg. ] else msg.to_s end end msgs.each do |msg| @log.send level, msg end exceptions.each do |ex| @log.debug { "Debugging backtrace:\n " + ex.backtrace.join("\n ") } end end |
#gather_startup_logs ⇒ Object
136 137 138 |
# File 'lib/new_relic/agent/agent_logger.rb', line 136 def gather_startup_logs StartupLogger.instance.dump(self) end |
#info(*msgs) ⇒ Object
32 33 34 |
# File 'lib/new_relic/agent/agent_logger.rb', line 32 def info(*msgs) format_and_send(:info, msgs) end |
#is_startup_logger? ⇒ Boolean
40 41 42 |
# File 'lib/new_relic/agent/agent_logger.rb', line 40 def is_startup_logger? false end |
#set_log_format! ⇒ Object
129 130 131 132 133 134 |
# File 'lib/new_relic/agent/agent_logger.rb', line 129 def set_log_format! def @log.(severity, , progname, msg) prefix = @logdev.dev == STDOUT ? '** [NewRelic]' : '' prefix + "[#{.strftime("%m/%d/%y %H:%M:%S %z")} #{Socket.gethostname} (#{$$})] #{severity} : #{msg}\n" end end |
#set_log_level!(config) ⇒ Object
113 114 115 |
# File 'lib/new_relic/agent/agent_logger.rb', line 113 def set_log_level!(config) @log.level = AgentLogger.log_level_for(config.fetch(:log_level)) end |
#wants_stdout(config) ⇒ Object
99 100 101 |
# File 'lib/new_relic/agent/agent_logger.rb', line 99 def wants_stdout(config) config[:log_file_path].upcase == "STDOUT" end |
#warn(*msgs) ⇒ Object
28 29 30 |
# File 'lib/new_relic/agent/agent_logger.rb', line 28 def warn(*msgs) format_and_send(:warn, msgs) end |