From 8f5bc755ae0c1c8863e6ea0a03ba11d199f33f6a Mon Sep 17 00:00:00 2001 From: sdogruyol Date: Fri, 12 Feb 2016 16:09:15 +0200 Subject: [PATCH] Add logger macro to easily setup custom log_handler --- spec/config_spec.cr | 6 ------ spec/macros_spec.cr | 7 +++++++ src/kemal/base_log_handler.cr | 1 + src/kemal/config.cr | 1 - src/kemal/macros.cr | 9 ++++++++- 5 files changed, 16 insertions(+), 8 deletions(-) diff --git a/spec/config_spec.cr b/spec/config_spec.cr index 6b3d776..8fd88d2 100644 --- a/spec/config_spec.cr +++ b/spec/config_spec.cr @@ -29,10 +29,4 @@ describe "Config" do config.handlers.size.should eq(1) end - it "sets a custom logger" do - config = Kemal::Config::INSTANCE - config.logger = CustomLogHandler.new("production") - config.handlers.first.should be_a(CustomLogHandler) - config.logger.should be_a(CustomLogHandler) - end end diff --git a/spec/macros_spec.cr b/spec/macros_spec.cr index 9d3b3a7..00d4ab2 100644 --- a/spec/macros_spec.cr +++ b/spec/macros_spec.cr @@ -27,5 +27,12 @@ describe "Macros" do logging false Kemal.config.logging.should eq false end + + it "sets a custom logger" do + config = Kemal::Config::INSTANCE + logger CustomLogHandler.new("production") + config.handlers.first.should be_a(CustomLogHandler) + config.logger.should be_a(CustomLogHandler) + end end end diff --git a/src/kemal/base_log_handler.cr b/src/kemal/base_log_handler.cr index fce9d62..71b72f9 100644 --- a/src/kemal/base_log_handler.cr +++ b/src/kemal/base_log_handler.cr @@ -6,6 +6,7 @@ class Kemal::BaseLogHandler < HTTP::Handler end def call(context) + call_next context end def write(message) diff --git a/src/kemal/config.cr b/src/kemal/config.cr index f04a96e..57fbf4b 100644 --- a/src/kemal/config.cr +++ b/src/kemal/config.cr @@ -19,7 +19,6 @@ module Kemal def logger=(logger : Kemal::BaseLogHandler) @logger = logger - HANDLERS << @logger.not_nil! end def scheme diff --git a/src/kemal/macros.cr b/src/kemal/macros.cr index 025f74e..0abcf6c 100644 --- a/src/kemal/macros.cr +++ b/src/kemal/macros.cr @@ -34,10 +34,17 @@ end # development: STDOUT in # production: kemal.log macro log(message) - Kemal::LogHandler::INSTANCE.write "#{{{message}}}\n" if Kemal.config.logging + Kemal.config.logger.write "#{{{message}}}\n" end # Enables / Disables logging macro logging(status) Kemal.config.logging = {{status}} +end + +macro logger(logger) + Kemal.config.logger = {{logger}} + Kemal.config.add_handler {{logger}} end + +