Complete decoupling logger
This commit is contained in:
parent
52538afb10
commit
3c75d727ba
9 changed files with 53 additions and 28 deletions
|
@ -31,7 +31,8 @@ describe "Config" do
|
||||||
|
|
||||||
it "sets a custom logger" do
|
it "sets a custom logger" do
|
||||||
config = Kemal::Config::INSTANCE
|
config = Kemal::Config::INSTANCE
|
||||||
config.logger = CustomTestHandler.new
|
config.logger = CustomLogHandler.new("production")
|
||||||
config.handlers.first.should be_a(CustomTestHandler)
|
config.handlers.first.should be_a(CustomLogHandler)
|
||||||
|
config.logger.should be_a(CustomLogHandler)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,27 +2,27 @@ require "./spec_helper"
|
||||||
|
|
||||||
describe "Kemal::LogHandler" do
|
describe "Kemal::LogHandler" do
|
||||||
it "creates a handler" do
|
it "creates a handler" do
|
||||||
logger = Kemal::LogHandler.new "production"
|
logger = Kemal::CommonLogHandler.new "production"
|
||||||
logger.handler.should_not be nil
|
logger.handler.should_not be nil
|
||||||
end
|
end
|
||||||
|
|
||||||
it "creates a STDOUT handler by default" do
|
it "creates a STDOUT handler by default" do
|
||||||
config = Kemal.config
|
config = Kemal.config
|
||||||
logger = Kemal::LogHandler.new "production"
|
logger = Kemal::CommonLogHandler.new "production"
|
||||||
logger.handler.should be_a IO
|
logger.handler.should be_a IO
|
||||||
end
|
end
|
||||||
|
|
||||||
it "creates a file handler in production" do
|
it "creates a file handler in production" do
|
||||||
config = Kemal.config
|
config = Kemal.config
|
||||||
config.env = "production"
|
config.env = "production"
|
||||||
logger = Kemal::LogHandler.new "production"
|
logger = Kemal::CommonLogHandler.new "production"
|
||||||
logger.handler.should be_a File
|
logger.handler.should be_a File
|
||||||
end
|
end
|
||||||
|
|
||||||
it "writes to a file in production" do
|
it "writes to a file in production" do
|
||||||
config = Kemal.config
|
config = Kemal.config
|
||||||
config.env = "production"
|
config.env = "production"
|
||||||
logger = Kemal::LogHandler.new "production"
|
logger = Kemal::CommonLogHandler.new "production"
|
||||||
request = HTTP::Request.new("GET", "/?message=world&time=now")
|
request = HTTP::Request.new("GET", "/?message=world&time=now")
|
||||||
io = MemoryIO.new
|
io = MemoryIO.new
|
||||||
response = HTTP::Server::Response.new(io)
|
response = HTTP::Server::Response.new(io)
|
||||||
|
|
|
@ -10,6 +10,8 @@ class CustomTestHandler < HTTP::Handler
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class CustomLogHandler < Kemal::BaseLogHandler; end
|
||||||
|
|
||||||
def create_request_and_return_io(handler, request)
|
def create_request_and_return_io(handler, request)
|
||||||
io = MemoryIO.new
|
io = MemoryIO.new
|
||||||
response = HTTP::Server::Response.new(io)
|
response = HTTP::Server::Response.new(io)
|
||||||
|
@ -34,6 +36,8 @@ def create_ws_request_and_return_io(handler, request)
|
||||||
end
|
end
|
||||||
|
|
||||||
Spec.before_each do
|
Spec.before_each do
|
||||||
Kemal.config.env = "development"
|
config = Kemal.config
|
||||||
Kemal.config.handlers.clear
|
config.env = "development"
|
||||||
|
config.setup_logging
|
||||||
|
config.handlers.clear
|
||||||
end
|
end
|
||||||
|
|
10
src/kemal.cr
10
src/kemal.cr
|
@ -4,9 +4,8 @@ require "./kemal/middleware/*"
|
||||||
at_exit do
|
at_exit do
|
||||||
Kemal::CLI.new
|
Kemal::CLI.new
|
||||||
config = Kemal.config
|
config = Kemal.config
|
||||||
if config.logging
|
config.setup_logging
|
||||||
config.logger.write "[#{config.env}] Kemal is ready to lead at #{config.scheme}://#{config.host_binding}:#{config.port}\n"
|
config.logger.write "[#{config.env}] Kemal is ready to lead at #{config.scheme}://#{config.host_binding}:#{config.port}\n"
|
||||||
end
|
|
||||||
config.add_handler Kemal::StaticFileHandler.new(config.public_folder)
|
config.add_handler Kemal::StaticFileHandler.new(config.public_folder)
|
||||||
config.add_handler Kemal::RouteHandler::INSTANCE
|
config.add_handler Kemal::RouteHandler::INSTANCE
|
||||||
|
|
||||||
|
@ -14,10 +13,7 @@ at_exit do
|
||||||
server.ssl = config.ssl
|
server.ssl = config.ssl
|
||||||
|
|
||||||
Signal::INT.trap {
|
Signal::INT.trap {
|
||||||
if config.logging
|
config.logger.write "Kemal is going to take a rest!\n"
|
||||||
config.logger.write "Kemal is going to take a rest!\n"
|
|
||||||
config.logger.handler.close
|
|
||||||
end
|
|
||||||
server.close
|
server.close
|
||||||
exit
|
exit
|
||||||
}
|
}
|
||||||
|
|
13
src/kemal/base_log_handler.cr
Normal file
13
src/kemal/base_log_handler.cr
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
require "http"
|
||||||
|
|
||||||
|
class Kemal::BaseLogHandler < HTTP::Handler
|
||||||
|
|
||||||
|
def initialize(@env)
|
||||||
|
end
|
||||||
|
|
||||||
|
def call(context)
|
||||||
|
end
|
||||||
|
|
||||||
|
def write(message)
|
||||||
|
end
|
||||||
|
end
|
|
@ -1,7 +1,7 @@
|
||||||
require "colorize"
|
require "colorize"
|
||||||
require "http"
|
require "http"
|
||||||
|
|
||||||
class Kemal::LogHandler < HTTP::Handler
|
class Kemal::CommonLogHandler < Kemal::BaseLogHandler
|
||||||
# INSTANCE = new
|
# INSTANCE = new
|
||||||
getter handler
|
getter handler
|
||||||
|
|
|
@ -2,7 +2,7 @@ module Kemal
|
||||||
class Config
|
class Config
|
||||||
INSTANCE = Config.new
|
INSTANCE = Config.new
|
||||||
HANDLERS = [] of HTTP::Handler
|
HANDLERS = [] of HTTP::Handler
|
||||||
property host_binding, ssl, port, env, public_folder, logging, logger
|
property host_binding, ssl, port, env, public_folder, logging
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
@host_binding = "0.0.0.0" unless @host_binding
|
@host_binding = "0.0.0.0" unless @host_binding
|
||||||
|
@ -10,7 +10,16 @@ module Kemal
|
||||||
@env = "development" unless @env
|
@env = "development" unless @env
|
||||||
@public_folder = "./public"
|
@public_folder = "./public"
|
||||||
@logging = true
|
@logging = true
|
||||||
@logger = Kemal::LogHandler.new(@env) if @logging
|
@logger = nil
|
||||||
|
end
|
||||||
|
|
||||||
|
def logger
|
||||||
|
@logger.not_nil!
|
||||||
|
end
|
||||||
|
|
||||||
|
def logger=(logger : Kemal::BaseLogHandler)
|
||||||
|
@logger = logger
|
||||||
|
HANDLERS << @logger.not_nil!
|
||||||
end
|
end
|
||||||
|
|
||||||
def scheme
|
def scheme
|
||||||
|
@ -21,15 +30,6 @@ module Kemal
|
||||||
HANDLERS
|
HANDLERS
|
||||||
end
|
end
|
||||||
|
|
||||||
def logger
|
|
||||||
@logger.not_nil!
|
|
||||||
end
|
|
||||||
|
|
||||||
def logger=(logger)
|
|
||||||
HANDLERS << logger
|
|
||||||
@logger = logger
|
|
||||||
end
|
|
||||||
|
|
||||||
def add_handler(handler : HTTP::Handler)
|
def add_handler(handler : HTTP::Handler)
|
||||||
HANDLERS << handler
|
HANDLERS << handler
|
||||||
end
|
end
|
||||||
|
@ -37,6 +37,16 @@ module Kemal
|
||||||
def add_ws_handler(handler : HTTP::WebSocketHandler)
|
def add_ws_handler(handler : HTTP::WebSocketHandler)
|
||||||
HANDLERS << handler
|
HANDLERS << handler
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def setup_logging
|
||||||
|
if @logging
|
||||||
|
@logger = Kemal::CommonLogHandler.new(@env)
|
||||||
|
HANDLERS << @logger.not_nil!
|
||||||
|
elsif @logging == false
|
||||||
|
@logger = Kemal::NullLogHandler.new(@env)
|
||||||
|
HANDLERS << @logger.not_nil!
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.config
|
def self.config
|
||||||
|
|
2
src/kemal/null_log_handler.cr
Normal file
2
src/kemal/null_log_handler.cr
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
class Kemal::NullLogHandler < Kemal::BaseLogHandler
|
||||||
|
end
|
|
@ -35,7 +35,6 @@ class Kemal::RouteHandler < HTTP::Handler
|
||||||
context.response.print body
|
context.response.print body
|
||||||
return context
|
return context
|
||||||
rescue ex
|
rescue ex
|
||||||
# Kemal.config.logger.write "Exception: #{ex.to_s}\n"
|
|
||||||
return render_500(context, ex.to_s)
|
return render_500(context, ex.to_s)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue