Class: Captive::Presenter::LogLine

Inherits:
Object
  • Object
show all
Defined in:
lib/captive/presenter/log_line.rb

Overview

Parses a raw kubectl log line and renders it as a colorized terminal string.

Constant Summary collapse

BRACKET_RE =
/\A\[(?:pod\/)?(?<pod>[^\/\]]+)(?:\/(?<container>[^\/\]]+))?\]\z/

Instance Method Summary collapse

Constructor Details

#initialize(app:, paint: Pastel.new) ⇒ LogLine

Returns a new instance of LogLine.



12
13
14
15
# File 'lib/captive/presenter/log_line.rb', line 12

def initialize(app:, paint: Pastel.new)
  @app       = app
  @colorizer = LogColorizer.new(paint: paint)
end

Instance Method Details

#call(raw_line) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/captive/presenter/log_line.rb', line 17

def call(raw_line)
  line = raw_line.chomp
  first, second, message = line.split(" ", 3)
  return line if first.nil? || second.nil? || message.nil?

  bracket, ts = pick_bracket_and_timestamp(first, second)
  return line unless bracket

  label = LogUtils::ProcessLabel.for(pod: bracket[:pod], namespace: @app, container: bracket[:container])
  @colorizer.format_line(ts: ts, label: label, message: message)
end