From 9cf89e6971eed5351c82b6d45bf81ae4aa0dd6da Mon Sep 17 00:00:00 2001 From: Valery Zajkov Date: Sun, 12 Dec 2021 15:14:22 -0500 Subject: [PATCH] Add ability to specify custom logger (#2725) * Add ability to specify custom logger * Add test * Use regex match --- lib/puma/dsl.rb | 5 +++++ test/config/custom_logger.rb | 13 +++++++++++++ test/test_config.rb | 16 +++++++++++++--- 3 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 test/config/custom_logger.rb diff --git a/lib/puma/dsl.rb b/lib/puma/dsl.rb index 308a9687fe..21f3818cc1 100644 --- a/lib/puma/dsl.rb +++ b/lib/puma/dsl.rb @@ -365,6 +365,11 @@ def log_requests(which=true) @options[:log_requests] = which end + # Pass in a custom logging class instance + def logger(custom_logger) + @options[:logger] = custom_logger + end + # Show debugging info # def debug diff --git a/test/config/custom_logger.rb b/test/config/custom_logger.rb new file mode 100644 index 0000000000..0794625f38 --- /dev/null +++ b/test/config/custom_logger.rb @@ -0,0 +1,13 @@ +class CustomLogger + def initialize(output=STDOUT) + @output = output + end + + def write(msg) + @output.print 'Custom logging: ' + msg + @output.flush + end +end + +log_requests +logger CustomLogger.new(STDOUT) diff --git a/test/test_config.rb b/test/test_config.rb index 758910ca18..b478441813 100644 --- a/test/test_config.rb +++ b/test/test_config.rb @@ -59,6 +59,19 @@ def test_ssl_configuration_from_DSL assert_equal [200, {}, ["embedded app"]], app.call({}) end + def test_custom_logger_from_DSL + conf = Puma::Configuration.new do |c| + c.load "test/config/custom_logger.rb" + end + + conf.load + out, _ = capture_subprocess_io do + conf.options[:logger].write('test') + end + + assert_match /Custom logging: test/, out + end + def test_ssl_bind skip_if :jruby skip_unless :ssl @@ -122,9 +135,6 @@ def test_ssl_bind_jruby assert_equal [ssl_binding], conf.options[:binds] end - - - def test_ssl_bind_no_tlsv1_1 skip_if :jruby skip_unless :ssl