Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom logger #2770

Merged
merged 23 commits into from
Mar 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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))
nateberkopec marked this conversation as resolved.
Show resolved Hide resolved
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