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.



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

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



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

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

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



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

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



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

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



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

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

#write(message) ⇒ Object



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

def write(message)
  @device.write message
end