Class: PromptSanitizer::Audit::MemoryAuditLog
- Defined in:
- lib/prompt_sanitizer/audit/memory_audit_log.rb
Overview
Thread-safe in-memory audit log backend.
Events are stored in a plain Array protected by a Mutex. Data is lost on process restart — use this for development, testing, or short-lived jobs. For persistence, implement a custom backend (e.g. ActiveRecord, SQLite) using Audit::Base.
Usage:
log = PromptSanitizer::Audit::MemoryAuditLog.new
sanitizer = PromptSanitizer.sanitizer(audit_log: log)
sanitizer.sanitize("contact [email protected]")
log.count # => 1
log.export # => "[{\"entity_type\":\"email\", ...}]"
Instance Method Summary collapse
- #clear ⇒ Object
- #count(since: nil) ⇒ Integer
-
#events ⇒ Array<AuditEvent>
A frozen snapshot (thread-safe copy).
- #export(format: :json, since: nil, session_id: nil) ⇒ String
-
#initialize ⇒ MemoryAuditLog
constructor
A new instance of MemoryAuditLog.
- #record(event) ⇒ Object
Constructor Details
#initialize ⇒ MemoryAuditLog
Returns a new instance of MemoryAuditLog.
20 21 22 23 24 |
# File 'lib/prompt_sanitizer/audit/memory_audit_log.rb', line 20 def initialize super @mutex = Mutex.new @events = [] end |
Instance Method Details
#clear ⇒ Object
64 65 66 67 |
# File 'lib/prompt_sanitizer/audit/memory_audit_log.rb', line 64 def clear @mutex.synchronize { @events.clear } nil end |
#count(since: nil) ⇒ Integer
60 61 62 |
# File 'lib/prompt_sanitizer/audit/memory_audit_log.rb', line 60 def count(since: nil) _filter(since: since).length end |
#events ⇒ Array<AuditEvent>
Returns a frozen snapshot (thread-safe copy).
33 34 35 |
# File 'lib/prompt_sanitizer/audit/memory_audit_log.rb', line 33 def events @mutex.synchronize { @events.dup } end |
#export(format: :json, since: nil, session_id: nil) ⇒ String
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/prompt_sanitizer/audit/memory_audit_log.rb', line 41 def export(format: :json, since: nil, session_id: nil) rows = _filter(since: since, session_id: session_id).map(&:to_h) case format.to_sym when :json JSON.generate(rows) when :csv return "" if rows.empty? fields = rows.first.keys lines = [fields.join(",")] rows.each { |r| lines << fields.map { |f| r[f].to_s }.join(",") } lines.join("\n") else raise ArgumentError, "Unknown format: #{format.inspect}. Use :json or :csv" end end |
#record(event) ⇒ Object
27 28 29 30 |
# File 'lib/prompt_sanitizer/audit/memory_audit_log.rb', line 27 def record(event) @mutex.synchronize { @events << event } nil end |