From 850af8819e5790077718cdb0d6c5cbafbf6cac11 Mon Sep 17 00:00:00 2001 From: Sdogruyol Date: Thu, 11 Feb 2016 23:26:47 +0200 Subject: [PATCH] Start decoupling logger --- spec/config_spec.cr | 6 ++++++ spec/logger_spec.cr | 8 ++++---- src/kemal.cr | 2 -- src/kemal/config.cr | 4 ++-- src/kemal/log_handler.cr | 5 ++--- src/kemal/route_handler.cr | 2 +- 6 files changed, 15 insertions(+), 12 deletions(-) diff --git a/spec/config_spec.cr b/spec/config_spec.cr index 56441d4..9283459 100644 --- a/spec/config_spec.cr +++ b/spec/config_spec.cr @@ -28,4 +28,10 @@ describe "Config" do config.add_handler CustomTestHandler.new config.handlers.size.should eq(1) end + + it "sets a custom logger" do + config = Kemal::Config::INSTANCE + config.logger = CustomTestHandler.new + config.handlers.first.should be_a(CustomTestHandler) + end end diff --git a/spec/logger_spec.cr b/spec/logger_spec.cr index e5af632..6ae6267 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 + logger = Kemal::LogHandler.new "production" logger.handler.should_not be nil end it "creates a STDOUT handler by default" do config = Kemal.config - logger = Kemal::LogHandler.new + logger = Kemal::LogHandler.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 + logger = Kemal::LogHandler.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 + logger = Kemal::LogHandler.new "production" request = HTTP::Request.new("GET", "/?message=world&time=now") io = MemoryIO.new response = HTTP::Server::Response.new(io) diff --git a/src/kemal.cr b/src/kemal.cr index 8f94fc2..95372b7 100644 --- a/src/kemal.cr +++ b/src/kemal.cr @@ -5,8 +5,6 @@ at_exit do Kemal::CLI.new config = Kemal.config if config.logging - logger = Kemal::LogHandler.new - config.logger = logger config.logger.write "[#{config.env}] Kemal is ready to lead at #{config.scheme}://#{config.host_binding}:#{config.port}\n" end config.add_handler Kemal::StaticFileHandler.new(config.public_folder) diff --git a/src/kemal/config.cr b/src/kemal/config.cr index df3e6eb..ffe4284 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 + property host_binding, ssl, port, env, public_folder, logging, logger def initialize @host_binding = "0.0.0.0" unless @host_binding @@ -10,7 +10,7 @@ module Kemal @env = "development" unless @env @public_folder = "./public" @logging = true - @logger = nil + @logger = Kemal::LogHandler.new(@env) unless @logging end def scheme diff --git a/src/kemal/log_handler.cr b/src/kemal/log_handler.cr index 7e76265..02ae6f7 100644 --- a/src/kemal/log_handler.cr +++ b/src/kemal/log_handler.cr @@ -2,11 +2,10 @@ require "colorize" require "http" class Kemal::LogHandler < HTTP::Handler - INSTANCE = new + # INSTANCE = new getter handler - def initialize - @env = Kemal.config.env + def initialize(@env) @handler = if @env == "production" handler = File.new("kemal.log", "a") handler.flush_on_newline = true diff --git a/src/kemal/route_handler.cr b/src/kemal/route_handler.cr index 1646c11..ab66589 100644 --- a/src/kemal/route_handler.cr +++ b/src/kemal/route_handler.cr @@ -35,7 +35,7 @@ class Kemal::RouteHandler < HTTP::Handler context.response.print body return context rescue ex - Kemal::LogHandler::INSTANCE.write "Exception: #{ex.to_s}\n" + # Kemal.config.logger.write "Exception: #{ex.to_s}\n" return render_500(context, ex.to_s) end end