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
*.log

View file

@ -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

View file

@ -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

View file

@ -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

View file

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

View file

@ -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
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