Class: MintHttp::RequestLogger
- Inherits:
-
Object
- Object
- MintHttp::RequestLogger
- Defined in:
- lib/mint_http/request_logger.rb
Constant Summary collapse
- MAX_BODY_SIZE =
15 * 1024
- BODY_ALLOWED_TYPES =
[/application\/.+/, /text\/.+/]
Instance Attribute Summary collapse
-
#filter_list ⇒ Object
readonly
Returns the value of attribute filter_list.
-
#local_address ⇒ Object
readonly
Returns the value of attribute local_address.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#remote_address ⇒ Object
readonly
Returns the value of attribute remote_address.
-
#time_connected ⇒ Object
readonly
Returns the value of attribute time_connected.
-
#time_connecting ⇒ Object
readonly
Returns the value of attribute time_connecting.
-
#time_ended ⇒ Object
readonly
Returns the value of attribute time_ended.
-
#time_started ⇒ Object
readonly
Returns the value of attribute time_started.
-
#time_total ⇒ Object
readonly
Returns the value of attribute time_total.
-
#tls_config ⇒ Object
readonly
Returns the value of attribute tls_config.
Instance Method Summary collapse
-
#initialize(logger, filter_list, filter = true) ⇒ RequestLogger
constructor
A new instance of RequestLogger.
- #log_connected ⇒ Object
- #log_connection_info(http) ⇒ Object
- #log_end ⇒ Object
- #log_error(error) ⇒ Object
- #log_request(request, net_request) ⇒ Object
- #log_response(response) ⇒ Object
- #log_start ⇒ Object
- #put_timing(response) ⇒ Object
- #write_log ⇒ Object
Constructor Details
#initialize(logger, filter_list, filter = true) ⇒ RequestLogger
Returns a new instance of RequestLogger.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/mint_http/request_logger.rb', line 23 def initialize(logger, filter_list, filter = true) @logger = logger @filter_list = filter_list.map(&:downcase) @filter = filter @time_started = 0.0 @time_ended = 0.0 @time_connected = 0.0 @time_total = 0.0 @time_connecting = 0.0 @request = nil @net_request = nil @response = nil @error = nil end |
Instance Attribute Details
#filter_list ⇒ Object (readonly)
Returns the value of attribute filter_list.
11 12 13 |
# File 'lib/mint_http/request_logger.rb', line 11 def filter_list @filter_list end |
#local_address ⇒ Object (readonly)
Returns the value of attribute local_address.
18 19 20 |
# File 'lib/mint_http/request_logger.rb', line 18 def local_address @local_address end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
10 11 12 |
# File 'lib/mint_http/request_logger.rb', line 10 def logger @logger end |
#remote_address ⇒ Object (readonly)
Returns the value of attribute remote_address.
19 20 21 |
# File 'lib/mint_http/request_logger.rb', line 19 def remote_address @remote_address end |
#time_connected ⇒ Object (readonly)
Returns the value of attribute time_connected.
14 15 16 |
# File 'lib/mint_http/request_logger.rb', line 14 def time_connected @time_connected end |
#time_connecting ⇒ Object (readonly)
Returns the value of attribute time_connecting.
16 17 18 |
# File 'lib/mint_http/request_logger.rb', line 16 def time_connecting @time_connecting end |
#time_ended ⇒ Object (readonly)
Returns the value of attribute time_ended.
13 14 15 |
# File 'lib/mint_http/request_logger.rb', line 13 def time_ended @time_ended end |
#time_started ⇒ Object (readonly)
Returns the value of attribute time_started.
12 13 14 |
# File 'lib/mint_http/request_logger.rb', line 12 def time_started @time_started end |
#time_total ⇒ Object (readonly)
Returns the value of attribute time_total.
15 16 17 |
# File 'lib/mint_http/request_logger.rb', line 15 def time_total @time_total end |
#tls_config ⇒ Object (readonly)
Returns the value of attribute tls_config.
17 18 19 |
# File 'lib/mint_http/request_logger.rb', line 17 def tls_config @tls_config end |
Instance Method Details
#log_connected ⇒ Object
61 62 63 64 |
# File 'lib/mint_http/request_logger.rb', line 61 def log_connected @time_connected = clock_time @time_connecting = @time_connected - @time_started end |
#log_connection_info(http) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/mint_http/request_logger.rb', line 130 def log_connection_info(http) @tls_config = http.instance_eval do if use_ssl? { version: @socket.io.ssl_version, cipher: @socket.io.cipher[0] } else nil end end socket = http. if socket && socket.is_a?(TCPSocket) @local_address = socket.local_address @remote_address = socket.remote_address end end |
#log_end ⇒ Object
56 57 58 59 |
# File 'lib/mint_http/request_logger.rb', line 56 def log_end @time_ended = clock_time @time_total = @time_ended - @time_started end |
#log_error(error) ⇒ Object
48 49 50 |
# File 'lib/mint_http/request_logger.rb', line 48 def log_error(error) @error = error end |
#log_request(request, net_request) ⇒ Object
39 40 41 42 |
# File 'lib/mint_http/request_logger.rb', line 39 def log_request(request, net_request) @request = request @net_request = net_request end |
#log_response(response) ⇒ Object
44 45 46 |
# File 'lib/mint_http/request_logger.rb', line 44 def log_response(response) @response = response end |
#log_start ⇒ Object
52 53 54 |
# File 'lib/mint_http/request_logger.rb', line 52 def log_start @time_started = clock_time end |
#put_timing(response) ⇒ Object
67 68 69 70 71 72 73 |
# File 'lib/mint_http/request_logger.rb', line 67 def put_timing(response) response.time_started = @time_started response.time_ended = @time_ended response.time_connected = @time_connected response.time_total = @time_total response.time_connecting = @time_connecting end |
#write_log ⇒ Object
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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/mint_http/request_logger.rb', line 75 def write_log path = build_path(@request.request_url) version = @response&.version || '1.1' tls = 'None' if @tls_config tls = "#{@tls_config[:version]} Cipher: #{@tls_config[:cipher]}" end local_address = if @local_address "#{@local_address.ip_address}:#{@local_address.ip_port}" else 'N/A' end remote_address = if @remote_address "#{@remote_address.ip_address}:#{@remote_address.ip_port}" else 'N/A' end buffer = String.new(encoding: 'UTF-8') buffer << <<~TXT MintHttp Log (#{@request.request_url}) @@ Timeouts: #{@request.open_timeout}, #{@request.write_timeout}, #{@request.read_timeout} @@ Time: #{@time_started.round(3)} -> #{@time_connected.round(3)} connecting: #{time_connecting.round(3)} total: #{@time_total.round(3)} seconds @@ TLS: #{tls} @@ Address: #{local_address} -> #{remote_address} -> #{@request.method.upcase} #{path} HTTP/#{version} #{masked_headers(@net_request.each_header.to_h, '-> ')} -> #{masked_body(@net_request.body, @request.headers['content-type'])} ======= TXT if @response buffer << <<~TXT <- Response: HTTP/#{@response.version} #{@response.status_code} #{@response.status_text} #{masked_headers(@response.headers, '<- ')} <- Length: #{@response.body&.bytesize || 0} Body: #{masked_body(@response.body, @response.headers['content-type'])} TXT end if @error buffer << "!! Error: #{@error.class}, message: #{@error.}" end unless buffer.valid_encoding? raise 'Buffer has not valid encoding' end @logger.info(buffer.strip) rescue Encoding::CompatibilityError => e @logger.warn(e) end |