reidmorrison / rails_semantic_logger

Rails Semantic Logger replaces the Rails default logger with Semantic Logger
https://logger.rocketjob.io/rails
Apache License 2.0
330 stars 116 forks source link

Unicorn after_fork? #2

Closed dvdplm closed 10 years ago

dvdplm commented 11 years ago

We have an issue where no logging happens in our rails app after our before_fork handler executes on a deploy.

What is the right way to re-initialize SemanticLogger after the fork? (I'm guessing that is what happens).

We currently have code like this in the unicorn.conf:

after_fork do |server, worker|
  if defined?(ActiveRecord::Base)
    ActiveRecord::Base.establish_connection
    Rails.logger.info('Connected to ActiveRecord')
  end

  if defined?(Resque)
    Resque.redis = ENV['REDIS_RESQUE_URL']
    Rails.logger.info("Resque connected to Redis at #{ENV['REDIS_RESQUE_URL']}")
  end
end

We do not see those two log messages (and none of the others). Pointers?

reidmorrison commented 11 years ago

I put a patch into SemanticLogger to allow re-opening of the File Appender.

Add the following to the Gemfile before rails_semantic_logger:

gem 'semantic_logger', :git => 'git://github.com/ClarityServices/semantic_logger.git'

Then update the above code as follows:

after_fork do |server, worker|
  if defined?(ActiveRecord::Base)
    ActiveRecord::Base.establish_connection
    # Re-open the File Appender
    SemanticLogger.reopen
    Rails.logger.info('Connected to ActiveRecord')
  end

  if defined?(Resque)
    Resque.redis = ENV['REDIS_RESQUE_URL']
    Rails.logger.info("Resque connected to Redis at #{ENV['REDIS_RESQUE_URL']}")
  end
end

This feature is also useful if anyone wants to delete the log file rather than use a coney truncate approach to log file rotation.

Let me know if this works and I will publish the gem for it