Make logger configurable

This commit is contained in:
Sdogruyol 2016-01-17 13:08:12 +02:00
parent 68d23c2678
commit 2af3648682
4 changed files with 23 additions and 6 deletions

View file

@ -21,4 +21,11 @@ describe "Macros" do
Kemal.config.handlers.size.should eq 1
end
end
describe "#logging" do
it "sets logging status" do
logging false
Kemal.config.logging.should eq false
end
end
end

View file

@ -4,18 +4,22 @@ require "./kemal/middleware/*"
at_exit do
Kemal::CLI.new
config = Kemal.config
logger = Kemal::Logger.new
config.add_handler logger
if config.logging
logger = Kemal::Logger.new
config.add_handler logger
logger.write "[#{config.env}] Kemal is ready to lead at #{config.scheme}://#{config.host_binding}:#{config.port}\n"
Signal::INT.trap {
logger.write "Kemal is going to take a rest!\n"
logger.handler.close
}
end
config.add_handler Kemal::StaticFileHandler.new(config.public_folder)
config.add_handler Kemal::Handler::INSTANCE
server = HTTP::Server.new(config.host_binding.not_nil!.to_slice, config.port, config.handlers)
server.ssl = config.ssl
logger.write "[#{config.env}] Kemal is ready to lead at #{config.scheme}://#{config.host_binding}:#{config.port}\n"
Signal::INT.trap {
logger.write "Kemal is going to take a rest!\n"
logger.handler.close
server.close
exit
}

View file

@ -4,13 +4,14 @@ module Kemal
class Config
INSTANCE = Config.new
HANDLERS = [] of HTTP::Handler
property host_binding, ssl, port, env, public_folder
property host_binding, ssl, port, env, public_folder, logging
def initialize
@host_binding = "0.0.0.0" unless @host_binding
@port = 3000
@env = "development" unless @env
@public_folder = "./public"
@logging = true
end
def scheme

View file

@ -41,3 +41,8 @@ end
macro log(message)
Kemal::Logger::INSTANCE.write "#{{{message}}}\n"
end
# Enables / Disables logging
macro logging(status)
Kemal.config.logging = {{status}}
end