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

Allow to define a custom formatter for logs #1816

Merged
merged 15 commits into from Aug 1, 2019
5 changes: 2 additions & 3 deletions History.md
@@ -1,7 +1,7 @@
## Master

* Features
* Your feature goes here (#Github Number)
* Add log_formatter configuration (#1816)

* Bugfixes
* Your bugfix goes here (#Github Number)
Expand All @@ -11,8 +11,7 @@
* 2 bugfixes
* Fix socket removed after reload - should fix problems with systemd socket activation. (#1829)
* Add extconf tests for DTLS_method & TLS_server_method, use in minissl.rb. Should fix "undefined symbol: DTLS_method" when compiling against old OpenSSL versions. (#1832)
* 1 other
ylecuyer marked this conversation as resolved.
Show resolved Hide resolved
* Removed unnecessary RUBY_VERSION checks. (#1827)


## 4.0.0 / 2019-06-25

Expand Down
4 changes: 4 additions & 0 deletions lib/puma/dsl.rb
Expand Up @@ -282,6 +282,10 @@ def stdout_redirect(stdout=nil, stderr=nil, append=false)
@options[:redirect_append] = append
end

def log_formatter(&block)
@options[:log_formatter] = block
end

# Configure +min+ to be the minimum number of threads to use to answer
# requests and +max+ the maximum.
#
Expand Down
10 changes: 10 additions & 0 deletions lib/puma/events.rb
Expand Up @@ -23,6 +23,16 @@ def call(str)
end
end

class CustomFormatter
def initialize(formatter)
@formatter = formatter
end

def call(str)
@formatter.call(str)
end
end

include Const

# Create an Events object that prints to +stdout+ and +stderr+.
Expand Down
10 changes: 6 additions & 4 deletions lib/puma/launcher.rb
Expand Up @@ -50,9 +50,6 @@ def initialize(conf, launcher_args={})
@original_argv = @argv.dup
@config = conf

@binder = Binder.new(@events)
@binder.import_from_env

@environment = conf.environment

# Advertise the Configuration
Expand All @@ -63,6 +60,11 @@ def initialize(conf, launcher_args={})
@options = @config.options
@config.clamp

@events.formatter = Events::CustomFormatter.new(@options[:log_formatter]) if @options[:log_formatter]

@binder = Binder.new(@events)
ylecuyer marked this conversation as resolved.
Show resolved Hide resolved
@binder.import_from_env

generate_restart_data

if clustered? && !Process.respond_to?(:fork)
Expand All @@ -81,7 +83,7 @@ def initialize(conf, launcher_args={})
set_rack_environment

if clustered?
@events.formatter = Events::PidFormatter.new
@events.formatter = Events::PidFormatter.new unless @options[:log_formatter]
ylecuyer marked this conversation as resolved.
Show resolved Hide resolved
@options[:logger] = @events

@runner = Cluster.new(self, @events)
Expand Down
3 changes: 3 additions & 0 deletions test/config/custom_log_formatter.rb
@@ -0,0 +1,3 @@
log_formatter do |str|
"[#{Process.pid}] [#{Socket.gethostname}] #{Time.now}: #{str}"
end
24 changes: 24 additions & 0 deletions test/test_cli.rb
Expand Up @@ -253,6 +253,30 @@ def test_state_file_callback_filtering
assert_empty keys_not_stripped
end

def test_log_formatter_default_single
cli = Puma::CLI.new [ ]
assert_instance_of Puma::Events::DefaultFormatter, cli.launcher.events.formatter
end

def test_log_formatter_default_clustered
skip_on :jruby, :windows

cli = Puma::CLI.new [ "-w 2" ]
assert_instance_of Puma::Events::PidFormatter, cli.launcher.events.formatter
end

def test_log_formatter_custom_single
cli = Puma::CLI.new [ "--config", "test/config/custom_log_formatter.rb" ]
assert_instance_of Puma::Events::CustomFormatter, cli.launcher.events.formatter
end

def test_log_formatter_custom_clustered
skip_on :jruby, :windows
ylecuyer marked this conversation as resolved.
Show resolved Hide resolved

cli = Puma::CLI.new [ "--config", "test/config/custom_log_formatter.rb", "-w 2" ]
assert_instance_of Puma::Events::CustomFormatter, cli.launcher.events.formatter
end

def test_state
url = "tcp://127.0.0.1:#{next_port}"
cli = Puma::CLI.new ["--state", @tmp_path, "--control", url]
Expand Down
2 changes: 1 addition & 1 deletion test/test_events.rb
Expand Up @@ -152,7 +152,7 @@ def test_pid_formatter
end

def test_custom_log_formatter
ylecuyer marked this conversation as resolved.
Show resolved Hide resolved
custom_formatter = proc { |str| "-> #{ str }" }
custom_formatter = Puma::Events::CustomFormatter.new(Proc.new { |str| "-> #{str}" })

out, _ = capture_io do
events = Puma::Events.stdio
Expand Down