Updated logger to be more robust
This commit is contained in:
parent
639d4a7a48
commit
28d4d5167b
6 changed files with 39 additions and 16 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1 +1,2 @@
|
|||
.crystal
|
||||
*.log
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -8,6 +8,7 @@ module Kemal
|
|||
|
||||
def initialize
|
||||
@port = 3000
|
||||
@env = "development" unless @env
|
||||
end
|
||||
|
||||
def scheme
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue