Module: OllamaChat::Utils::LogRotation
- Defined in:
- lib/ollama_chat/utils/log_rotation.rb
Overview
Truncates a log file in-place, keeping only the last N lines.
Uses File::Tail#backward to seek to the start of the tail portion
without loading the entire file into memory. The file is rewritten
in-place (same inode) so that tailing processes (e.g.
ollama_chat_log) can continue following the file.
Class Method Summary collapse
-
.truncate_tail(path, keep_lines:) ⇒ Object
Truncates a log file in-place, keeping only the last
keep_lines.
Class Method Details
.truncate_tail(path, keep_lines:) ⇒ Object
Truncates a log file in-place, keeping only the last keep_lines.
If the file has fewer lines than keep_lines, it is left untouched.
The operation is a no-op if the file does not exist.
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/ollama_chat/utils/log_rotation.rb', line 17 def self.truncate_tail(path, keep_lines:) return unless File.exist?(path) File.open(path, 'r+') do |f| f.extend(File::Tail) f.backward(keep_lines) return if f.pos.zero? tail = f.read f.rewind f.write(tail) f.truncate(f.pos) end end |