Add logger macro to easily setup custom log_handler

This commit is contained in:
sdogruyol 2016-02-12 16:09:15 +02:00
parent 3c75d727ba
commit 8f5bc755ae
5 changed files with 16 additions and 8 deletions

View file

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

View file

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

View file

@ -6,6 +6,7 @@ class Kemal::BaseLogHandler < HTTP::Handler
end
def call(context)
call_next context
end
def write(message)

View file

@ -19,7 +19,6 @@ module Kemal
def logger=(logger : Kemal::BaseLogHandler)
@logger = logger
HANDLERS << @logger.not_nil!
end
def scheme

View file

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