mirror of
				https://gitea.invidious.io/iv-org/shard-kemal.git
				synced 2024-08-15 00:53:36 +00:00 
			
		
		
		
	Improve logging and specs
This commit is contained in:
		
							parent
							
								
									dc3cc74798
								
							
						
					
					
						commit
						98efbfe341
					
				
					 7 changed files with 41 additions and 30 deletions
				
			
		
							
								
								
									
										22
									
								
								spec/common_log_handler_spec.cr
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								spec/common_log_handler_spec.cr
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,22 @@ | ||||||
|  | require "./spec_helper" | ||||||
|  | 
 | ||||||
|  | describe "Kemal::CommonLogHandler" do | ||||||
|  |   it "logs to the given IO" do | ||||||
|  |     config = Kemal.config | ||||||
|  |     io = MemoryIO.new | ||||||
|  |     logger = Kemal::CommonLogHandler.new io | ||||||
|  |     logger.write "Something" | ||||||
|  |     io.to_s.should eq "Something" | ||||||
|  |   end | ||||||
|  | 
 | ||||||
|  |   it "creates log message for each request" do | ||||||
|  |     request = HTTP::Request.new("GET", "/") | ||||||
|  |     io = MemoryIO.new | ||||||
|  |     context_io = MemoryIO.new | ||||||
|  |     response = HTTP::Server::Response.new(context_io) | ||||||
|  |     context = HTTP::Server::Context.new(request, response) | ||||||
|  |     logger = Kemal::CommonLogHandler.new io | ||||||
|  |     logger.call(context) | ||||||
|  |     io.to_s.should_not be nil | ||||||
|  |   end | ||||||
|  | end | ||||||
|  | @ -1,14 +0,0 @@ | ||||||
| require "./spec_helper" |  | ||||||
| 
 |  | ||||||
| describe "Kemal::LogHandler" do |  | ||||||
|   it "creates a handler" do |  | ||||||
|     logger = Kemal::CommonLogHandler.new |  | ||||||
|     logger.handler.should_not be nil |  | ||||||
|   end |  | ||||||
| 
 |  | ||||||
|   it "creates a STDOUT handler by default" do |  | ||||||
|     config = Kemal.config |  | ||||||
|     logger = Kemal::CommonLogHandler.new |  | ||||||
|     logger.handler.should be_a IO |  | ||||||
|   end |  | ||||||
| end |  | ||||||
|  | @ -9,7 +9,13 @@ class CustomTestHandler < HTTP::Handler | ||||||
|   end |   end | ||||||
| end | end | ||||||
| 
 | 
 | ||||||
| class CustomLogHandler < Kemal::BaseLogHandler; end | class CustomLogHandler < Kemal::BaseLogHandler | ||||||
|  |   def call(context) | ||||||
|  |   end | ||||||
|  | 
 | ||||||
|  |   def write(message) | ||||||
|  |   end | ||||||
|  | end | ||||||
| 
 | 
 | ||||||
| def create_request_and_return_io(handler, request) | def create_request_and_return_io(handler, request) | ||||||
|   io = MemoryIO.new |   io = MemoryIO.new | ||||||
|  |  | ||||||
|  | @ -1,14 +1,7 @@ | ||||||
| module Kemal | module Kemal | ||||||
|   # All loggers must inherit from `Kemal::BaseLogHandler`. |   # All loggers must inherit from `Kemal::BaseLogHandler`. | ||||||
|   class Kemal::BaseLogHandler < HTTP::Handler |   abstract class Kemal::BaseLogHandler < HTTP::Handler | ||||||
|     def initialize |     abstract def call(context) | ||||||
|     end |     abstract def write(message) | ||||||
| 
 |  | ||||||
|     def call(context) |  | ||||||
|       call_next context |  | ||||||
|     end |  | ||||||
| 
 |  | ||||||
|     def write(message) |  | ||||||
|     end |  | ||||||
|   end |   end | ||||||
| end | end | ||||||
|  |  | ||||||
|  | @ -1,10 +1,9 @@ | ||||||
| module Kemal | module Kemal | ||||||
|   class CommonLogHandler < Kemal::BaseLogHandler |   class CommonLogHandler < Kemal::BaseLogHandler | ||||||
|     @handler : IO::FileDescriptor |     @handler : IO | ||||||
|     getter handler |  | ||||||
| 
 | 
 | ||||||
|     def initialize |     def initialize(io : IO) | ||||||
|       @handler = STDOUT |       @handler = io | ||||||
|     end |     end | ||||||
| 
 | 
 | ||||||
|     def call(context) |     def call(context) | ||||||
|  |  | ||||||
|  | @ -74,7 +74,7 @@ module Kemal | ||||||
| 
 | 
 | ||||||
|     private def setup_log_handler |     private def setup_log_handler | ||||||
|       @logger ||= if @logging |       @logger ||= if @logging | ||||||
|                     Kemal::CommonLogHandler.new |                     Kemal::CommonLogHandler.new(STDOUT) | ||||||
|                   else |                   else | ||||||
|                     Kemal::NullLogHandler.new |                     Kemal::NullLogHandler.new | ||||||
|                   end |                   end | ||||||
|  |  | ||||||
|  | @ -1,5 +1,10 @@ | ||||||
| module Kemal | module Kemal | ||||||
|   # This is here to represent the logger corresponding to Null Object Pattern. |   # This is here to represent the logger corresponding to Null Object Pattern. | ||||||
|   class NullLogHandler < Kemal::BaseLogHandler |   class NullLogHandler < Kemal::BaseLogHandler | ||||||
|  |     def call(context) | ||||||
|  |     end | ||||||
|  | 
 | ||||||
|  |     def write(message) | ||||||
|  |     end | ||||||
|   end |   end | ||||||
| end | end | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue