Module: OllamaChat::Utils::LogViewer

Defined in:
lib/ollama_chat/utils/log_viewer.rb

Overview

Utilities for formatting, filtering, and displaying JSON log lines with colorized output and regex-based path matching.

Constant Summary collapse

Color =

ANSI color utility for terminal output.

Term::ANSIColor
LEVEL_COLORS =

Maps standard logging levels to their corresponding ANSI colors.

{
  'debug' => :green,
  'info'  => :blue,
  'warn'  => :yellow,
  'error' => :red,
  'fatal' => :magenta
}.freeze
DATA_COLORS =

Maps data types to their corresponding ANSI colors for structured payload # output.

{
  key:     :cyan,
  numeric: :green,
  boolean: :magenta,
  string:  :white
}.freeze

Class Method Summary collapse

Class Method Details

.format_line(line, display_data: true, match: nil) ⇒ String?

Formats a single JSON log line into a colorized, human-readable string.

Parameters:

  • line (String)

    The raw JSON log line.

  • display_data (Boolean) (defaults to: true)

    Whether to include the structured data payload.

  • match (String, Array<String>, nil) (defaults to: nil)

    Regex patterns to filter log entries by.

Returns:

  • (String, nil)

    The formatted log line, or nil if filtered out.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ollama_chat/utils/log_viewer.rb', line 35

def format_line(line, display_data: true, match: nil)
  return nil if line.blank?

  event = JSON.parse(line)

  level  = (event['level'] || 'info').downcase
  time   = event['time'] || ''
  msg    = event['msg'] || ''
  data   = event['data'] || {}
  prog   = event['progname']

  matched?(event:, match:) or return

  color = LEVEL_COLORS[level] || :white
  header = '[%s] %s %s: %s' % [
    time,
    Color.bold { Color.color(color) { level.upcase } },
    prog,
    msg,
  ]

  payload = ""
  if display_data && data.is_a?(Hash) && data.any?
    colorized_data = data.deep_transform(
      key:   -> k { Color.cyan { k } },
      value: -> v {
        if s = v.ask_and_send(:to_str)
          JSON.parse(s) rescue v
        else
          v
        end
      }
    )
    payload = "\n" + format_hash(colorized_data)
  end

  header + payload
rescue JSON::ParserError
  line unless match
end