diff --git a/spec/macros_spec.cr b/spec/macros_spec.cr index cceed89..9d3b3a7 100644 --- a/spec/macros_spec.cr +++ b/spec/macros_spec.cr @@ -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 diff --git a/src/kemal.cr b/src/kemal.cr index 5431303..eaab7a7 100644 --- a/src/kemal.cr +++ b/src/kemal.cr @@ -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 } diff --git a/src/kemal/config.cr b/src/kemal/config.cr index 0e9afec..ea93802 100644 --- a/src/kemal/config.cr +++ b/src/kemal/config.cr @@ -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 diff --git a/src/kemal/macros.cr b/src/kemal/macros.cr index 3bea929..2a9b8b3 100644 --- a/src/kemal/macros.cr +++ b/src/kemal/macros.cr @@ -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