Module: HttpLog

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

Defined Under Namespace

Classes: BodyParsingError, Configuration

Constant Summary collapse

VERSION =
'1.7.0'.freeze
LOG_PREFIX =
'[httplog] '.freeze
PARAM_MASK =
'[FILTERED]'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationObject Also known as: config



18
19
20
# File 'lib/httplog/http_log.rb', line 18

def configuration
  @configuration ||= Configuration.new
end

Class Method Details

.call(options = {}) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/httplog/http_log.rb', line 32

def call(options = {})
  parse_request(options)
  if config.json_log
    log_json(options)
  elsif config.graylog_formatter
    log_graylog(options)
  elsif config.compact_log
    log_compact(options[:method], options[:url], options[:response_code], options[:benchmark])
  else
    HttpLog.log_request(options[:method], options[:url])
    HttpLog.log_headers(options[:request_headers])
    HttpLog.log_data(options[:request_body])
    HttpLog.log_status(options[:response_code])
    HttpLog.log_benchmark(options[:benchmark])
    HttpLog.log_headers(options[:response_headers])
    HttpLog.log_body(options[:response_body], options[:mask_body], options[:encoding], options[:content_type])
  end
end

.colorize(msg) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/httplog/http_log.rb', line 177

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 StandardError
  warn "HTTPLOG CONFIGURATION ERROR: #{config.color} is not a valid color"
  msg
end

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

Yields:



27
28
29
30
# File 'lib/httplog/http_log.rb', line 27

def configure
  yield(configuration)
  configuration.json_parser ||= ::JSON if configuration.json_log || configuration.url_masked_body_pattern
end

.log(msg) ⇒ Object



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

def log(msg)
  return unless config.enabled

  config.logger.public_send(config.logger_method, config.severity, colorize(prefix + msg.to_s))
end

.log_benchmark(seconds) ⇒ Object



94
95
96
97
98
# File 'lib/httplog/http_log.rb', line 94

def log_benchmark(seconds)
  return unless config.log_benchmark

  log("Benchmark: #{seconds.to_f.round(6)} seconds")
end

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



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

def log_body(body, mask_body, encoding = nil, content_type = nil)
  return unless config.log_response

  data = parse_body(body, mask_body, encoding, content_type)

  if config.prefix_response_lines
    log('Response:')
    log_data_lines(data)
  else
    log("Response:\n#{data}")
  end
rescue BodyParsingError => e
  log("Response: #{e.message}")
end

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



167
168
169
170
171
# File 'lib/httplog/http_log.rb', line 167

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} #{masked(uri)} completed with status code #{status} in #{seconds.to_f.round(6)} seconds")
end

.log_connection(host, port = nil) ⇒ Object



67
68
69
70
71
# File 'lib/httplog/http_log.rb', line 67

def log_connection(host, port = nil)
  return if config.json_log || config.compact_log || !config.log_connect

  log("Connecting: #{[host, port].compact.join(':')}")
end

.log_data(data) ⇒ Object



156
157
158
159
160
161
162
163
164
165
# File 'lib/httplog/http_log.rb', line 156

def log_data(data)
  return unless config.log_data

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

.log_headers(headers = {}) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/httplog/http_log.rb', line 79

def log_headers(headers = {})
  return unless config.log_headers

  masked(headers).each do |key, value|
    log("Header: #{key}: #{value}")
  end
end

.log_request(method, uri) ⇒ Object



73
74
75
76
77
# File 'lib/httplog/http_log.rb', line 73

def log_request(method, uri)
  return unless config.log_request

  log("Sending: #{method.to_s.upcase} #{masked(uri)}")
end

.log_status(status) ⇒ Object



87
88
89
90
91
92
# File 'lib/httplog/http_log.rb', line 87

def log_status(status)
  return unless config.log_status

  status = Rack::Utils.status_code(status) unless status == /\d{3}/
  log("Status: #{status}")
end

.masked_body_url?(url) ⇒ Boolean

Returns:

  • (Boolean)


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

def masked_body_url?(url)
  config.filter_parameters.any? && config.url_masked_body_pattern && url.to_s.match(config.url_masked_body_pattern)
end

.parse_body(body, mask_body, encoding, content_type) ⇒ Object

Raises:



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/httplog/http_log.rb', line 115

def parse_body(body, mask_body, encoding, content_type)
  raise BodyParsingError, "(not showing binary data)" unless text_based?(content_type)


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

  body_copy = body.dup
  body_copy = body.to_s if defined?(HTTP::Response::Body) && body.is_a?(HTTP::Response::Body)
  return nil if body_copy.nil? || body_copy.empty?


  if encoding =~ /gzip/
    begin
      sio = StringIO.new(body_copy.to_s)
      gz = Zlib::GzipReader.new(sio)
      body_copy = gz.read
    rescue Zlib::GzipFile::Error
      log("(gzip decompression failed)")
    end
  end

  result = utf_encoded(body_copy.to_s, content_type)

  if mask_body
    if content_type =~ /json/
      result = begin
                 masked_data config.json_parser.load(result)
               rescue => e
                 'Failed to mask response body: ' + e.message
               end
    else
      result = masked(result)
    end
  end
  result
end

.reset!Object



23
24
25
# File 'lib/httplog/http_log.rb', line 23

def reset!
  @configuration = nil
end

.transform_response_code(response_code_name) ⇒ Object



173
174
175
# File 'lib/httplog/http_log.rb', line 173

def transform_response_code(response_code_name)
  Rack::Utils::HTTP_STATUS_CODES.detect { |_k, v| v.to_s.casecmp(response_code_name.to_s).zero? }.first
end

.url_approved?(url) ⇒ Boolean

Returns:

  • (Boolean)


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

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

  !config.url_whitelist_pattern || url.to_s.match(config.url_whitelist_pattern)
end