From 98b6ab08fab98a38a5c7c18e27617e0c9cfb6517 Mon Sep 17 00:00:00 2001 From: Aravinda Vishwanathapura Date: Sun, 29 Jan 2023 23:13:43 +0530 Subject: [PATCH] Fix multiple logger handlers when custom logger is used When `logger` function is used, it adds logger handler after the static file handler. This causes 404 errors for static files. Default logger is also added this makes two logger handlers per every request. Reproducer: ```crystal require "kemal" public_folder "./public" get "/hello" do |env| "Hello!" end class MyCustomLogger < Kemal::BaseLogHandler def call(context) puts "I'm logging some custom stuff here." call_next(context) # => This calls the next handler end # This is used from `log` method. def write(message) STDERR.puts message # => Logs the output to STDERR end end \# This works \# Kemal.config.logger = MyCustomLogger.new \# This fails while serving static files logger MyCustomLogger.new Kemal.run ``` With this PR, `logger` function only updates the `Kemal.config` and `Kemal.run` will take care of adding logger handler. This PR also fixes the warning when tried to run the example code for adding custom logger. ``` 10 | def call(env) ^-- Warning: positional parameter 'env' corresponds to parameter 'context' of the overridden method Kemal::BaseLogHandler#call(context : HTTP::Server::Context), which has a different name and may affect named argument passing ``` Signed-off-by: Aravinda Vishwanathapura --- src/kemal/helpers/helpers.cr | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/kemal/helpers/helpers.cr b/src/kemal/helpers/helpers.cr index 11a6b62..a829808 100644 --- a/src/kemal/helpers/helpers.cr +++ b/src/kemal/helpers/helpers.cr @@ -48,13 +48,13 @@ end # This is used to replace the built-in `Kemal::LogHandler` with a custom logger. # # A custom logger must inherit from `Kemal::BaseLogHandler` and must implement -# `call(env)`, `write(message)` methods. +# `call(context)`, `write(message)` methods. # # ``` # class MyCustomLogger < Kemal::BaseLogHandler -# def call(env) +# def call(context) # puts "I'm logging some custom stuff here." -# call_next(env) # => This calls the next handler +# call_next(context) # => This calls the next handler # end # # # This is used from `log` method. @@ -71,7 +71,6 @@ end # ``` def logger(logger : Kemal::BaseLogHandler) Kemal.config.logger = logger - Kemal.config.add_handler logger end # Enables / Disables static file serving.