Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Provide separate Rails log files for each Unicorn worker
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(
path = /#{Regexp.escape(ext)}$/, ".#{}#{ext}"
# open the file in the same way rails does:
file =, 'a')
file.sync = Rails.application.config.autoflush_log
logdev.instance_variable_set(:@dev, file)

This comment has been minimized.

Copy link

@ggayan ggayan commented Nov 16, 2016

This worked just right for a rails 4 application I am working on above all different solutions I've tried. Commenting here for visibility.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.