Class: Zlog::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/zlog/cli.rb

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ CLI

Returns a new instance of CLI.



4
5
6
7
8
9
10
11
# File 'lib/zlog/cli.rb', line 4

def initialize opts = {}
  @layout = case opts[:layout]
    when :named
      Zlog::Layouts.named
    else
      Zlog::Layouts.simple
    end
end

Instance Method Details

#convert_line(line, opts = {}) ⇒ Object



13
14
15
16
17
18
# File 'lib/zlog/cli.rb', line 13

def convert_line line, opts = {}
  e = Zlog::json_2_event(line)
  if not e.nil?
    @layout.format(e)
  end
end

#convert_stdin(opts = {}, &handler) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/zlog/cli.rb', line 20

def convert_stdin opts = {}, &handler
  # collect all results to an array which will be returned
  # in case that no block is given for processing
  res = []
  handler = lambda{|line| res.push line} if not block_given?

  # process each line
  while line = STDIN.gets do
    handler.( convert_line line, opts )
  end

  # return the result, in case we have any
  res
end