Heroku - how to flush Rails logs after each request - ruby-on-rails

I'm running a Rails 3 app on Heroku.
This is the output of heroku logs --tail --source app:
https://gist.github.com/4000998
The Started GET "/" lines appear in real time as HTTP requests are made. However, the Processing by Controller lines get buffered and then dumped to the log all at once.
This makes it very hard to debug issues in the app as it is difficult to match up the controller logs with the rest of the app's logs. How do I get the buffered logs to flush after each request?

Add the following line to your config.ru:
$stdout.sync = true

Related

Rails: I don't get any error messages in my log file only 500 page

I am running a rails 3 app under ruby 1.9.3 with puma.
When open localhost:3000 there is always the 500 page shown.
I've tried to set binding.pry and log messages across the files (e.g. ApplicationController), but it seems they are never reached.
I changed the root path to a controller that does not exist, still no log messages.
So it seems like the route is never reached as well.
I ve looked into the rails log, puma log, rack log but nothing is shown.
Any idea how to get appropriate error messages?
Please help, I am so frustrated, looking for the erros for days.
I should add that I can see requests in my log:
Started GET "/" for 127.0.0.1 at 2016-03-30 00:09:24 +0200
But that's it. Rails.logger isnt even logging most of the time.
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
is also set.
I've tried RACK_ENV=development puma config.ru

How can i track the issue in production RAILS

have on pdf creating function, but that PDF Generation different response, in local development working fine but the server shows 500 error. how can i track the issue in production ?
You can track issues in production in various way. There are several answers available. But better I should share very common practice regarding a rails application. If you are using apache/nginx as your http server. You can tail there log and make the operation on browser on the same time so you can get realtime log from the http server.
Next, based on your configuration (as per convention over configuration) there should be production.log in your app log directory. You can run the following command in your terminal/command line tools and check the log on real time:
tail -f log/production.log
I assumed in your case production.log is the log file currently logging everything by your production application server (passenger/unicorn/puma/etc..)
For assets and client end issues, try using your browser's console to inspect the issues.
I hope my answer will give you some idea!

Log response time at Heroku

Heroku adds a HTTP header called X-Request-Start. Its a unix timestamp.
I have a Rails 3.2 application at Heroku and want to log response time for HTTP requests.
Whats the latest point of possible integration in Rails to do:
start = Time.at(env['HTTP_X_REQUEST_START'].to_f / 1000.0).utc
response_time = Time.now.utc - start
I'm thinking of implementing a Rack middleware to calculate the response time.
Are you aware that Heroku already logs your response time? In your terminal do heroku logs -t --app your-app-name and you'll see service=xxxms which is the number of miliseconds each request takes.

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