Skip to content

Instantly share code, notes, and snippets.

@motebaya
Created August 10, 2024 13:06
Show Gist options
  • Save motebaya/d3436ec35874a342e01da10170c85bca to your computer and use it in GitHub Desktop.
Save motebaya/d3436ec35874a342e01da10170c85bca to your computer and use it in GitHub Desktop.
remove all unnecessary characters that are displayed in the console during debug mode for net/http or HTTParty. Related: https://github.com/jnunemaker/httparty/issues/806
#!/usr/bin/ruby
require 'logger'
class Logging
def initialize(progname:, level: Logger::INFO)
@logger = Logger.new($stdout, progname: progname)
@logger.level = level
end
# crlf -> \r\n" at endline
# hexadecimal inside double quote -> "000070a"
# read/reading bytes section -> reading/read <int> bytes...
# unicode escape bytes -> "\x1F\x8B.....\b\x00\""
def <<(msg)
msg = msg.strip.gsub('\\r\\n"', '"')
return if msg.empty? || msg.match(/reading\s\d+\sbytes|<-|read\s\d+\sbytes/) || msg.match(/"[^"]{1,5}"/) || msg.match(/->\s""/) || msg.match(/->.*\\x[0-9A-Fa-f]{2}.*/) || msg.match(/->\s"[\d+]*"$/) || msg.match?(/->\s"\h+"$/)
@logger.debug(msg.strip)
end
end
@motebaya
Copy link
Author

call:

require 'httparty'
require_relative 'lib/Logging'

logger = Logging.new(progname: 'net', level: Logger::DEBUG)
HTTParty::Basement.debug_output(logger)
response = HTTParty.get('https://jsonplaceholder.typicode.com/posts/1')
puts response.code

res:

D, [2024-08-10T20:09:42.256245 #2896] DEBUG -- net: opening connection to jsonplaceholder.typicode.com:443...
D, [2024-08-10T20:09:42.330504 #2896] DEBUG -- net: opened
D, [2024-08-10T20:09:42.330937 #2896] DEBUG -- net: starting SSL for jsonplaceholder.typicode.com:443...
D, [2024-08-10T20:09:42.705760 #2896] DEBUG -- net: SSL established, protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384
D, [2024-08-10T20:09:42.706213 #2896] DEBUG -- net: "GET /posts/1 HTTP/1.1\r\nAccept-Encoding: gzip;q=1.0,deflate;q=0.6,identity;q=0.3\r\nAccept: */*\r\nUser-Agent: Ruby\r\nConnection: close\r\nHost: jsonplaceholder.typicode.com\r\n"
D, [2024-08-10T20:09:43.131877 #2896] DEBUG -- net: -> "HTTP/1.1 200 OK"
D, [2024-08-10T20:09:43.132117 #2896] DEBUG -- net: -> "Date: Sat, 10 Aug 2024 13:09:43 GMT"
D, [2024-08-10T20:09:43.132285 #2896] DEBUG -- net: -> "Content-Type: application/json; charset=utf-8"
D, [2024-08-10T20:09:43.132475 #2896] DEBUG -- net: -> "Transfer-Encoding: chunked"
D, [2024-08-10T20:09:43.132749 #2896] DEBUG -- net: -> "Connection: close"
D, [2024-08-10T20:09:43.133045 #2896] DEBUG -- net: -> "Reporting-Endpoints: heroku-nel=https://nel.heroku.com/reports?ts=1710788742&sid=e11707d5-02a7-43ef-b45e-2cf4d2036f7d&s=cKlVvZMWhQBb3bTSEeg%2FvlafQNvF7QcyHbxMwpeQWC0%3D"
D, [2024-08-10T20:09:43.133367 #2896] DEBUG -- net: -> "X-Powered-By: Express"
D, [2024-08-10T20:09:43.133636 #2896] DEBUG -- net: -> "X-Ratelimit-Limit: 1000"
D, [2024-08-10T20:09:43.133903 #2896] DEBUG -- net: -> "X-Ratelimit-Remaining: 999"
D, [2024-08-10T20:09:43.134069 #2896] DEBUG -- net: -> "X-Ratelimit-Reset: 1710788747"
D, [2024-08-10T20:09:43.134234 #2896] DEBUG -- net: -> "Vary: Origin, Accept-Encoding"
D, [2024-08-10T20:09:43.134396 #2896] DEBUG -- net: -> "Access-Control-Allow-Credentials: true"
D, [2024-08-10T20:09:43.134558 #2896] DEBUG -- net: -> "Cache-Control: max-age=43200"
D, [2024-08-10T20:09:43.134695 #2896] DEBUG -- net: -> "Pragma: no-cache"
D, [2024-08-10T20:09:43.134855 #2896] DEBUG -- net: -> "Expires: -1"
D, [2024-08-10T20:09:43.134977 #2896] DEBUG -- net: -> "X-Content-Type-Options: nosniff"
D, [2024-08-10T20:09:43.135090 #2896] DEBUG -- net: -> "Etag: W/\"124-yiKdLzqO5gfBrJFrcdJ8Yq0LGnU\""
D, [2024-08-10T20:09:43.135203 #2896] DEBUG -- net: -> "Via: 1.1 vegur"
D, [2024-08-10T20:09:43.135311 #2896] DEBUG -- net: -> "CF-Cache-Status: HIT"
D, [2024-08-10T20:09:43.135421 #2896] DEBUG -- net: -> "Age: 5044"
D, [2024-08-10T20:09:43.135563 #2896] DEBUG -- net: -> "Server: cloudflare"
D, [2024-08-10T20:09:43.135667 #2896] DEBUG -- net: -> "CF-RAY: 8b1038d06c957e86-LAX"
D, [2024-08-10T20:09:43.135771 #2896] DEBUG -- net: -> "Content-Encoding: gzip"
D, [2024-08-10T20:09:43.136349 #2896] DEBUG -- net: Conn close
200

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment