Class: NewRelic::Agent::AuditLogger

Inherits:
Object
  • Object
show all
Defined in:
lib/new_relic/agent/audit_logger.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ AuditLogger

Returns a new instance of AuditLogger.



7
8
9
10
11
# File 'lib/new_relic/agent/audit_logger.rb', line 7

def initialize(config)
  @config = config
  @enabled = @config[:'audit_log.enabled']
  @encoder = NewRelic::Agent::NewRelicService::Encoders::Identity
end

Instance Attribute Details

#enabled=(value) ⇒ Object (writeonly)

Sets the attribute enabled

Parameters:

  • value

    the value to set the attribute enabled to.



13
14
15
# File 'lib/new_relic/agent/audit_logger.rb', line 13

def enabled=(value)
  @enabled = value
end

Instance Method Details

#enabled?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/new_relic/agent/audit_logger.rb', line 15

def enabled?
  @enabled
end

#ensure_log_pathObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/new_relic/agent/audit_logger.rb', line 45

def ensure_log_path
  path = File.expand_path(@config[:'audit_log.path'])
  log_dir = File.dirname(path)

  begin
    FileUtils.mkdir_p(log_dir)
    FileUtils.touch(path)
  rescue SystemCallError => e
    msg = "Audit log disabled, failed opening log at '#{path}': #{e}"
    ::NewRelic::Agent.logger.warn(msg)
    path = nil
  end

  path
end

#log_formatterObject



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

def log_formatter
  if @formatter.nil?
    @formatter = Logger::Formatter.new
    def @formatter.call(severity, time, progname, msg)
      "[#{time} #{Socket.gethostname} (#{$$})] : #{msg}\n"
    end
  end
  @formatter
end

#log_request(uri, data, marshaller) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/new_relic/agent/audit_logger.rb', line 23

def log_request(uri, data, marshaller)
  if enabled?
    setup_logger unless setup?
    request_body = if marshaller.class.human_readable?
      marshaller.dump(data, :encoder => @encoder)
    else
      marshaller.prepare(data, :encoder => @encoder).inspect
    end
    @log.info("REQUEST: #{uri}")
    @log.info("REQUEST BODY: #{request_body}")
  end
rescue SystemCallError => e
  ::NewRelic::Agent.logger.warn("Failed writing to audit log: #{e}")
end

#setup?Boolean

Returns:

  • (Boolean)


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

def setup?
  !@log.nil?
end

#setup_loggerObject



38
39
40
41
42
43
# File 'lib/new_relic/agent/audit_logger.rb', line 38

def setup_logger
  path = ensure_log_path
  @log = ::Logger.new(path || "/dev/null")
  @log.formatter = log_formatter
  ::NewRelic::Agent.logger.info("Audit log enabled at '#{path}'") if path
end