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



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

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



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

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



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

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



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

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



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

def create_null_logger
  @log = NewRelic::Agent::NullLogger.new
end

#debug(*msgs) ⇒ Object



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

def debug(*msgs)
  format_and_send(:debug, msgs)
end

#error(*msgs) ⇒ Object



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

def error(*msgs)
  format_and_send(:error, msgs)
end

#fatal(*msgs) ⇒ Object



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

def fatal(*msgs)
  format_and_send(:fatal, msgs)
end

#find_or_create_file_path(path_setting, root) ⇒ Object



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

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



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

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



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

def gather_startup_logs
  StartupLogger.instance.dump(self)
end

#info(*msgs) ⇒ Object



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

def info(*msgs)
  format_and_send(:info, msgs)
end

#is_startup_logger?Boolean

Returns:

  • (Boolean)


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

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.



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

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



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

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



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

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

#wants_stdout(config) ⇒ Object



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

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

#warn(*msgs) ⇒ Object



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

def warn(*msgs)
  format_and_send(:warn, msgs)
end