# encoding: utf-8 require 'logger' require 'fileutils' require 'one_apm/support/hostname' module OneApm module Logger class AuditLogger def initialize @enabled = OneApm::Manager.config[:'audit_log.enabled'] @encoder = OneApm::Support::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 StandardError, SystemStackError, SystemCallError => e OneApm::Manager.logger.warn("Failed writing to audit log", e) rescue Exception => e OneApm::Manager.logger.warn("Failed writing to audit log with exception. Re-raising in case of interupt.", e) raise end def setup_logger path = ensure_log_path if path @log = ::Logger.new(path) @log.formatter = create_log_formatter OneApm::Manager.logger.info("Audit log enabled at '#{path}'") else @log = OneApm::Logger::NullLogger.new end end def ensure_log_path path = File.expand_path(OneApm::Manager.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}" OneApm::Manager.logger.warn(msg) path = nil end path end def create_log_formatter @hostname = OneApm::Agent::Hostname.get Proc.new do |severity, time, progname, msg| "[#{time} #{@hostname} (#{$$})] : #{msg}\n" end end end end end