Class: RailsAiBridge::Tools::ReadLogs::TailFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_ai_bridge/tools/read_logs.rb

Overview

Formats a log tail: file metadata plus the redacted, capped tail lines.

The file is consumed in a single streaming pass; tail lines are kept in a buffer capped at the requested length, so memory stays bounded no matter how large the log file is.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_io, lines:, detail:) ⇒ TailFormatter

Returns a new instance of TailFormatter.

Parameters:

  • file_io (File) —

    opened log file descriptor (binary mode)

  • lines (Integer, nil) —

    requested tail length

  • detail (String) —

    detail level



89
90
91
92
93
# File 'lib/rails_ai_bridge/tools/read_logs.rb', line 89

def initialize(file_io, lines:, detail:)
  @file_io = file_io
  @lines = lines
  @detail = detail
end

Class Method Details

.append_to_tail(tail_lines, line, cap) ⇒ void

This method returns an undefined value.

Appends a redacted line to the tail buffer, maintaining the cap.

Parameters:

  • tail_lines (Array<String>) —

    current tail buffer

  • line (String) —

    raw line to redact and append

  • cap (Integer) —

    maximum buffer size



134
135
136
137
# File 'lib/rails_ai_bridge/tools/read_logs.rb', line 134

def self.append_to_tail(tail_lines, line, cap)
  tail_lines << redact(line)
  tail_lines.shift while tail_lines.size > cap
end

.redact(line) ⇒ String

Byte-caps, scrubs, and redacts a single line.

Parameters:

  • line (String) —

    raw line from the log file

Returns:

  • (String) —

    redacted line



111
112
113
114
115
# File 'lib/rails_ai_bridge/tools/read_logs.rb', line 111

def self.redact(line)
  trimmed = line.force_encoding(Encoding::UTF_8).scrub
  sanitized = Registry::MessageSanitizer.sanitize(trimmed.chomp)
  truncate_to_valid_encoding(sanitized)
end

Instance Method Details

#format ⇒ Hash

Builds the payload.

Returns:

  • (Hash) —

    compact payload with file, size_bytes, total_lines and (for non-summary detail) redacted tail lines



99
100
101
102
103
104
105
# File 'lib/rails_ai_bridge/tools/read_logs.rb', line 99

def format
  return build_summary if @detail == 'summary'

  build_tail
ensure
  @file_io.close
end