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.
[x] refactor logger system same as MIME type system.
[x] register logger adapter
[x] rename @logger to @writer for disambiguation
[x] expose @writer to be a setter
[x] JSON-formatted logging support
[x] remove Halite::Logger.clear to keep built-in adapters
[x] use adapter both with name and instance class support!
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"})
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.
@logger
to@writer
for disambiguation@writer
to be a setterHalite::Logger.clear
to keep built-in adaptersBefore
After