Class: Logstream::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/logstream/client.rb

Constant Summary collapse

GREEN =
'32;1'
RED =
'31;1'
YELLOW =
'33;1'
BLUE =
'34;1'
CYAN =
'46;37;1'
LOG_TYPE_COLORS =
{
  'apache-request' => {
    /^5/ => RED,
    /^4/ => YELLOW,
    /^[123]/ => GREEN,
  },
  'bal-access' => {
    /^5/ => RED,
    /^4/ => YELLOW,
    /^[123]/ => GREEN,
  },
  'apache-error' => RED,
  'php-error' => RED,
  'drupal-watchdog' => BLUE,
  'logtailor-error' => RED,
  'logtailor-debug' => CYAN,
}

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Client

Returns a new instance of Client.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/logstream/client.rb', line 7

def initialize(opts = {})
  opts = { 
    :logger => Logger.new(STDOUT),
    :log_prefix => '',
    :shows => {},
    :hides => {},
    :columns => [ 'text' ],
    :no_color => false,
    :debug => false,
  }.merge(opts)
  @opts = opts
  @columns = {
    :type => "%-15s",
    :disp_time => "%s",
    :server => "%-8s",
    :text => "%s",
  }
end

Instance Method Details

#color(type, status) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/logstream/client.rb', line 123

def color(type, status)
  color = LOG_TYPE_COLORS[type]
  if color.is_a? Hash
    color = color.find { |k,v| status.to_s =~ k }[1] rescue nil
  end
  color = nil if @opts[:no_color]
  begin
    print "\e[#{color}m" if color
    yield
  ensure
    print "\e[0m" if color
  end
end

#debug(msg) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/logstream/client.rb', line 26

def debug(msg)
  if @opts[:debug]
    color('logtailor-debug', nil) do
      puts msg
    end
  end
end

#debug_recv(msg) ⇒ Object



38
39
40
# File 'lib/logstream/client.rb', line 38

def debug_recv(msg)
  debug("<- #{msg}")
end

#debug_send(msg) ⇒ Object



34
35
36
# File 'lib/logstream/client.rb', line 34

def debug_send(msg)
  debug("-> #{msg}")
end

#run(url, connect_message) ⇒ Object



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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/logstream/client.rb', line 42

def run(url, connect_message)
  EM.run do
    debug_send("connect to #{url}")
    ws = Faye::WebSocket::Client.new(url)
    ws.on :open do
      debug_send(connect_message)
      ws.send(connect_message)
    end
    ws.on :message do |body,type|
      debug_recv(body.data)
      msg = JSON.parse(body.data)
      case msg['cmd']
      when 'success'
        color('logtailor-error', msg['code']) do
          # puts "#{msg.inspect}"
        end
      when 'error'
        color('logtailor-error', msg['code']) do
          puts "#{msg.inspect}"
        end
        ws.close
        EM.stop
      when 'available'
        send_msg(ws, { 'cmd' => 'enable', 'type' => msg['type'], 'server' => msg['server'] }) if @opts[:types].include?(msg['type'])
      when 'line'
        next unless msg.all? { |k,v| @opts[:shows][k].nil? || v.to_s =~ @opts[:shows][k] }
        next if msg.any? { |k,v| @opts[:hides][k] && v.to_s =~ @opts[:hides][k] }
        p = ''
        color(msg['log_type'], msg['http_status']) do
          @opts[:columns].each do |column|
            print("#{p}#{@columns[column.to_sym] || '%s'}" % [ msg[column.to_s] ])
            p = ' '
          end
        end
        puts
      end
    end
    ws.on :close do
      @opts[:logger].info "#{@opts[:log_prefix]}: connection closed"
      ws.close
      EM.stop
    end
    ws.on :error do |error|
      @opts[:logger].info "#{@opts[:log_prefix]}: error: #{error.message}"
      ws.close
      EM.stop
    end
  end
rescue Interrupt
  # exit cleanly
end

#send_msg(ws, msg) ⇒ Object



94
95
96
97
98
# File 'lib/logstream/client.rb', line 94

def send_msg(ws, msg)
  body = msg.to_json
  debug_send(body)
  ws.send(body)
end