mirror of
				https://gitea.invidious.io/iv-org/shard-kemal.git
				synced 2024-08-15 00:53:36 +00:00 
			
		
		
		
	Remove file handler from CommonLogHandler. Log only to STDOUT. Fixes #159
This commit is contained in:
		
							parent
							
								
									9d84d51975
								
							
						
					
					
						commit
						3ea6a01afc
					
				
					 5 changed files with 8 additions and 36 deletions
				
			
		|  | @ -29,7 +29,7 @@ describe "Macros" do | ||||||
|     end |     end | ||||||
|     it "sets a custom logger" do |     it "sets a custom logger" do | ||||||
|       config = Kemal::Config::INSTANCE |       config = Kemal::Config::INSTANCE | ||||||
|       logger CustomLogHandler.new("production") |       logger CustomLogHandler.new | ||||||
|       config.handlers.last.should be_a(CustomLogHandler) |       config.handlers.last.should be_a(CustomLogHandler) | ||||||
|       config.logger.should be_a(CustomLogHandler) |       config.logger.should be_a(CustomLogHandler) | ||||||
|     end |     end | ||||||
|  |  | ||||||
|  | @ -2,35 +2,13 @@ require "./spec_helper" | ||||||
| 
 | 
 | ||||||
| describe "Kemal::LogHandler" do | describe "Kemal::LogHandler" do | ||||||
|   it "creates a handler" do |   it "creates a handler" do | ||||||
|     logger = Kemal::CommonLogHandler.new "production" |     logger = Kemal::CommonLogHandler.new | ||||||
|     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::CommonLogHandler.new "production" |     logger = Kemal::CommonLogHandler.new | ||||||
|     logger.handler.should be_a IO |     logger.handler.should be_a IO | ||||||
|   end |   end | ||||||
| 
 |  | ||||||
|   it "creates a file handler in production" do |  | ||||||
|     config = Kemal.config |  | ||||||
|     config.env = "production" |  | ||||||
|     logger = Kemal::CommonLogHandler.new "production" |  | ||||||
|     logger.handler.should be_a File |  | ||||||
|   end |  | ||||||
| 
 |  | ||||||
|   it "writes to a file in production" do |  | ||||||
|     config = Kemal.config |  | ||||||
|     config.env = "production" |  | ||||||
|     logger = Kemal::CommonLogHandler.new "production" |  | ||||||
|     request = HTTP::Request.new("GET", "/?message=world&time=now") |  | ||||||
|     io = MemoryIO.new |  | ||||||
|     response = HTTP::Server::Response.new(io) |  | ||||||
|     context = HTTP::Server::Context.new(request, response) |  | ||||||
|     logger.call(context) |  | ||||||
|     response.close |  | ||||||
|     str = File.read("kemal.log") |  | ||||||
|     File.delete("kemal.log") |  | ||||||
|     str.includes?("GET /?message=world&time=now").should eq true |  | ||||||
|   end |  | ||||||
| end | end | ||||||
|  |  | ||||||
|  | @ -2,7 +2,7 @@ require "http" | ||||||
| 
 | 
 | ||||||
| # All loggers must inherit from `Kemal::BaseLogHandler`. | # All loggers must inherit from `Kemal::BaseLogHandler`. | ||||||
| class Kemal::BaseLogHandler < HTTP::Handler | class Kemal::BaseLogHandler < HTTP::Handler | ||||||
|   def initialize(@env : String) |   def initialize | ||||||
|   end |   end | ||||||
| 
 | 
 | ||||||
|   def call(context) |   def call(context) | ||||||
|  |  | ||||||
|  | @ -4,14 +4,8 @@ class Kemal::CommonLogHandler < Kemal::BaseLogHandler | ||||||
|   @handler : IO::FileDescriptor |   @handler : IO::FileDescriptor | ||||||
|   getter handler |   getter handler | ||||||
| 
 | 
 | ||||||
|   def initialize(@env) |   def initialize | ||||||
|     @handler = if @env == "production" |     @handler = STDOUT | ||||||
|                  handler = File.new("kemal.log", "a") |  | ||||||
|                  handler.flush_on_newline = true |  | ||||||
|                  handler |  | ||||||
|                else |  | ||||||
|                  STDOUT |  | ||||||
|                end |  | ||||||
|   end |   end | ||||||
| 
 | 
 | ||||||
|   def call(context) |   def call(context) | ||||||
|  |  | ||||||
|  | @ -61,9 +61,9 @@ module Kemal | ||||||
| 
 | 
 | ||||||
|     def setup_log_handler |     def setup_log_handler | ||||||
|       @logger ||= if @logging |       @logger ||= if @logging | ||||||
|                     Kemal::CommonLogHandler.new(@env) |                     Kemal::CommonLogHandler.new | ||||||
|                   else |                   else | ||||||
|                     Kemal::NullLogHandler.new(@env) |                     Kemal::NullLogHandler.new | ||||||
|                   end |                   end | ||||||
|       HANDLERS.insert(0, @logger.not_nil!) |       HANDLERS.insert(0, @logger.not_nil!) | ||||||
|     end |     end | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue