Class: Magick::AuditLog

Inherits:
Object
  • Object
show all
Defined in:
lib/magick/audit_log.rb

Defined Under Namespace

Classes: Entry

Constant Summary collapse

DEFAULT_MAX_ENTRIES =
10_000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adapter = nil, max_entries: DEFAULT_MAX_ENTRIES) ⇒ AuditLog

Returns a new instance of AuditLog.



31
32
33
34
35
36
# File 'lib/magick/audit_log.rb', line 31

def initialize(adapter = nil, max_entries: DEFAULT_MAX_ENTRIES)
  @adapter = adapter || default_adapter
  @logs = []
  @max_entries = max_entries.to_i.positive? ? max_entries.to_i : DEFAULT_MAX_ENTRIES
  @mutex = Mutex.new
end

Instance Attribute Details

#max_entriesObject (readonly)

Returns the value of attribute max_entries.



38
39
40
# File 'lib/magick/audit_log.rb', line 38

def max_entries
  @max_entries
end

Instance Method Details

#entries(feature_name: nil, limit: 100) ⇒ Object



57
58
59
60
61
# File 'lib/magick/audit_log.rb', line 57

def entries(feature_name: nil, limit: 100)
  snapshot = @mutex.synchronize { @logs.dup }
  snapshot = snapshot.select { |e| e.feature_name == feature_name.to_s } if feature_name
  snapshot.last(limit)
end

#log(feature_name, action, user_id: nil, changes: {}, metadata: {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/magick/audit_log.rb', line 40

def log(feature_name, action, user_id: nil, changes: {}, metadata: {})
  entry = Entry.new(feature_name, action, user_id: user_id, changes: changes, metadata: )
  @mutex.synchronize do
    @logs << entry
    # Cap in-memory ring; older entries fall out once we cross the limit.
    # This keeps long-running processes from growing @logs unboundedly.
    @logs.shift while @logs.size > @max_entries
    @adapter.append(entry) if @adapter.respond_to?(:append)
  end

  if defined?(Magick::Rails::Events) && Magick::Rails::Events.rails8?
    Magick::Rails::Events.audit_logged(feature_name, action: action, user_id: user_id, changes: changes, **)
  end

  entry
end

#sizeObject



63
64
65
# File 'lib/magick/audit_log.rb', line 63

def size
  @mutex.synchronize { @logs.size }
end