Class: RobotLab::To::JsonlLogger
- Inherits:
-
Object
- Object
- RobotLab::To::JsonlLogger
- Defined in:
- lib/robot_lab/to/jsonl_logger.rb
Overview
Writes structured JSONL events to the run log file.
Events emitted before the log file is ready are buffered in memory and flushed on first open (capacity: 100 lines).
Constant Summary collapse
- MAX_BUFFER =
100
Instance Method Summary collapse
- #close ⇒ Object
-
#initialize ⇒ JsonlLogger
constructor
A new instance of JsonlLogger.
- #log(event, **payload) ⇒ Object
- #open(path) ⇒ Object
Constructor Details
#initialize ⇒ JsonlLogger
Returns a new instance of JsonlLogger.
14 15 16 17 18 |
# File 'lib/robot_lab/to/jsonl_logger.rb', line 14 def initialize @path = nil @buffer = [] @file = nil end |
Instance Method Details
#close ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/robot_lab/to/jsonl_logger.rb', line 38 def close if @file @file.flush begin @file.fsync rescue SystemCallError nil end @file.close end @file = nil end |
#log(event, **payload) ⇒ Object
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/robot_lab/to/jsonl_logger.rb', line 27 def log(event, **payload) entry = { event: event, ts: Time.now.iso8601(3) }.merge(payload).compact line = JSON.generate(entry) if @file @file.write("#{line}\n") # single write keeps the line intact on crash else @buffer.shift if @buffer.size >= MAX_BUFFER @buffer << line end end |
#open(path) ⇒ Object
20 21 22 23 24 25 |
# File 'lib/robot_lab/to/jsonl_logger.rb', line 20 def open(path) @path = path @file = File.open(path, "a") @file.sync = true flush_buffer end |