See http://jordan.broughs.net/archives/2014/09/provide-separate-rails-log-files-for-each-unicorn-worker
Last active
January 26, 2017 22:16
-
-
Save jordan-brough/c522280544d88d643499 to your computer and use it in GitHub Desktop.
Provide separate Rails log files for each Unicorn worker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
after_fork do |server, worker| | |
# This log hack provides separate log files for each unicorn worker. | |
# Since Unicorn forks worker processes after loggers are already initialized, by this point other | |
# things (like ActiveRecord::Base.logger) are already pointing directly at the current | |
# Rails.logger instance so we can't just point `Rails.logger` elsewhere. | |
logdev = Rails.logger.instance_variable_get(:@logdev) | |
ext = File.extname(logdev.dev.path) | |
path = logdev.dev.path.gsub /#{Regexp.escape(ext)}$/, ".#{worker.nr}#{ext}" | |
# open the file in the same way rails does: | |
# https://github.com/rails/rails/blob/4606e75/railties/lib/rails/application/bootstrap.rb#L38-L40 | |
file = File.open(path, 'a') | |
file.binmode | |
file.sync = Rails.application.config.autoflush_log | |
logdev.dev.flush | |
logdev.dev.close | |
logdev.instance_variable_set(:@dev, file) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This worked just right for a rails 4 application I am working on above all different solutions I've tried. Commenting here for visibility.