Sha256: eb1c1bbcfb7d56d8b4cf5f0c0e37f72abace5250d75535133ef546ddac0a29d4

Contents?: true

Size: 2 KB

Versions: 10

Compression:

Stored size: 2 KB

Contents

# encoding: utf-8
# This file is distributed under New Relic's license terms.
# See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.

require 'logger'
require 'fileutils'

module NewRelic
  module Agent
    class AuditLogger
      def initialize
        @enabled = NewRelic::Agent.config[:'audit_log.enabled']
        @encoder = NewRelic::Agent::NewRelicService::Encoders::Identity
      end

      attr_writer :enabled

      def enabled?
        @enabled
      end

      def setup?
        !@log.nil?
      end

      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

      def setup_logger
        path = ensure_log_path
        if path
          @log = ::Logger.new(path)
          @log.formatter = create_log_formatter
          ::NewRelic::Agent.logger.info("Audit log enabled at '#{path}'")
        else
          @log = NewRelic::Agent::NullLogger.new
        end
      end

      def ensure_log_path
        path = File.expand_path(NewRelic::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}"
          ::NewRelic::Agent.logger.warn(msg)
          path = nil
        end

        path
      end

      def create_log_formatter
        @hostname = Socket.gethostname
        Proc.new do |severity, time, progname, msg|
          "[#{time} #{@hostname} (#{$$})] : #{msg}\n"
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
newrelic_rpm-3.8.1.221 lib/new_relic/agent/audit_logger.rb
newrelic_rpm-3.8.0.218 lib/new_relic/agent/audit_logger.rb
newrelic_rpm-3.7.3.204 lib/new_relic/agent/audit_logger.rb
newrelic_rpm-3.7.3.199 lib/new_relic/agent/audit_logger.rb
newrelic_rpm-3.7.2.195 lib/new_relic/agent/audit_logger.rb
newrelic_rpm-3.7.2.192 lib/new_relic/agent/audit_logger.rb
newrelic_rpm-3.7.2.190.beta lib/new_relic/agent/audit_logger.rb
newrelic_rpm-3.7.1.188 lib/new_relic/agent/audit_logger.rb
newrelic_rpm-3.7.1.182 lib/new_relic/agent/audit_logger.rb
newrelic_rpm-3.7.1.180 lib/new_relic/agent/audit_logger.rb