Updated logger to be more robust

This commit is contained in:
Sdogruyol 2015-11-18 22:45:49 +02:00
parent 639d4a7a48
commit 28d4d5167b
6 changed files with 39 additions and 16 deletions

1
.gitignore vendored
View file

@ -1 +1,2 @@
.crystal .crystal
*.log

View file

@ -12,6 +12,17 @@ describe "Config" do
config.port.should eq 3000 config.port.should eq 3000
end 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 it "adds a custom handler" do
config = Kemal.config config = Kemal.config
config.add_handler CustomTestHandler.new config.add_handler CustomTestHandler.new

View file

@ -1,11 +1,16 @@
require "./spec_helper" require "./spec_helper"
describe "Logger" do describe "Logger" do
it "logs stuff" do it "creates a STDOUT handler by default" do
# IO.pipe do |r, w| config = Kemal.config
# logger = Kemal::Logger.new(w) logger = Kemal::Logger.new
# logger.info "Info from logger" logger.handler.should be_a IO
# r.gets.should match(/Info from logger/) end
# 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
end end

View file

@ -7,7 +7,7 @@ at_exit do
Kemal.config.port = opt_port.to_i Kemal.config.port = opt_port.to_i
end end
opts.on("-e ", "--environment ", "environment") do |env| opts.on("-e ", "--environment ", "environment") do |env|
env ? Kemal.config.env = env : "development" Kemal.config.env = env
end end
end end
@ -17,9 +17,6 @@ at_exit do
config.add_handler HTTP::StaticFileHandler.new("./public") config.add_handler HTTP::StaticFileHandler.new("./public")
server = HTTP::Server.new(config.port, config.handlers) server = HTTP::Server.new(config.port, config.handlers)
server.ssl = config.ssl server.ssl = config.ssl
puts "Kemal is ready to lead at #{config.scheme}://0.0.0.0:#{config.port}"
server.listen server.listen
end end

View file

@ -8,6 +8,7 @@ module Kemal
def initialize def initialize
@port = 3000 @port = 3000
@env = "development" unless @env
end end
def scheme def scheme

View file

@ -1,13 +1,12 @@
class Kemal::Logger < HTTP::Handler class Kemal::Logger < HTTP::Handler
property handler getter handler
def initialize def initialize
@env = Kemal.config.env @env = Kemal.config.env
if @env == "production" @handler = STDOUT
@handler = File.new("kemal.log", "a+") config = Kemal.config
else @startup_message = "Kemal is ready to lead at #{config.scheme}://0.0.0.0:#{config.port}"
@handler = STDOUT @env == "production" ? setup_file_handler : setup_stdout_handler
end
end end
def call(request) def call(request)
@ -33,4 +32,13 @@ class Kemal::Logger < HTTP::Handler
"#{(millis * 1000).round(2)}µs" "#{(millis * 1000).round(2)}µs"
end 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 end