Close
Glad You're Ready. Let's Get Started!

Let us know how we can contact you.

Thank you!

We'll respond shortly.

Ryan Ong

Posts By

LABS
How to split up rails 3.x logger by unicorn workers

TLDR: Put this in your unicorn config. Make sure not to have duplicate after_fork calls

after_fork do |server, worker| if defined?(ActiveSupport::TaggedLogging) && Rails.logger.kind_of?(ActiveSupport::TaggedLogging) Rails.logger.instance_eval do level = @logger.level # flush and close logger to make sure we don't cut off any potential logging before this @logger && @logger.flush && @logger.close @logger = ActiveSupport::BufferedLogger.new(Rails.root.join("log", "rails_#{Rails.env}.#{worker.nr}.log").to_s, level) end elsif Rails.logger.kind_of?(ActiveSupport::BufferedLogger) Rails.logger.instance_eval do @log && @log.flush && @log.close @log_path = Rails.root.join("log", "rails_#{Rails.env}.#{worker.nr}.log").to_s if respond_to?(:open_logfile, true) @log = open_logfile(@log_path) elsif respond_to?(:open_log, true) if File.exist?(@log_path) @log = open_log(@log_path, (File::WRONLY | File::APPEND)) else FileUtils.mkdir_p(File.dirname(@log_path)) @log = open_log(l@log_path, (File::WRONLY | File::APPEND | File::CREAT)) end end end end end How this works!

Read more »