From 28d4d5167b00e70959dec58bc3b9d24f6b3fe653 Mon Sep 17 00:00:00 2001 From: Sdogruyol Date: Wed, 18 Nov 2015 22:45:49 +0200 Subject: [PATCH] Updated logger to be more robust --- .gitignore | 1 + spec/config_spec.cr | 11 +++++++++++ spec/logger_spec.cr | 17 +++++++++++------ src/kemal.cr | 5 +---- src/kemal/config.cr | 1 + src/kemal/logger.cr | 20 ++++++++++++++------ 6 files changed, 39 insertions(+), 16 deletions(-) diff --git a/.gitignore b/.gitignore index d1f2bed..f31288b 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .crystal +*.log diff --git a/spec/config_spec.cr b/spec/config_spec.cr index 5f72f10..d4332f9 100644 --- a/spec/config_spec.cr +++ b/spec/config_spec.cr @@ -12,6 +12,17 @@ describe "Config" do config.port.should eq 3000 end + it "sets default environment to development" do + config = Kemal.config + config.env.should eq "development" + end + + it "set environment to production" do + config = Kemal.config + config.env = "production" + config.env.should eq "production" + end + it "adds a custom handler" do config = Kemal.config config.add_handler CustomTestHandler.new diff --git a/spec/logger_spec.cr b/spec/logger_spec.cr index af206ec..ae265da 100644 --- a/spec/logger_spec.cr +++ b/spec/logger_spec.cr @@ -1,11 +1,16 @@ require "./spec_helper" describe "Logger" do - it "logs stuff" do - # IO.pipe do |r, w| - # logger = Kemal::Logger.new(w) - # logger.info "Info from logger" - # r.gets.should match(/Info from logger/) - # end + it "creates a STDOUT handler by default" do + config = Kemal.config + logger = Kemal::Logger.new + logger.handler.should be_a IO + end + + it "creates a file handler in production" do + config = Kemal.config + config.env = "production" + logger = Kemal::Logger.new + logger.handler.should be_a File end end diff --git a/src/kemal.cr b/src/kemal.cr index 44d8066..c71a7f7 100644 --- a/src/kemal.cr +++ b/src/kemal.cr @@ -7,7 +7,7 @@ at_exit do Kemal.config.port = opt_port.to_i end opts.on("-e ", "--environment ", "environment") do |env| - env ? Kemal.config.env = env : "development" + Kemal.config.env = env end end @@ -17,9 +17,6 @@ at_exit do config.add_handler HTTP::StaticFileHandler.new("./public") server = HTTP::Server.new(config.port, config.handlers) - server.ssl = config.ssl - - puts "Kemal is ready to lead at #{config.scheme}://0.0.0.0:#{config.port}" server.listen end diff --git a/src/kemal/config.cr b/src/kemal/config.cr index 14a6130..8307525 100644 --- a/src/kemal/config.cr +++ b/src/kemal/config.cr @@ -8,6 +8,7 @@ module Kemal def initialize @port = 3000 + @env = "development" unless @env end def scheme diff --git a/src/kemal/logger.cr b/src/kemal/logger.cr index 0c23671..ff1abcd 100644 --- a/src/kemal/logger.cr +++ b/src/kemal/logger.cr @@ -1,13 +1,12 @@ class Kemal::Logger < HTTP::Handler - property handler + getter handler def initialize @env = Kemal.config.env - if @env == "production" - @handler = File.new("kemal.log", "a+") - else - @handler = STDOUT - end + @handler = STDOUT + config = Kemal.config + @startup_message = "Kemal is ready to lead at #{config.scheme}://0.0.0.0:#{config.port}" + @env == "production" ? setup_file_handler : setup_stdout_handler end def call(request) @@ -33,4 +32,13 @@ class Kemal::Logger < HTTP::Handler "#{(millis * 1000).round(2)}µs" end + + private def setup_file_handler + @handler = File.new("kemal.log", "a+") + @handler.write @startup_message.to_slice + end + + private def setup_stdout_handler + @handler.print @startup_message + end end