Module: NewRelic::Control::LoggingMethods

Included in:
NewRelic::Control
Defined in:
lib/new_relic/control/logging_methods.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#log_fileObject

Returns the value of attribute log_file.



6
7
8
# File 'lib/new_relic/control/logging_methods.rb', line 6

def log_file
  @log_file
end

Instance Method Details

#logObject



8
9
10
11
12
13
14
15
16
# File 'lib/new_relic/control/logging_methods.rb', line 8

def log
  # If we try to get a log before one has been set up, return a stdout log
  unless @log
    l = Logger.new(STDOUT)
    l.level = Logger::INFO
    return l
  end
  @log
end

#log!(msg, level = :info) ⇒ Object

send the given message to STDOUT so that it shows up in the console. This should be used for important informational messages at boot. The to_stdout may be implemented differently by different config subclasses. This will NOT print anything if tracers are not enabled



22
23
24
25
26
# File 'lib/new_relic/control/logging_methods.rb', line 22

def log!(msg, level=:info)
  return unless should_log?
  to_stdout msg
  log.send level, msg if @log
end

#log_file_nameObject



69
70
71
# File 'lib/new_relic/control/logging_methods.rb', line 69

def log_file_name
  fetch('log_file_name', 'newrelic_agent.log')
end

#log_pathObject



60
61
62
63
64
65
66
67
# File 'lib/new_relic/control/logging_methods.rb', line 60

def log_path
  return if @log_path
  @log_path = File.expand_path(fetch('log_file_path', 'log/'))
  if !File.directory?(@log_path) && ! (Dir.mkdir(@log_path) rescue nil)
    log!("Error creating New Relic log directory '#{@log_path}'", :error)
  end
  @log_path
end

#setup_logObject

Control subclasses may override this, but it can be called multiple times.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/new_relic/control/logging_methods.rb', line 33

def setup_log
  @log_file = "#{log_path}/#{log_file_name}"
  @log = Logger.new(@log_file) rescue nil

  # change the format just for our logger

  def log.format_message(severity, timestamp, progname, msg)
    "[#{timestamp.strftime("%m/%d/%y %H:%M:%S %z")} #{Socket.gethostname} (#{$$})] #{severity} : #{msg}\n"
  end

  # set the log level as specified in the config file

  case fetch("log_level","info").downcase
    when "debug" then log.level = Logger::DEBUG
    when "info" then log.level = Logger::INFO
    when "warn" then log.level = Logger::WARN
    when "error" then log.level = Logger::ERROR
    when "fatal" then log.level = Logger::FATAL
  else log.level = Logger::INFO
  end
  log
end

#should_log?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/new_relic/control/logging_methods.rb', line 28

def should_log?
  @settings && agent_enabled?
end

#to_stdout(msg) ⇒ Object



56
57
58
# File 'lib/new_relic/control/logging_methods.rb', line 56

def to_stdout(msg)
  STDOUT.puts "** [NewRelic] " + msg
end