Be sure to close handlers
This commit is contained in:
parent
86dfd1a24d
commit
0e23e8d9a3
2 changed files with 22 additions and 14 deletions
11
src/kemal.cr
11
src/kemal.cr
|
@ -12,11 +12,20 @@ at_exit do
|
|||
end
|
||||
|
||||
config = Kemal.config
|
||||
config.add_handler Kemal::Logger.new
|
||||
logger = Kemal::Logger.new
|
||||
config.add_handler logger
|
||||
config.add_handler Kemal::Handler::INSTANCE
|
||||
config.add_handler HTTP::StaticFileHandler.new("./public")
|
||||
|
||||
server = HTTP::Server.new(config.port, config.handlers)
|
||||
server.ssl = config.ssl
|
||||
logger.write "Kemal is ready to lead at #{config.scheme}://0.0.0.0:#{config.port}\n"
|
||||
|
||||
Signal::INT.trap {
|
||||
logger.handler.close
|
||||
server.close
|
||||
exit
|
||||
}
|
||||
|
||||
server.listen
|
||||
end
|
||||
|
|
|
@ -3,10 +3,11 @@ class Kemal::Logger < HTTP::Handler
|
|||
|
||||
def initialize
|
||||
@env = Kemal.config.env
|
||||
@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
|
||||
@handler = if @env == "production"
|
||||
File.new("kemal.log", "a+")
|
||||
else
|
||||
STDOUT
|
||||
end
|
||||
end
|
||||
|
||||
def call(request)
|
||||
|
@ -15,8 +16,7 @@ class Kemal::Logger < HTTP::Handler
|
|||
elapsed = Time.now - time
|
||||
elapsed_text = elapsed_text(elapsed)
|
||||
output_message = "#{request.method} #{request.resource} - #{response.status_code} (#{elapsed_text})\n"
|
||||
@handler.print output_message if @env == "development"
|
||||
@handler.write output_message.to_slice if @env == "production"
|
||||
write output_message
|
||||
response
|
||||
end
|
||||
|
||||
|
@ -33,12 +33,11 @@ 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
|
||||
def write(message)
|
||||
if @env == "production"
|
||||
@handler.write message.to_slice
|
||||
else
|
||||
@handler.print message
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue