Class: Ask::Rails::Tools::ReadLog

Inherits:
Ask::Rails::Tool show all
Defined in:
lib/ask/rails/tools/read_log.rb

Constant Summary collapse

MAX_LINES =
500
LEVEL_PATTERNS =
{
  "ERROR" => /\bERROR\b/,
  "WARN"  => /\bWARN\b/,
  "INFO"  => /\bINFO\b/,
  "DEBUG" => /\bDEBUG\b/
}.freeze

Instance Method Summary collapse

Methods inherited from Ask::Rails::Tool

#rails_root

Instance Method Details

#execute(lines: 50, level: nil, search: nil, file: nil) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ask/rails/tools/read_log.rb', line 23

def execute(lines: 50, level: nil, search: nil, file: nil)
  lines = [lines.to_i, MAX_LINES].min
  log_path = resolve_log_path(file)

  unless log_path.exist?
    return Ask::Result.failure(
      "Log file not found: #{log_path}. The application may not have written any logs yet."
    )
  end

  content = read_tail(log_path, MAX_LINES * 2)
  return { lines: [], total_lines: 0, path: log_path.to_s } if content.empty?

  raw_lines = content.lines

  filtered = apply_filters(raw_lines, level: level, search: search)
  recent = filtered.last(lines).map(&:chomp)

  {
    lines: recent,
    total_lines: raw_lines.size,
    matched_lines: filtered.size,
    path: log_path.to_s,
    filters_applied: { level: level, search: search }.compact
  }
end