1
0
Fork 0
mirror of https://github.com/puma/puma.git synced 2022-11-09 13:48:40 -05:00

Allow to define a custom formatter for logs (#1816)

* Allow to define a custom formatter for logs
This commit is contained in:
Yoann Lecuyer 2019-08-01 21:21:23 +02:00 committed by Nate Berkopec
parent 0b584ab8cc
commit b0c56db7f6
5 changed files with 37 additions and 2 deletions

View file

@ -1,7 +1,7 @@
## Master
* Features
* Your feature goes here (#Github Number)
* Add log_formatter configuration (#1816)
* Bugfixes
* Your bugfix goes here (#Github Number)

View file

@ -282,6 +282,10 @@ module Puma
@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.
#

View file

@ -63,6 +63,9 @@ module Puma
@options = @config.options
@config.clamp
@events.formatter = Events::PidFormatter.new if clustered?
@events.formatter = options[:log_formatter] if @options[:log_formatter]
generate_restart_data
if clustered? && !Process.respond_to?(:fork)
@ -81,7 +84,6 @@ module Puma
set_rack_environment
if clustered?
@events.formatter = Events::PidFormatter.new
@options[:logger] = @events
@runner = Cluster.new(self, @events)

View file

@ -0,0 +1,3 @@
log_formatter do |str|
"[#{Process.pid}] [#{Socket.gethostname}] #{Time.now}: #{str}"
end

View file

@ -253,6 +253,32 @@ class TestCLI < Minitest::Test
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 NO_FORK_MSG unless HAS_FORK
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 Proc, cli.launcher.events.formatter
assert_match(/^\[.*\] \[.*\] .*: test$/, cli.launcher.events.format('test'))
end
def test_log_formatter_custom_clustered
skip NO_FORK_MSG unless HAS_FORK
cli = Puma::CLI.new [ "--config", "test/config/custom_log_formatter.rb", "-w 2" ]
assert_instance_of Proc, cli.launcher.events.formatter
assert_match(/^\[.*\] \[.*\] .*: test$/, cli.launcher.events.format('test'))
end
def test_state
url = "tcp://127.0.0.1:#{next_port}"
cli = Puma::CLI.new ["--state", @tmp_path, "--control", url]