From 3c75d727ba8c04bced369ef213c2f35ea8f32508 Mon Sep 17 00:00:00 2001 From: sdogruyol Date: Fri, 12 Feb 2016 14:11:21 +0200 Subject: [PATCH] Complete decoupling logger --- spec/config_spec.cr | 5 +-- spec/logger_spec.cr | 8 ++--- spec/spec_helper.cr | 8 +++-- src/kemal.cr | 10 ++---- src/kemal/base_log_handler.cr | 13 ++++++++ .../{log_handler.cr => common_log_handler.cr} | 2 +- src/kemal/config.cr | 32 ++++++++++++------- src/kemal/null_log_handler.cr | 2 ++ src/kemal/route_handler.cr | 1 - 9 files changed, 53 insertions(+), 28 deletions(-) create mode 100644 src/kemal/base_log_handler.cr rename src/kemal/{log_handler.cr => common_log_handler.cr} (97%) create mode 100644 src/kemal/null_log_handler.cr diff --git a/spec/config_spec.cr b/spec/config_spec.cr index 9283459..6b3d776 100644 --- a/spec/config_spec.cr +++ b/spec/config_spec.cr @@ -31,7 +31,8 @@ describe "Config" do it "sets a custom logger" do config = Kemal::Config::INSTANCE - config.logger = CustomTestHandler.new - config.handlers.first.should be_a(CustomTestHandler) + config.logger = CustomLogHandler.new("production") + config.handlers.first.should be_a(CustomLogHandler) + config.logger.should be_a(CustomLogHandler) end end diff --git a/spec/logger_spec.cr b/spec/logger_spec.cr index 6ae6267..18e169d 100644 --- a/spec/logger_spec.cr +++ b/spec/logger_spec.cr @@ -2,27 +2,27 @@ require "./spec_helper" describe "Kemal::LogHandler" do it "creates a handler" do - logger = Kemal::LogHandler.new "production" + logger = Kemal::CommonLogHandler.new "production" logger.handler.should_not be nil end it "creates a STDOUT handler by default" do config = Kemal.config - logger = Kemal::LogHandler.new "production" + logger = Kemal::CommonLogHandler.new "production" logger.handler.should be_a IO end it "creates a file handler in production" do config = Kemal.config config.env = "production" - logger = Kemal::LogHandler.new "production" + logger = Kemal::CommonLogHandler.new "production" logger.handler.should be_a File end it "writes to a file in production" do config = Kemal.config config.env = "production" - logger = Kemal::LogHandler.new "production" + logger = Kemal::CommonLogHandler.new "production" request = HTTP::Request.new("GET", "/?message=world&time=now") io = MemoryIO.new response = HTTP::Server::Response.new(io) diff --git a/spec/spec_helper.cr b/spec/spec_helper.cr index 7ef084b..08a6d3d 100644 --- a/spec/spec_helper.cr +++ b/spec/spec_helper.cr @@ -10,6 +10,8 @@ class CustomTestHandler < HTTP::Handler end end +class CustomLogHandler < Kemal::BaseLogHandler; end + def create_request_and_return_io(handler, request) io = MemoryIO.new response = HTTP::Server::Response.new(io) @@ -34,6 +36,8 @@ def create_ws_request_and_return_io(handler, request) end Spec.before_each do - Kemal.config.env = "development" - Kemal.config.handlers.clear + config = Kemal.config + config.env = "development" + config.setup_logging + config.handlers.clear end diff --git a/src/kemal.cr b/src/kemal.cr index 95372b7..d4e8fa9 100644 --- a/src/kemal.cr +++ b/src/kemal.cr @@ -4,9 +4,8 @@ require "./kemal/middleware/*" at_exit do Kemal::CLI.new config = Kemal.config - if config.logging - config.logger.write "[#{config.env}] Kemal is ready to lead at #{config.scheme}://#{config.host_binding}:#{config.port}\n" - end + config.setup_logging + config.logger.write "[#{config.env}] Kemal is ready to lead at #{config.scheme}://#{config.host_binding}:#{config.port}\n" config.add_handler Kemal::StaticFileHandler.new(config.public_folder) config.add_handler Kemal::RouteHandler::INSTANCE @@ -14,10 +13,7 @@ at_exit do server.ssl = config.ssl Signal::INT.trap { - if config.logging - config.logger.write "Kemal is going to take a rest!\n" - config.logger.handler.close - end + config.logger.write "Kemal is going to take a rest!\n" server.close exit } diff --git a/src/kemal/base_log_handler.cr b/src/kemal/base_log_handler.cr new file mode 100644 index 0000000..fce9d62 --- /dev/null +++ b/src/kemal/base_log_handler.cr @@ -0,0 +1,13 @@ +require "http" + +class Kemal::BaseLogHandler < HTTP::Handler + + def initialize(@env) + end + + def call(context) + end + + def write(message) + end +end diff --git a/src/kemal/log_handler.cr b/src/kemal/common_log_handler.cr similarity index 97% rename from src/kemal/log_handler.cr rename to src/kemal/common_log_handler.cr index 02ae6f7..dbb858f 100644 --- a/src/kemal/log_handler.cr +++ b/src/kemal/common_log_handler.cr @@ -1,7 +1,7 @@ require "colorize" require "http" -class Kemal::LogHandler < HTTP::Handler +class Kemal::CommonLogHandler < Kemal::BaseLogHandler # INSTANCE = new getter handler diff --git a/src/kemal/config.cr b/src/kemal/config.cr index f888f61..f04a96e 100644 --- a/src/kemal/config.cr +++ b/src/kemal/config.cr @@ -2,7 +2,7 @@ module Kemal class Config INSTANCE = Config.new HANDLERS = [] of HTTP::Handler - property host_binding, ssl, port, env, public_folder, logging, logger + property host_binding, ssl, port, env, public_folder, logging def initialize @host_binding = "0.0.0.0" unless @host_binding @@ -10,7 +10,16 @@ module Kemal @env = "development" unless @env @public_folder = "./public" @logging = true - @logger = Kemal::LogHandler.new(@env) if @logging + @logger = nil + end + + def logger + @logger.not_nil! + end + + def logger=(logger : Kemal::BaseLogHandler) + @logger = logger + HANDLERS << @logger.not_nil! end def scheme @@ -21,15 +30,6 @@ module Kemal HANDLERS end - def logger - @logger.not_nil! - end - - def logger=(logger) - HANDLERS << logger - @logger = logger - end - def add_handler(handler : HTTP::Handler) HANDLERS << handler end @@ -37,6 +37,16 @@ module Kemal def add_ws_handler(handler : HTTP::WebSocketHandler) HANDLERS << handler end + + def setup_logging + if @logging + @logger = Kemal::CommonLogHandler.new(@env) + HANDLERS << @logger.not_nil! + elsif @logging == false + @logger = Kemal::NullLogHandler.new(@env) + HANDLERS << @logger.not_nil! + end + end end def self.config diff --git a/src/kemal/null_log_handler.cr b/src/kemal/null_log_handler.cr new file mode 100644 index 0000000..5eec3ef --- /dev/null +++ b/src/kemal/null_log_handler.cr @@ -0,0 +1,2 @@ +class Kemal::NullLogHandler < Kemal::BaseLogHandler +end diff --git a/src/kemal/route_handler.cr b/src/kemal/route_handler.cr index ab66589..166879a 100644 --- a/src/kemal/route_handler.cr +++ b/src/kemal/route_handler.cr @@ -35,7 +35,6 @@ class Kemal::RouteHandler < HTTP::Handler context.response.print body return context rescue ex - # Kemal.config.logger.write "Exception: #{ex.to_s}\n" return render_500(context, ex.to_s) end end