Search code examples
ruby-on-railsnginxunicorn

Rails 3.2.x - No production log entries on an ubuntu, nginx, unicorn setup


On an ubuntu, nginx, unicorn setup the only production log entries I see are from command line commands such as deployments and migrations. I am getting no entries from the website activity at all.

I have read up on this bug report https://github.com/rails/rails/issues/4277#commit-ref-b332877 and I have tried adding

Rails.logger.instance_variable_get(:@logger).instance_variable_get(:@log_dest).sync = true if Rails.logger to an initializer as suggested but that just gives me an error indicating that sync is nil whenever I try to run anything at all

I have the same issue on rails v 3.2.1 and 3.2.9

Any ideas how I can get the log output?


Solution

  • Finally solved this Firstly I believe (I could be wrong and stand to be corrected on this) that at the time of asking this question that there were bugs in the unicorn gem that meant log entries were not being flushed see this report for further info on this particular issue https://github.com/rails/rails/issues/4277

    I'm using the latest unicorn gem and all is fine now but a quick note on the unicorn config file and other reasons why you may not see production log entries for an nGinx/Unicorn setup

    1) nGinx can return a 500 error (if you have your nginx config set up to do this) if it is unable to connect to your web server. i.e. unicorn has failed to start up.

    Obviously if unicorn is failing then your rails is not running and you will be hunting down rails log entries that just won't exist. If this is the case then check you unicorn config to see where you are writing unicorn errors to. My unicorn.rb file looks similar to this

    root = "/home/some_user/some_app/current"
    pid "#{root}/tmp/pids/unicorn.pid"
    stderr_path "#{root}/log/unicorn.log"
    stdout_path "#{root}/log/unicorn.log"
    

    Note that I am directing both error and normal unicorn log entries to the same log file. The standard is to use different log files but I just find it simpler for me to have only one unicorn log file to have to look through.

    The reason why you may be seeing 500 error pages instead of the bad gateway error that you would normally see if your web server is not running is that you may have the nginx.conf file set to serve static error pages from your app, so you may have something like this

    server {
      listen 80;
      server_name my_domain_name.com;
      root /path_to_my_app_root/current/public;
    
      location ^~ /assets/ {
        gzip_static on;
        expires max;
        add_header Cache-Control public;
      }
    
      try_files $uri/index.html $uri @unicorn;
      location @unicorn {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://ff1;
      }
    
      error_page 500 502 503 504 /500.html;
      client_max_body_size 4G;
      keepalive_timeout 10;
    }
    

    The error_page 500 502 503 504 /500.html; line tells nginx to serve your apps static 500.html page instead of the bad gateway nginx page if your server is not running. That's cool but it is confusing if you don't take into account that a rails 500 page is not necessarily going to give you production log entries.

    To sum up. If you are seeing your apps 500.html page and you are seeing no entries in your production.log to help you track down the issue just remember to check the unicorn error log file s defined in your unicorn.rb config to check what, if any, reason is being given for the server failing to start up.