Class: Tire::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/tire/logger.rb

Instance Method Summary collapse

Constructor Details

#initialize(device, options = {}) ⇒ Logger

Returns a new instance of Logger.



5
6
7
8
9
10
11
12
13
14
# File 'lib/tire/logger.rb', line 5

def initialize(device, options={})
  @device = if device.respond_to?(:write)
    device
  else
    File.open(device, 'a')
  end
  @device.sync = true if @device.respond_to?(:sync)
  @options = options
  at_exit { @device.close unless @device.closed? } if @device.respond_to?(:closed?) && @device.respond_to?(:close)
end

Instance Method Details

#levelObject



16
17
18
# File 'lib/tire/logger.rb', line 16

def level
  @options[:level] || 'info'
end

#log_request(endpoint, params = nil, curl = '') ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/tire/logger.rb', line 24

def log_request(endpoint, params=nil, curl='')
  # 2001-02-12 18:20:42:32 [_search] (articles,users)
  #
  # curl -X POST ....
  #
  content  = "# #{time}"
  content += " [#{endpoint}]"
  content += " (#{params.inspect})" if params
  content += "\n#\n"
  content += curl
  content += "\n\n"
  write content
end

#log_response(status, took = nil, json = '') ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/tire/logger.rb', line 38

def log_response(status, took=nil, json='')
  # 2001-02-12 18:20:42:32 [200] (4 msec)
  #
  # {
  #   "took" : 4,
  #   "hits" : [...]
  #   ...
  # }
  #
  content  = "# #{time}"
  content += " [#{status}]"
  content += " (#{took} msec)" if took
  content += "\n#\n" unless json.to_s !~ /\S/
  json.to_s.each_line { |line| content += "# #{line}" } unless json.to_s !~ /\S/
  content += "\n\n"
  write content
end

#timeObject



56
57
58
# File 'lib/tire/logger.rb', line 56

def time
  Time.now.strftime('%Y-%m-%d %H:%M:%S:%L')
end

#write(message) ⇒ Object



20
21
22
# File 'lib/tire/logger.rb', line 20

def write(message)
  @device.write message
end