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
-
.format_hash(hash, indent = 2) ⇒ String
private
Recursively formats a hash into an indented, multi-line string.
-
.format_line(line, display_data: true, match: nil) ⇒ String?
Formats a single JSON log line into a colorized, human-readable string.
-
.matched?(event:, match:) ⇒ Boolean
private
Checks if a log event matches the provided regex patterns.
Class Method Details
.format_hash(hash, indent = 2) ⇒ String (private)
Recursively formats a hash into an indented, multi-line string.
114 115 116 |
# File 'lib/ollama_chat/utils/log_viewer.rb', line 114 def format_hash(hash, indent = 2) ComplexConfig::Tree.convert(Color.bold { '🪵 data' }, hash) end |
.format_line(line, display_data: true, match: nil) ⇒ String?
Formats a single JSON log line into a colorized, human-readable string.
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 |
.matched?(event:, match:) ⇒ Boolean (private)
Checks if a log event matches the provided regex patterns.
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/ollama_chat/utils/log_viewer.rb', line 83 def matched?(event:, match:) match or return true match.to_a.all? do |m| /\A(?<path>[^=]+)?(?:=(?<value_match>.*))?/ =~ m if path = path.split(?.).full? value = event while idx = path.shift case value when Array value = value[idx.to_i] when Hash value = value[idx] else break end end path.empty? or return if value_match value.ask_and_send(:match?, value_match) else value.present? end end end end |