icyleaf / halite

💎HTTP Requests Client with a chainable REST API, built-in sessions and middlewares.
MIT License
170 stars 13 forks source link

Refactor logger system #19

Closed icyleaf closed 6 years ago

icyleaf commented 6 years ago

At first, i just only want add json-formatted logger, for current logger system was not carry it, so make a little refactor same as MIME type system and add JSON-formatted logging.

Before

class CustomLogger < Halite::Logger
  def request(request)
    @logger.info ">> | %s | %s %s" % [request.verb, request.uri, request.body]
  end

  def response(response)
    @logger.info "<< | %s | %s %s" % [response.status_code, response.uri, response.mime_type]
  end
end

Halite.logger(CustomLogger.new)
      .get("http://httpbin.org/get", params: {name: "foobar"})

After

class CustomLogger < Halite::Logger::Adapter
  def request(request)
    @logger.info "| >> | %s | %s %s" % [request.verb, request.uri, request.body]
  end

  def response(response)
    @logger.info "| << | %s | %s %s" % [response.status_code, response.uri, response.content_type]
  end
end

Halite::Logger.register_adapter "custom", CustomLogger.new

Halite.logger(logger: CustomLogger.new)
      .get("http://httpbin.org/get", params: {name: "foobar"})
# Also register name support
Halite.logger(adapter: "custom")
      .get("http://httpbin.org/get", params: {name: "foobar"})