Rails: How to debug in production environment? - ruby-on-rails

I've deployed to a VPS, but when visiting to my site, message shows:
We're sorry but something went wrong.
How could I start to debug?
I've deployed this commit successfully to heroku before. And before pushing to VPS, the site was running well.
I tried to see the log/production.log, but it only says Connecting to database specified by database.yml

Start with logs. Set more verbose log level to see what happens: set config.log_level = :debug in your config/environments/production.rb
More general, try something like errbit to see details about your production errors.

Related

Production log file is not updating, Rails 4.16 + Passanger

I have Rails 4.1.6 app on Passanger (Shared host).
Quite a while I worked in development, but now I switched to production.
In environment.rb:
ENV['RAILS_ENV'] = 'production'
then touch tmp/restart.txt to restart server.
When accessing my app I am receiving Internal Server Error. It is ok. I expected that, so I went to production.log to see what caused error, but it was blank.
Before I switched to production, development.log file showed all logs without problem.
What I tried:
1) Deleted production.log and restarted server. After folder reload production.log was created again. Permisions: 0664 the same as development.log
2) Added lines and restarted server :
config.log_level = :debug
RAILS_DEFAULT_LOGGER = Logger.new('log/production.log')
3) Double checked if current environment is production.
4) Manually wrote in production.log using console command:
Rails.logger.error('TEST ERROR')
After this command I saw this error message in production.log file.
Thanks in advance for any help.
Finally, I solved my problem.
Bassically, there wasn't any problem with production.log , problem was that my Rails 4 app didn't really start. I received Apache error:
Missing `secret_key_base` for 'production' environment, set this value in `config/secrets.yml`
So I solved above error with help of this answer
And now Rails 4 app starts and production.log updates without problem.

500 in Heroku logs, but no error details from Rails (even with rails_12factor)

When I tail the logs and hit my app, I have heroku[router] telling me I received a 500, and the app shows the usual "We're sorry, but something went wrong" message... but there are no additional details from Rails in the logs to tell me what is causing the 500.
I've read a couple of other threads that asked about this but they mostly end with people suggesting to do a rake:db migrate which fixed the error but there's no further discussion about the logging unfortunately. (And yes I'm sure there's no pending migrations 😄)
I have the rails_12factor gem installed
I've tried setting config.log_level = :debug in production.rb
I've tried creating a config variable of LOG_LEVEL set to DEBUG (which should supersede the config setting)
I've set $stdout.sync = true in config.ru as suggested here
What am I missing?
As an aside, have tried re-producing my actual problem locally with a backup of the database being used with Heroku but I can't reproduce it, so I'm finally forced to get to the bottom of this logging problem.
I first just got this to work by manually adding:
config.logger = Logger.new(STDOUT)
to production.rb but that obscured the real issue...
RACK_ENV and RAILS_ENV were both set to staging while I had the gem loading in the :production set... So although Heroku told me it was using the rails_12factor gem when the instance started, it wasn't really (I assumed it wouldn't be listed there if it wasn't a production environment, wrong assumption!)
staging.rb was loading production.rb which is why setting STDOUT there worked, thus confusing the issue further.
Thanks for the replies, and hope this helps someone else in future - go check your config variables and/or change where the gem is being loaded! :)
Debugging errors on heorku can get difficult most of the time.
For people facing a similar problem I suggest using PaperTrail heroku add-on for logging, It is free upto a certain limit.
It is realtime and provides with searching of logs, events hooks, mailing alerts etc. facilities. You will never have trouble debugging errors on heroku.
You can simply install it using the heroku cli command.
heroku addons:create papertrail

Rails stopped logging

My rails app running in development environment stopped logging all of a sudden and I am not able to figure out why.
I tried logging into a new file by doing
config.logger = Logger.new('log/temp.log')
config.log_level = :debug
But still no luck. The new file temp.log was created but nothing is logged in the file. The thing is this happens on my development server running nginx (I run my rails app using "rails s -d" on this server). The exact same files, when I run on my local machine (my own computer), logging works fine.
So I feel the reason logging is not working is because of something specific to the server, but then I didn't do anything much on the server (e.g. I didn't install new gems, etc.) Logging has been working fine until few days ago.
When I go to rails console
rails c
> Rails.logger.debug "hello"
=> true
I do get "hello" logged into 'log/temp.log' specified above in config file.
I think permission on log directory or file is ok. What else could be wrong?
I believe it's a locking issue which you might be able to solve after removing the call to the logger which causes this.
I ran into this issue with Redmine 1.x,
I found newrelic_rpm entry in the production.log, saying it didn't run, and 1 line of a Redmine plugin init.
After removing both, newrelic_rpm from the environment.rb (config.gem line), and the plugin logger init message, the logging facility appears to be restored and log entries are appearing again.

check 500 internal server error in production mode in rails 3.2

I have my app in production mode in my linode account and I get in one page a 500 internal server error the message:
We're sorry, but something went wrong.
However in my development environment works fine this page.
How can I debug this error?
How can I see the error origin in my production mode?
I want that rails show errors in production mode.
How can I do it?
Thank you!
If you have access to ssh, log in to your server via ssh and go to your rails log directory, which is inside your rails directory.
Once you are there run the command tail production.log . If this doesn't give you enough information you can also do a tail -n100 production.log (gives you last hundred lines of the production log).
If you have deployed via heroku, then you can access the logs by running heroku logs in your local console. (more information here https://devcenter.heroku.com/articles/logging)
I also find it helpful to use the exception_notification gem https://github.com/rails/exception_notification when running in production, as it emails you a stacktrace when an error occurs. Plenty of others also use Hoptoad (http://hoptoadapp.com/) or Exceptional (http://www.exceptional.io/) however i prefer the simple exception_notification gem.
Also, in some rare occasions when i can't trace the error as a final measure i sometimes open up port 3000 temporarily on the remote server firewall and cd to the rails project and run rails server production with the log level set to debug in config/environments/production.rb so i can see the error in the console, and then close off the port when i have finished.
Hope that helps.
tail -n100 production.log
will only show the last 100 lines of the log file. Just in case you want to see the log running in real time.
use this
tail -1000f log/production.log

How to properly diagnose a 500 error (Rails, Passenger, Nginx, Postgres)

I'm having a real tough time diagnosing a 500 error from my application running in production. I've had it working before, but after re-deploying via Capastrano I am unable to get it going.
Here are the facts:
The server is setup with nginx + passenger, and I'm using
PostgreSQL.
Static assets are working properly, as in I'm able to access them just fine in a browser.
I can access the rails console via RAILS_ENV=production bundle exec rails console and perform Active Record actions (like
retrieving data from the db).
Within console, I can run app.get("/"), which returns a 500 error as well (after first showing the query that was run to load
the model).
The production.log file is never written to. I've set permissions 777 on it just for the hell of it. I've also set the log level to
:debug with nothing to show for it.
The nginx log (which passenger also uses) shows no indication of errors, it just notifies about cache misses.
Because nothing of use is being logged, I have no idea what to do here. I've tried setting full permission on the entire app directory with no help. Restarted the server multiple times, nothing. The database is there and rails can clearly communicate with it. I'm not sure what I did to get it to run the first time around. I just don't know why rails isn't outputting anything to the log.
Okay, I figured this out. The app ran fine in development mode, so I knew something production-specific was screwing it up. I went into config/environments/production.rb and changes these settings:
# Full error reports are disabled and caching is turned on
config.consider_all_requests_local = false # changed from true
config.action_controller.perform_caching = true # changed from false
And then after restarting passenger, rails showed me the error w/ stacktrace. Turns out I was forgetting to precompile the asset pipeline!
Things to check
1) Are you sure you are running in production environment?
Check to see if any entries are in the development.log file
2) Set up your app to email you when a 500 error occurs with a full stack trace. I use the Exception Notifier gem but there are plenty of other solutions for this.
3) When checking your app in the console are you sure you are starting the console in production mode? It is possible that the app is not starting up at all and you just forgot to set the production param thereby thinking that the app runs fine when it doesn't.
4) Are you getting an nginx 500 error or the Rails 500 error? If nginx then it is likely that your app is not starting at all and highly unlikely that you will get any rails error in your log file. The assets are static files and navigating to them proves nothing other than that they exist.
5) Are you sure you are checking the right folder on the server? Sounds really stupid but capistrano could be deploying the app to a folder that is different to the folder that nginx is looking for for your app so double check both the folder capistrano is deploying to and the folder that nginx is looking for are the same.
Just a suggestion, I would use puma rather than passenger. It's awesome with nginx.
My problem is passenger's log file (error.log) has nothing. Then it's a rotation log issue. Run
passenger-config reopen-logs
solved my problem. More.
Have you tried running in development mode to see if the error reports itself?

Resources