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



133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/logstream/client.rb', line 133

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, info) ⇒ 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
93
94
95
96
97
98
99
100
101
102
# File 'lib/logstream/client.rb', line 42

def run(url, info)
  EM.run do
    debug_send("connect to #{url}")
    connect_message = {
      'cmd' => 'stream-environment',
      'site' => info['site'],
      'env' => info['environment'],
      't' => info['t'],
      'd' => info['hmac'],
    }
    ws = Faye::WebSocket::Client.new(url)
    ws.on :open do
      @running = false
    end
    ws.on :message do |body,type|
      debug_recv(body.data)
      msg = JSON.parse(body.data)
      case msg['cmd']
      when 'connected'
        debug_send(connect_message.to_json)
        ws.send(connect_message.to_json) unless @running
        @running = true
      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



104
105
106
107
108
# File 'lib/logstream/client.rb', line 104

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