class Mmtrix::Agent::AuditLogger

Attributes

enabled[W]

Public Class Methods

new() click to toggle source
# File lib/mmtrix/agent/audit_logger.rb, line 12
def initialize
  @enabled = Mmtrix::Agent.config[:'audit_log.enabled']
  @endpoints = Mmtrix::Agent.config[:'audit_log.endpoints']
  @encoder = Mmtrix::Agent::MmtrixService::Encoders::Identity
end

Public Instance Methods

allowed_endpoint?(uri) click to toggle source
# File lib/mmtrix/agent/audit_logger.rb, line 46
def allowed_endpoint?(uri)
  @endpoints.any? { |endpoint| uri =~ endpoint }
end
create_log_formatter() click to toggle source
# File lib/mmtrix/agent/audit_logger.rb, line 85
def create_log_formatter
  @hostname = Mmtrix::Agent::Hostname.get
  @prefix = wants_stdout? ? '** [Mmtrix]' : ''
  Proc.new do |severity, time, progname, msg|
    "#{@prefix}[#{time} #{@hostname} (#{$$})] : #{msg}\n"
  end
end
enabled?() click to toggle source
# File lib/mmtrix/agent/audit_logger.rb, line 20
def enabled?
  @enabled
end
ensure_log_path() click to toggle source
# File lib/mmtrix/agent/audit_logger.rb, line 65
def ensure_log_path
  path = File.expand_path(Mmtrix::Agent.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}"
    ::Mmtrix::Agent.logger.warn(msg)
    path = nil
  end

  path
end
log_request(uri, data, marshaller) click to toggle source
# File lib/mmtrix/agent/audit_logger.rb, line 28
def log_request(uri, data, marshaller)
  return unless enabled? && allowed_endpoint?(uri)

  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}")
rescue StandardError, SystemStackError, SystemCallError => e
  ::Mmtrix::Agent.logger.warn("Failed writing to audit log", e)
rescue Exception => e
  ::Mmtrix::Agent.logger.warn("Failed writing to audit log with exception. Re-raising in case of interupt.", e)
  raise
end
setup?() click to toggle source
# File lib/mmtrix/agent/audit_logger.rb, line 24
def setup?
  !@log.nil?
end
setup_logger() click to toggle source
# File lib/mmtrix/agent/audit_logger.rb, line 50
def setup_logger
  if wants_stdout?
    # Using $stdout global for easier reassignment in testing
    @log = ::Logger.new($stdout)
    ::Mmtrix::Agent.logger.info("Audit log enabled to STDOUT")
  elsif path = ensure_log_path
    @log = ::Logger.new(path)
    ::Mmtrix::Agent.logger.info("Audit log enabled at '#{path}'")
  else
    @log = Mmtrix::Agent::NullLogger.new
  end

  @log.formatter = create_log_formatter
end
wants_stdout?() click to toggle source
# File lib/mmtrix/agent/audit_logger.rb, line 81
def wants_stdout?
  ::Mmtrix::Agent.config[:'audit_log.path'].upcase == "STDOUT"
end