Module: HttpLog

Defined in:
lib/httplog/version.rb,
lib/httplog/http_log.rb,
lib/httplog/configuration.rb

Defined Under Namespace

Classes: Configuration

Constant Summary collapse

VERSION =
'1.1.1'.freeze
LOG_PREFIX =
'[httplog] '.freeze

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationObject Also known as: config, options



15
16
17
# File 'lib/httplog/http_log.rb', line 15

def configuration
  @configuration ||= Configuration.new
end

Class Method Details

.colorize(msg) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/httplog/http_log.rb', line 120

def colorize(msg)
  return msg unless config.color
  if config.color.is_a?(Hash)
    msg = Rainbow(msg).color(config.color[:color]) if config.color[:color]
    msg = Rainbow(msg).bg(config.color[:background]) if config.color[:background]
  else
    msg = Rainbow(msg).color(config.color)
  end
  msg
rescue
  warn "HTTPLOG CONFIGURATION ERROR: #{config.color} is not a valid color"
  msg
end

.configure {|configuration| ... } ⇒ Object

Yields:



25
26
27
# File 'lib/httplog/http_log.rb', line 25

def configure
  yield(configuration)
end

.log(msg) ⇒ Object



34
35
36
37
# File 'lib/httplog/http_log.rb', line 34

def log(msg)
  return unless config.enabled
  config.logger.log(config.severity, colorize(prefix + msg))
end

.log_benchmark(seconds) ⇒ Object



62
63
64
65
# File 'lib/httplog/http_log.rb', line 62

def log_benchmark(seconds)
  return if config.compact_log || !config.log_benchmark
  log("Benchmark: #{seconds.to_f.round(6)} seconds")
end

.log_body(body, encoding = nil, content_type = nil) ⇒ Object



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
# File 'lib/httplog/http_log.rb', line 67

def log_body(body, encoding = nil, content_type = nil)
  return if config.compact_log || !config.log_response

  unless text_based?(content_type)
    log('Response: (not showing binary data)')
    return
  end

  if body.is_a?(Net::ReadAdapter)
    # open-uri wraps the response in a Net::ReadAdapter that defers reading
    # the content, so the reponse body is not available here.
    log('Response: (not available yet)')
    return
  end

  if encoding =~ /gzip/ && body && !body.empty?
    begin
      sio = StringIO.new(body.to_s)
      gz = Zlib::GzipReader.new(sio)
      body = gz.read
    rescue Zlib::GzipFile::Error => e
      log("(gzip decompression failed)")
    end
  end

  data = utf_encoded(body.to_s, content_type)

  if config.prefix_response_lines
    log('Response:')
    log_data_lines(data)
  else
    log("Response:\n#{data}")
  end
end

.log_compact(method, uri, status, seconds) ⇒ Object



114
115
116
117
118
# File 'lib/httplog/http_log.rb', line 114

def log_compact(method, uri, status, seconds)
  return unless config.compact_log
  status = Rack::Utils.status_code(status) unless status == /\d{3}/
  log("#{method.to_s.upcase} #{uri} completed with status code #{status} in #{seconds} seconds")
end

.log_connection(host, port = nil) ⇒ Object



39
40
41
42
# File 'lib/httplog/http_log.rb', line 39

def log_connection(host, port = nil)
  return if config.compact_log || !config.log_connect
  log("Connecting: #{[host, port].compact.join(':')}")
end

.log_data(data) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
# File 'lib/httplog/http_log.rb', line 102

def log_data(data)
  return if config.compact_log || !config.log_data
  data = utf_encoded(data.to_s.dup)

  if config.prefix_data_lines
    log('Data:')
    log_data_lines(data)
  else
    log("Data: #{data}")
  end
end

.log_headers(headers = {}) ⇒ Object



49
50
51
52
53
54
# File 'lib/httplog/http_log.rb', line 49

def log_headers(headers = {})
  return if config.compact_log || !config.log_headers
  headers.each do |key, value|
    log("Header: #{key}: #{value}")
  end
end

.log_request(method, uri) ⇒ Object



44
45
46
47
# File 'lib/httplog/http_log.rb', line 44

def log_request(method, uri)
  return if config.compact_log || !config.log_request
  log("Sending: #{method.to_s.upcase} #{uri}")
end

.log_status(status) ⇒ Object



56
57
58
59
60
# File 'lib/httplog/http_log.rb', line 56

def log_status(status)
  return if config.compact_log || !config.log_status
  status = Rack::Utils.status_code(status) unless status == /\d{3}/
  log("Status: #{status}")
end

.reset!Object



21
22
23
# File 'lib/httplog/http_log.rb', line 21

def reset!
  @configuration = nil
end

.url_approved?(url) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
32
# File 'lib/httplog/http_log.rb', line 29

def url_approved?(url)
  return false if config.url_blacklist_pattern && url.to_s.match(config.url_blacklist_pattern)
  url.to_s.match(config.url_whitelist_pattern)
end