Skip to content

Commit

Permalink
Custom logger (#2770)
Browse files Browse the repository at this point in the history
* Add ability to specify custom logger

* Add test

* Use regex match

* Add custom logging in the log writer

* Write to custom logger instead of stdout if custom logger is present

* Check that the custom logger responds to write

* Use safe navigation

* Initialize to prevent warning

* Move test to not run parallel

* Update lib/puma/dsl.rb

Co-authored-by: Jack <5182053+phyzical@users.noreply.github.com>

* Update lib/puma/launcher.rb

Co-authored-by: Jack <5182053+phyzical@users.noreply.github.com>

* Update test/test_config.rb

Co-authored-by: Jack <5182053+phyzical@users.noreply.github.com>

* Update test/test_config.rb

Co-authored-by: Jack <5182053+phyzical@users.noreply.github.com>

* Update test/config/custom_logger.rb

Co-authored-by: Jack <5182053+phyzical@users.noreply.github.com>

---------

Co-authored-by: Jack <5182053+phyzical@users.noreply.github.com>
  • Loading branch information
vzajkov and phyzical committed Mar 29, 2023
1 parent 38d657d commit 1328380
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 2 deletions.
5 changes: 5 additions & 0 deletions lib/puma/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,11 @@ def log_requests(which=true)
@options[:log_requests] = which
end

# Pass in a custom logging class instance
def custom_logger(custom_logger)
@options[:custom_logger] = custom_logger
end

# Show debugging info
#
def debug
Expand Down
2 changes: 2 additions & 0 deletions lib/puma/launcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ def initialize(conf, launcher_args={})
@log_writer.formatter = LogWriter::PidFormatter.new if clustered?
@log_writer.formatter = options[:log_formatter] if @options[:log_formatter]

@log_writer.custom_logger = options[:custom_logger] if @options[:custom_logger]

generate_restart_data

if clustered? && !Puma.forkable?
Expand Down
9 changes: 7 additions & 2 deletions lib/puma/log_writer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ def call(str)
attr_reader :stdout,
:stderr

attr_accessor :formatter
attr_accessor :formatter, :custom_logger

# Create a LogWriter that prints to +stdout+ and +stderr+.
def initialize(stdout, stderr)
@formatter = DefaultFormatter.new
@custom_logger = nil
@stdout = stdout
@stderr = stderr

Expand All @@ -59,7 +60,11 @@ def self.null

# Write +str+ to +@stdout+
def log(str)
internal_write "#{@formatter.call str}\n"
if @custom_logger&.respond_to?(:write)
@custom_logger.write(format(str))
else
internal_write "#{@formatter.call str}\n"
end
end

def write(str)
Expand Down
13 changes: 13 additions & 0 deletions test/config/custom_logger.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class CustomLogger
def initialize(output=STDOUT)
@output = output
end

def write(msg)
@output.puts 'Custom logging: ' + msg
@output.flush
end
end

log_requests
custom_logger CustomLogger.new(STDOUT)
12 changes: 12 additions & 0 deletions test/test_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,18 @@ def assert_warning_for_hooks_defined_in_single_mode(hook_name)
end
end

# contains tests that cannot run parallel
class TestConfigFileSingle < TestConfigFileBase
def test_custom_logger_from_DSL
conf = Puma::Configuration.new { |c| c.load 'test/config/custom_logger.rb' }

conf.load
out, _ = capture_subprocess_io { conf.options[:custom_logger].write 'test' }

assert_equal "Custom logging: test\n", out
end
end

# Thread unsafe modification of ENV
class TestEnvModifificationConfig < TestConfigFileBase
def test_double_bind_port
Expand Down

0 comments on commit 1328380

Please sign in to comment.