Module: NewRelic::Control::LoggingMethods

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

Overview

Contains methods that relate to locating, creating, and writing to the log file and/or standard out

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#log_fileObject

Returns the value of attribute log_file.



8
9
10
# File 'lib/new_relic/control/logging_methods.rb', line 8

def log_file
  @log_file
end

Instance Method Details

#find_or_create_file_path(path_setting) ⇒ Object



109
110
111
112
113
114
115
116
117
# File 'lib/new_relic/control/logging_methods.rb', line 109

def find_or_create_file_path(path_setting)
  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

#logObject

returns either the log set up with setup_log or else a new logger pointing to standard out, if we’re trying to log before a log exists



13
14
15
16
17
18
19
20
# File 'lib/new_relic/control/logging_methods.rb', line 13

def log
  if !@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



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

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

#log_pathObject

Sets up and caches the log path, attempting to create it if it does not exist. If it does not succeed, it prints an error and returns nil. This comes from the configuration variable ‘log_file_path’ in the configuration file.



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/new_relic/control/logging_methods.rb', line 97

def log_path
  return @log_path if @log_path
  if log_to_stdout?
    @log_path = nil
  else
    log_path_setting = Agent.config[:log_file_path]
    @log_path = find_or_create_file_path(log_path_setting)
    log!("Error creating log directory #{log_path_setting}, using standard out for logging.", :warn) unless @log_path
  end
  @log_path
end

#log_to_stdout?Boolean

Returns:

  • (Boolean)


119
120
121
# File 'lib/new_relic/control/logging_methods.rb', line 119

def log_to_stdout?
  Agent.config['log_file_path'].upcase == 'STDOUT'
end

#set_log_format!(logger) ⇒ Object

patches the logger’s format_message method to change the format just for our logger



56
57
58
59
60
61
62
# File 'lib/new_relic/control/logging_methods.rb', line 56

def set_log_format!(logger)
  def logger.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
  logger
end

#set_log_level!(logger) ⇒ Object

set the log level as specified in the config file

Possible values are from the Logger class: debug, info, warn, error, and fatal Defaults to info



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/new_relic/control/logging_methods.rb', line 43

def set_log_level!(logger)
  case Agent.config[:log_level].downcase
    when "debug" then logger.level = Logger::DEBUG
    when "info" then logger.level = Logger::INFO
    when "warn" then logger.level = Logger::WARN
    when "error" then logger.level = Logger::ERROR
    when "fatal" then logger.level = Logger::FATAL
    else logger.level = Logger::INFO
  end
  logger
end

#setup_logObject

Create the logger using the configuration variables

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



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/new_relic/control/logging_methods.rb', line 67

def setup_log
  if log_to_stdout?
    @log = Logger.new(STDOUT) 
  else
    @log_file = "#{log_path}/#{Agent.config[:log_file_name]}"
    @log = Logger.new(@log_file) rescue nil
    @log ||= Logger.new(STDOUT) # failsafe to STDOUT
  end
  
  if @log
    set_log_format!(@log)
    set_log_level!(@log)
  end
  # note this is not the variable from above - it is the `log`
  # method, which returns a default logger if none is setup
  # above
  log
end

#should_log?Boolean

true if the agent has settings, and the agent is enabled, otherwise false

Returns:

  • (Boolean)


34
35
36
# File 'lib/new_relic/control/logging_methods.rb', line 34

def should_log?
  @settings && Agent.config[:agent_enabled]
end

#to_stdout(msg) ⇒ Object

simply puts a message to standard out, prepended with the ‘** [NewRelic]’ sigil to make sure people know where the message comes from. This should be used sparingly



89
90
91
# File 'lib/new_relic/control/logging_methods.rb', line 89

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