kemal/spec/request_log_handler_spec.cr
Hugo Parente Lima 19d3913b5d
Embrace Crystal standard Log for logging. (#705)
* Embrace Crystal standard Log for logging.

Kemal uses a LogHandler to log requests, this code predates the
Crystal Log class so while the Kemal documentation says that logging is
done using Log class, the http request log is done in a different way.

This patch deprecates:
- Kemal::Config#logger
- Kemal::Config#logger=(Kemal::BaseLogHandler)
- log(String)
- Kemal::LogHandler.initialize(IO)
- NullLogHandler

and changes:

- Add Kemal::Log (Log = ::Log.for(self))
- Kemal::LogHandler now uses Log.
- No handler is created if logging is set to false.

Old code using custom log handlers must work as before.

* Let ExceptionHandler use Log instead of log.

* Deprecate Kemal::LogHandler and adds Kemal::RequestLogHandler.

* Don't break API on Kemal#logger.

* Add test for Kemal::RequestLogHandler.

* Do not log redundant informations like timestamp and exception message.

* Use ex.message on unexpected exceptions log message.
2025-04-01 13:25:09 +03:00

17 lines
469 B
Crystal

require "log/spec"
require "./spec_helper"
describe Kemal::RequestLogHandler do
it "creates log message for each request" do
Log.setup(:none)
request = HTTP::Request.new("GET", "/")
response = HTTP::Server::Response.new(IO::Memory.new)
context = HTTP::Server::Context.new(request, response)
logger = Kemal::RequestLogHandler.new
Log.capture do |logs|
logger.call(context)
logs.check(:info, /404 GET \/ \d+.*s/)
end
end
end