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
Reopen logs working with renaming of the log file #1626
Conversation
test/rackup/hello-logs.ru
Outdated
def call(env) | ||
puts "write to log" | ||
@logger.info "hello" | ||
#p @logger.instance_variable_get(:@logdev) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this debugging which you want to keep in the test file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed
e65ee1a
to
181cb69
Compare
181cb69
to
8d211cb
Compare
8d211cb
to
74c1912
Compare
Please ask to re-open if you feel this is still needed in Puma. However, this PR as stands currently reimplements log rotation via SIGHUP without a) removing the old code path or b) providing a use case as to why this way is better. |
I was reminded of this recently when switching from Unicorn to Puma. Unicorn reopens all loggish-looking File objects on Which suggests an alternative! Offer some kind of Anyway, back to the beginning:
Filing this as a data point that while "better" is arguable, "easier to reliably migrate" from Unicorn is certain! |
|
Yep! Not everyone's a fan of the clever Unicorn behavior, either. 😈 I think it's possible to provide as a Puma plugin too though. I'll give that a shot to demonstrate. |
Here's what I set up, if you're looking for a quick way to get logrotate signaling Puma to reopen logfiles in your app. Caveats
# Gracefully reopen logfiles on HUP using Unicorn's reopener. Chains to
# Puma's HUP handler which signals its workers and reopens stdout/stderr.
module LogReopener
extend self
def trap(signal)
require "unicorn"
require "benchmark"
# Funky construction here: Signal.trap returns the previous trap, so we
# can refer to our own return value to chain handlers. (Puma's default
# handler broadcasts HUP to workers and reopens stdout/stderr in each.)
previous = Signal.trap(signal) do
yield reopen_logs
previous.call
end
end
def reopen_logs
timed { Unicorn::Util.reopen_logs }
end
private
def timed
result = nil
ms = 1000 * Benchmark.realtime { result = yield }
return result, ms
end
end
# Install the HUP handler as early as we can, before the whole app boots.
# Mimic Puma-esque log output indicating logfile reopens and delays.
LogReopener.trap "SIGHUP" do |count, elapsed|
puts "[#{Process.pid}] ! SIGHUP: reopened #{count} logfiles in #{elapsed.round}ms"
end |
Reopen logs of rack application after renaming log file