Complete decoupling logger

This commit is contained in:
sdogruyol 2016-02-12 14:11:21 +02:00
parent 52538afb10
commit 3c75d727ba
9 changed files with 53 additions and 28 deletions

View file

@ -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

View file

@ -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)

View file

@ -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

View file

@ -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
}

View file

@ -0,0 +1,13 @@
require "http"
class Kemal::BaseLogHandler < HTTP::Handler
def initialize(@env)
end
def call(context)
end
def write(message)
end
end

View file

@ -1,7 +1,7 @@
require "colorize"
require "http"
class Kemal::LogHandler < HTTP::Handler
class Kemal::CommonLogHandler < Kemal::BaseLogHandler
# INSTANCE = new
getter handler

View file

@ -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

View file

@ -0,0 +1,2 @@
class Kemal::NullLogHandler < Kemal::BaseLogHandler
end

View file

@ -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