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.



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



135
136
137
138
# File 'lib/new_relic/agent/agent_logger.rb', line 135

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



130
131
132
# File 'lib/new_relic/agent/agent_logger.rb', line 130

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



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/new_relic/agent/agent_logger.rb', line 70

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



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/new_relic/agent/agent_logger.rb', line 84

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



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

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



108
109
110
111
112
113
114
115
116
# File 'lib/new_relic/agent/agent_logger.rb', line 108

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_and_send(level, *msgs) ⇒ Object

Allows for passing exceptions in explicitly, which format with backtrace



61
62
63
64
65
66
67
68
# File 'lib/new_relic/agent/agent_logger.rb', line 61

def format_and_send(level, *msgs)
  msgs.flatten.each do |item|
    case item
    when Exception then log_exception(level, item, :debug)
    else @log.send(level, item)
    end
  end
end

#gather_startup_logsObject



141
142
143
# File 'lib/new_relic/agent/agent_logger.rb', line 141

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

Returns:

  • (Boolean)


40
41
42
# File 'lib/new_relic/agent/agent_logger.rb', line 40

def is_startup_logger?
  false
end

#log_exception(level, e, backtrace_level = level) ⇒ Object

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.



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/new_relic/agent/agent_logger.rb', line 48

def log_exception(level, e, backtrace_level=level)
  @log.send(level, "%p: %s" % [ e.class, e.message ])
  @log.send(backtrace_level) do
    backtrace = e.backtrace
    if backtrace
      "Debugging backtrace:\n" + backtrace.join("\n  ")
    else
      "No backtrace available."
    end
  end
end

#set_log_format!Object



134
135
136
137
138
139
# File 'lib/new_relic/agent/agent_logger.rb', line 134

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



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

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

#wants_stdout(config) ⇒ Object



104
105
106
# File 'lib/new_relic/agent/agent_logger.rb', line 104

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