From f66e9cd834c7caced50b7e16759458b22e1ffbe9 Mon Sep 17 00:00:00 2001 From: Sdogruyol Date: Fri, 20 Nov 2015 23:07:10 +0200 Subject: [PATCH] More logger specs --- spec/logger_spec.cr | 16 ++++++++++++++++ src/kemal/logger.cr | 10 +++++----- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/spec/logger_spec.cr b/spec/logger_spec.cr index ae265da..824498c 100644 --- a/spec/logger_spec.cr +++ b/spec/logger_spec.cr @@ -1,6 +1,11 @@ require "./spec_helper" describe "Logger" do + it "creates a handler" do + logger = Kemal::Logger.new + logger.handler.should_not be nil + end + it "creates a STDOUT handler by default" do config = Kemal.config logger = Kemal::Logger.new @@ -13,4 +18,15 @@ describe "Logger" do logger = Kemal::Logger.new logger.handler.should be_a File end + + it "writes to a file in production" do + config = Kemal.config + config.env = "production" + logger = Kemal::Logger.new + request = HTTP::Request.new("GET", "/?message=world&time=now") + logger.call request + str = File.read("kemal.log") + File.delete("kemal.log") + str.includes?("GET /?message=world&time=now").should eq true + end end diff --git a/src/kemal/logger.cr b/src/kemal/logger.cr index cad370c..36a290c 100644 --- a/src/kemal/logger.cr +++ b/src/kemal/logger.cr @@ -4,10 +4,10 @@ class Kemal::Logger < HTTP::Handler def initialize @env = Kemal.config.env @handler = if @env == "production" - File.new("kemal.log", "a+") - else - STDOUT - end + File.new("kemal.log", "a") + else + STDOUT + end end def call(request) @@ -35,7 +35,7 @@ class Kemal::Logger < HTTP::Handler def write(message) if @env == "production" - @handler.write message.to_slice + File.write "kemal.log", message else @handler.print message end