module SoarAuditorApi class AuditorAPI DEBUG_PREFIX = "debug:" unless defined? DEBUG_PREFIX; DEBUG_PREFIX.freeze INFO_PREFIX = "info:" unless defined? INFO_PREFIX; INFO_PREFIX.freeze WARN_PREFIX = "warn:" unless defined? WARN_PREFIX; WARN_PREFIX.freeze ERROR_PREFIX = "error:" unless defined? ERROR_PREFIX; ERROR_PREFIX.freeze FATAL_PREFIX = "fatal:" unless defined? FATAL_PREFIX; FATAL_PREFIX.freeze def initialize @configuration = nil @minimum_audit_level = :info @audit_levels = [:debug, :info, :warn, :error, :fatal] end def configure(configuration = nil) raise ArgumentError, "Invalid configuration provided" unless configuration_is_valid(configuration) @configuration = configuration end def set_audit_level(minimum_audit_level) raise ArgumentError, "Invalid audit level specified" unless @audit_levels.include?(minimum_audit_level) @minimum_audit_level = minimum_audit_level end def debug(data) audit(DEBUG_PREFIX + data.to_s) if audit_filtered(:debug) end def <<(data) audit(INFO_PREFIX + data.to_s) if audit_filtered(:info) end def info(data) audit(INFO_PREFIX + data.to_s) if audit_filtered(:info) end def warn(data) audit(WARN_PREFIX + data.to_s) if audit_filtered(:warn) end def error(data) audit(ERROR_PREFIX + data.to_s) if audit_filtered(:error) end def fatal(data) audit(FATAL_PREFIX + data.to_s) if audit_filtered(:fatal) end #Safety to ensure that the Auditor that extends this API implements this IOC method def configuration_is_valid(configuration) raise NotImplementedError, "Method must implement configuration_is_valid method in Auditor extending the API" end #Safety to ensure that the Auditor that extends this API implements this IOC method def audit(data) raise NotImplementedError, "Method must implement audit method in Auditor extending the API" end private def audit_filtered(audit_level) return @audit_levels.index(@minimum_audit_level) <= @audit_levels.index(audit_level) end end end