Class: Legate::ActivityLog
- Inherits:
-
Object
- Object
- Legate::ActivityLog
- Defined in:
- lib/legate/activity_log.rb
Overview
Simple in-memory activity log for tracking system events.
Constant Summary collapse
- MAX_EVENTS =
50
Class Method Summary collapse
- .clear ⇒ Object
- .instance ⇒ Object
-
.log(event_type, details = {}) ⇒ Object
Delegate class methods to instance.
- .recent(limit = 10) ⇒ Object
-
.safe_log(event_type, details = {}) ⇒ Object
Records an event, swallowing any error — activity logging must never break the request that triggered it.
Instance Method Summary collapse
-
#clear ⇒ Object
Clear all events.
-
#initialize ⇒ ActivityLog
constructor
A new instance of ActivityLog.
-
#log(event_type, details = {}) ⇒ Object
Log a new event.
-
#recent(limit = 10) ⇒ Array<Hash>
Get recent events.
Constructor Details
#initialize ⇒ ActivityLog
Returns a new instance of ActivityLog.
35 36 37 38 |
# File 'lib/legate/activity_log.rb', line 35 def initialize @events = [] @mutex = Mutex.new end |
Class Method Details
.clear ⇒ Object
30 31 32 |
# File 'lib/legate/activity_log.rb', line 30 def clear instance.clear end |
.instance ⇒ Object
9 10 11 |
# File 'lib/legate/activity_log.rb', line 9 def instance @instance ||= new end |
.log(event_type, details = {}) ⇒ Object
Delegate class methods to instance
14 15 16 |
# File 'lib/legate/activity_log.rb', line 14 def log(event_type, details = {}) instance.log(event_type, details) end |
.recent(limit = 10) ⇒ Object
26 27 28 |
# File 'lib/legate/activity_log.rb', line 26 def recent(limit = 10) instance.recent(limit) end |
.safe_log(event_type, details = {}) ⇒ Object
Records an event, swallowing any error — activity logging must never break the request that triggered it.
20 21 22 23 24 |
# File 'lib/legate/activity_log.rb', line 20 def safe_log(event_type, details = {}) log(event_type, details) rescue StandardError => e Legate.logger&.debug { "ActivityLog: failed to record #{event_type}: #{e.}" } end |
Instance Method Details
#clear ⇒ Object
Clear all events
65 66 67 68 69 |
# File 'lib/legate/activity_log.rb', line 65 def clear @mutex.synchronize do @events = [] end end |
#log(event_type, details = {}) ⇒ Object
Log a new event
43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/legate/activity_log.rb', line 43 def log(event_type, details = {}) @mutex.synchronize do event = { type: event_type.to_sym, details: details, timestamp: Time.now.utc } @events.unshift(event) @events = @events.first(MAX_EVENTS) end end |
#recent(limit = 10) ⇒ Array<Hash>
Get recent events
58 59 60 61 62 |
# File 'lib/legate/activity_log.rb', line 58 def recent(limit = 10) @mutex.synchronize do @events.first(limit) end end |