mirror of
https://gitea.invidious.io/iv-org/shard-kemal.git
synced 2024-08-15 00:53:36 +00:00
Start decoupling logger
This commit is contained in:
parent
ffc3d6d224
commit
850af8819e
6 changed files with 15 additions and 12 deletions
|
@ -28,4 +28,10 @@ describe "Config" do
|
||||||
config.add_handler CustomTestHandler.new
|
config.add_handler CustomTestHandler.new
|
||||||
config.handlers.size.should eq(1)
|
config.handlers.size.should eq(1)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "sets a custom logger" do
|
||||||
|
config = Kemal::Config::INSTANCE
|
||||||
|
config.logger = CustomTestHandler.new
|
||||||
|
config.handlers.first.should be_a(CustomTestHandler)
|
||||||
|
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
|
logger = Kemal::LogHandler.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
|
logger = Kemal::LogHandler.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
|
logger = Kemal::LogHandler.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
|
logger = Kemal::LogHandler.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)
|
||||||
|
|
|
@ -5,8 +5,6 @@ at_exit do
|
||||||
Kemal::CLI.new
|
Kemal::CLI.new
|
||||||
config = Kemal.config
|
config = Kemal.config
|
||||||
if config.logging
|
if config.logging
|
||||||
logger = Kemal::LogHandler.new
|
|
||||||
config.logger = logger
|
|
||||||
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
|
end
|
||||||
config.add_handler Kemal::StaticFileHandler.new(config.public_folder)
|
config.add_handler Kemal::StaticFileHandler.new(config.public_folder)
|
||||||
|
|
|
@ -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
|
property host_binding, ssl, port, env, public_folder, logging, logger
|
||||||
|
|
||||||
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,7 @@ module Kemal
|
||||||
@env = "development" unless @env
|
@env = "development" unless @env
|
||||||
@public_folder = "./public"
|
@public_folder = "./public"
|
||||||
@logging = true
|
@logging = true
|
||||||
@logger = nil
|
@logger = Kemal::LogHandler.new(@env) unless @logging
|
||||||
end
|
end
|
||||||
|
|
||||||
def scheme
|
def scheme
|
||||||
|
|
|
@ -2,11 +2,10 @@ require "colorize"
|
||||||
require "http"
|
require "http"
|
||||||
|
|
||||||
class Kemal::LogHandler < HTTP::Handler
|
class Kemal::LogHandler < HTTP::Handler
|
||||||
INSTANCE = new
|
# INSTANCE = new
|
||||||
getter handler
|
getter handler
|
||||||
|
|
||||||
def initialize
|
def initialize(@env)
|
||||||
@env = Kemal.config.env
|
|
||||||
@handler = if @env == "production"
|
@handler = if @env == "production"
|
||||||
handler = File.new("kemal.log", "a")
|
handler = File.new("kemal.log", "a")
|
||||||
handler.flush_on_newline = true
|
handler.flush_on_newline = true
|
||||||
|
|
|
@ -35,7 +35,7 @@ class Kemal::RouteHandler < HTTP::Handler
|
||||||
context.response.print body
|
context.response.print body
|
||||||
return context
|
return context
|
||||||
rescue ex
|
rescue ex
|
||||||
Kemal::LogHandler::INSTANCE.write "Exception: #{ex.to_s}\n"
|
# 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