Sha256: bbbcb1f4444b40e6f9daf8cb6d4f6e765ae89652356d6a71d887630352004c69

Contents?: true

Size: 1.84 KB

Versions: 19

Compression:

Stored size: 1.84 KB

Contents

require 'logger'
require 'fileutils'

module NewRelic
  module Agent
    class AuditLogger
      def initialize(config)
        @config = config
        @enabled = @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
        @log = ::Logger.new(path || "/dev/null")
        @log.formatter = log_formatter
        ::NewRelic::Agent.logger.info("Audit log enabled at '#{path}'") if path
      end

      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

      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
    end
  end
end

Version data entries

19 entries across 19 versions & 3 rubygems

Version Path
wd_newrelic_rpm-3.5.8 lib/new_relic/agent/audit_logger.rb
sundawg_newrelic_rpm-3.5.8.2 lib/new_relic/agent/audit_logger.rb
sundawg_newrelic_rpm-3.5.8.1 lib/new_relic/agent/audit_logger.rb
newrelic_rpm-3.5.8.72 lib/new_relic/agent/audit_logger.rb
newrelic_rpm-3.5.8.70 lib/new_relic/agent/audit_logger.rb
newrelic_rpm-3.5.8.64.beta lib/new_relic/agent/audit_logger.rb
newrelic_rpm-3.5.7.59 lib/new_relic/agent/audit_logger.rb
newrelic_rpm-3.5.7.59.beta lib/new_relic/agent/audit_logger.rb
newrelic_rpm-3.5.7.58.beta lib/new_relic/agent/audit_logger.rb
wd_newrelic_rpm-3.5.6 lib/new_relic/agent/audit_logger.rb
newrelic_rpm-3.5.7.57.beta lib/new_relic/agent/audit_logger.rb
newrelic_rpm-3.5.6.55 lib/new_relic/agent/audit_logger.rb
newrelic_rpm-3.5.6.48.beta lib/new_relic/agent/audit_logger.rb
newrelic_rpm-3.5.6.46.beta lib/new_relic/agent/audit_logger.rb
newrelic_rpm-3.5.6.42.beta lib/new_relic/agent/audit_logger.rb
newrelic_rpm-3.5.5.39.beta lib/new_relic/agent/audit_logger.rb
wd_newrelic_rpm-3.5.5 lib/new_relic/agent/audit_logger.rb
newrelic_rpm-3.5.5.38 lib/new_relic/agent/audit_logger.rb
newrelic_rpm-3.5.5.540.dev lib/new_relic/agent/audit_logger.rb