Skip to content

Commit

Permalink
rework
Browse files Browse the repository at this point in the history
  • Loading branch information
grosser committed Mar 9, 2018
1 parent dfdcbf6 commit 8de28aa
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions lib/samson/logging.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,25 @@
require 'syslog/logger'
config.logger = Syslog::Logger.new('samson')
config.lograge.formatter = Lograge::Formatters::Logstash.new
end

# puma kills itself with INT when it receives a SIGHUP see https://github.com/puma/puma/blob/master/docs/signals.md
# If we have a file based logger then we need to reopen that file to log to the new logfile after log rotation.
# The rails logger does not know what file it opened, so we have to help it https://github.com/rails/rails/issues/32211
# If we do not have a file-based logger we still do not want to kill the process, but log that we got the signal.
# We need to use self-pipe since we cannot reopen logs in an interrupt.
# This will break stdout_redirect feature of puma, which I hope nobody uses.
if ENV["SERVER_MODE"]
elsif ENV["SERVER_MODE"] # regular file logger that needs rotating
# Reopen logfile when we receive a SIGHUP for log-rotation
# puma normally kills itself with INT if it receives a SIGHUP https://github.com/puma/puma/blob/master/docs/signals.md
# We need to use self-pipe since we cannot reopen logs in an interrupt.
# This will break stdout_redirect feature of puma since we cannot support both
read, write = IO.pipe
Signal.trap(:SIGHUP) { write.puts }
Thread.new do
loop do
read.gets
Rails.logger.info "Received SIGHUP ... reopening logs"
# Rails.logger does not know what file it opened, so we help it https://github.com/rails/rails/issues/32211
dev = Rails.logger.instance_variable_get(:@logdev)&.dev
Rails.logger.reopen(dev.path) if dev&.is_a?(File)
Rails.logger.reopen(dev.path)
end
end
end

# workaround for our server ... always gets SIGHUP even though it does not do file logging
if ENV["IGNORE_SIGHUP"]
Signal.trap(:SIGHUP) {}
end

0 comments on commit 8de28aa

Please sign in to comment.