Hide rendering of partials from rails logs - ruby-on-rails

I believe the default behavior of rails logging on production is to not output the rendering of all partials. This should log in development but not on production.
However, I'm seeing this in production and I'm not sure how to remove it. My logs are too noisy. My production environment is Heroku running Unicorn and using Papertrail to view my logs. I know Unicorn does some wonky stuff with logs and to get them working properly in the first place I had to add this to my production.rb:
config.logger = Logger.new(STDOUT)
config.logger.level = Logger.const_get('INFO')
( Explained here: http://help.papertrailapp.com/kb/configuration/unicorn )
But even with log_level INFO I'm seeing huge blocks of these in all my logs:
Jun 25 22:15:15 tacktile app/web.1: Rendered photos/pieces/_caption.html.erb (0.7ms)
Jun 25 22:15:15 tacktile app/web.1: Rendered photos/pieces/_rights.html.erb (2.1ms)
Jun 25 22:15:15 tacktile app/web.1: Rendered photos/pieces/_category.html.erb (4.8ms)
Jun 25 22:15:15 tacktile app/web.1: Rendered photos/pieces/_caption.html.erb (0.3ms)
Jun 25 22:15:15 tacktile app/web.1: Rendered photos/pieces/_rights.html.erb (0.4ms)
Jun 25 22:15:15 tacktile app/web.1: Rendered photos/pieces/_category.html.erb (4.4ms)
Jun 25 22:15:15 tacktile app/web.1: Rendered photos/pieces/_caption.html.erb (0.3ms)
Jun 25 22:15:15 tacktile app/web.1: Rendered photos/pieces/_rights.html.erb (0.3ms)
Jun 25 22:15:15 tacktile app/web.1: Rendered photos/pieces/_category.html.erb (1.8ms)
Jun 25 22:15:15 tacktile app/web.1: Rendered photos/pieces/_caption.html.erb (0.4ms)
Jun 25 22:15:15 tacktile app/web.1: Rendered photos/pieces/_rights.html.erb (4.6ms)
Jun 25 22:15:15 tacktile app/web.1: Rendered photos/pieces/_category.html.erb (2.1ms)
Jun 25 22:15:15 tacktile app/web.1: Rendered photos/pieces/_caption.html.erb (0.3ms)
Jun 25 22:15:15 tacktile app/web.1: Rendered photos/pieces/_rights.html.erb (0.4ms)
Jun 25 22:15:15 tacktile app/web.1: Rendered photos/pieces/_category.html.erb (4.1ms)
Jun 25 22:15:15 tacktile app/web.1: Rendered photos/pieces/_caption.html.erb (0.2ms)
Jun 25 22:15:15 tacktile app/web.1: Rendered photos/pieces/_rights.html.erb (1.8ms)
Jun 25 22:15:15 tacktile app/web.1: Rendered photos/pieces/_category.html.erb (6.0ms)
Jun 25 22:15:15 tacktile app/web.1: Rendered photos/pieces/_caption.html.erb (0.5ms)
Jun 25 22:15:15 tacktile app/web.1: Rendered photos/pieces/_rights.html.erb (0.8ms)
Jun 25 22:15:15 tacktile app/web.1: Rendered photos/pieces/_category.html.erb (1.9ms)
Jun 25 22:15:15 tacktile app/web.1: Rendered photos/pieces/_caption.html.erb (0.3ms)
Jun 25 22:15:15 tacktile app/web.1: Rendered photos/pieces/_rights.html.erb (0.7ms)

For Rails 4 (at least):
Try this in your config/environments/development.rb
config.action_view.logger = nil

Use Lograge, it removes rendering times for partials

I got the following answer from a papertrail:
I think the quickest way to deal with this is to use our log filtering
functionality. That'll let you drop anything that matches a regex and
will save you from having to make any app configuration changes.
In the longer term, you'll probably want to silence these messages at
source. Lograge is probably your best bet for that. You may find it
also removes a few other bits but give it a go and let me know what
you think.
I know this is probably irrelevant to you at the moment, but for
future use you might also find some other useful tips here. It covers
lograge, removing static asset requests and unnecessary actions,
Let me know if you need help with anything mentioned above.

Instead of completely disabling actionview logging (as described in another answer), I opted to change the logging level for rendering to DEBUG. This way, it can be easily omitted from production logs by setting the log level to INFO or higher.
Note, this is for rails 5.2. I'm unsure if it would work on other versions.
module ViewLoggingOverride
def info(progname = nil, &block)
logger.debug(progname, &block) if logger
end
end
ActionView::LogSubscriber.include(ViewLoggingOverride)
Relevant rails code:
https://github.com/rails/rails/blob/5-2-stable/actionview/lib/action_view/log_subscriber.rb
https://github.com/rails/rails/blob/5-2-stable/activesupport/lib/active_support/log_subscriber.rb#L93-L99

This is so common that, in Papertrail emails notifying you of a logging spike, there is a link with this exact example.
This is the link.
I tweaked the regex a bit:
/\A\s{3}Rendered \w+\/_.+\.erb \(\d+\.\d+ms\)\z/
PS: I always found it odd they are printed at info level in the first place.

Related

invalid authencity token (rails, heroku, puma)

I was playing around with my rails app (puma server, deployed on heroku) in production, and got the authenticity error when submitted a long form (when I render the form auth token seems to be okay, the value is visible).
Usually the form works well. It was only the second time this has happened. In both cases a long time (~1hour) passed between rendering and submitting the form.
I couldn't find any answer to this problem. Is there a time limit for submitting the form based on when the form was rendered? Or is this a browser issue? It would be important to have this form open for a while since it's pretty long and users may want to think before they fill out the and submit the form.
Mar 01 07:36:32 appfaskyn app/web.1: Started POST "/products" for 141.101.96.195 at 2016-03-01 15:36:31 +0000
Mar 01 07:36:32 appfaskyn app/web.1: Processing by ProductsController#create as HTML
Mar 01 07:36:32 appfaskyn app/web.1: Can't verify CSRF token authenticity
Mar 01 07:36:32 appfaskyn app/web.1: ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticityToken):
Mar 01 07:36:32 appfaskyn app/web.1: vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.4/lib/action_controller/metal/request_forgery_protection.rb:181:in `handle_unverified_request'
Mar 01 07:36:32 appfaskyn app/web.1: vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.4/lib/action_controller/metal/request_forgery_protection.rb:209:in `handle_unverified_request'
Mar 01 07:36:32 appfaskyn app/web.1: vendor/bundle/ruby/2.2.0/gems/devise-3.5.6/lib/devise/controllers/helpers.rb:257:in `handle_unverified_request'
Mar 01 07:36:32 appfaskyn app/web.1: vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.4/lib/action_controller/metal/request_forgery_protection.rb:204:in `verify_authenticity_token'

I'm having huge Rails log is it normal?

I'm having a huge rails application log. is it normal ?'
768 megs for the production log !!
root#demo3:/home/canvas/canvas/log# ls -lh
total 960M
-rw-r--r-- 1 canvas canvas 192M Sep 28 12:37 delayed_job.log
-rw-rw-r-- 1 canvas canvas 265 Sep 22 08:57 development.log
-rw-r--r-- 1 canvas canvas 910K Sep 28 12:36 newrelic_agent.log
-rw-r--r-- 1 canvas canvas 768M Sep 28 12:37 production.log
-rw-r--r-- 1 root root 26K Sep 28 11:00 super_delayed_job_err.log
-rw-r--r-- 1 root root 113K Sep 22 14:07 super_delayed_job.log
Snippet from the log file:
[- 1e1f92f0-293e-0132-2906-00163c067c2e] Cache hit: _account_lookup2/1 ({})
[- 1e1f92f0-293e-0132-2906-00163c067c2e] Cache hit: settings_for_plugin2/sessions ({})
[- 208bd370-293e-0132-2906-00163c067c2e]
Processing UsersController#user_dashboard (for 54.248.250.232 at 2014-09-28 14:06:04) [GET]
[- 208bd370-293e-0132-2906-00163c067c2e] Parameters: {"controller"=>"users", "action"=>"user_dashboard"}
[- 208bd370-293e-0132-2906-00163c067c2e] Redirected to http://subdomain.example.com/login
[- 208bd370-293e-0132-2906-00163c067c2e] Filter chain halted as [:require_user] rendered_or_redirected.
[- 208bd370-293e-0132-2906-00163c067c2e] Completed in 3ms (DB: 0) | 302 Found [http://demo3.iqraalms.com/]
[- 208bd370-293e-0132-2906-00163c067c2e] [STAT] 903612 903612 0 903612 0.010000000000000231 0
Any idea how to optimise it ?
You could raise the log level to get less data (warn, error or fatal) in your config file as described in rails guide on debugging. Or as sjaime pointed out in his comment, logrotate is a utility that will solve this problem for you (compress your log every day/week/month or when it reaches a certain size, delete/email/keep old archives, ...).
One thing that will blow up your log tremendously are for example asset errors (missing fonts is a classic there). Make sure you have non of these. Other than that, with log level info and a couple of users on your site, your log will grow quickly.

Heroku Block Requests to URL

For some reason, a script of some sort makes a repeated request to my site every second or faster to a URL that doesn't exist. It's painful because it clogs up the logs (and is an unnecessary (although small) consumption of resources). Just wondering if there's a good way to deal with this for a site hosted on Heroku. The requests come from a different IP address each time.
Edit: As a note, the requests are always to the exact same URL.
Here's an example, which repeats every second or so, except from a different IP:
Feb 22 08:37:28 myApp app/web.1: ActionController::RoutingError (Not Found):
Feb 22 08:37:28 myApp app/web.1: app/controllers/application_controller.rb:31:in `not_found'
Feb 22 08:37:28 myApp app/web.1: app/controllers/my_controller.rb:141:in `my_method'
Feb 22 08:37:28 myApp app/web.1: [Exceptiontrap] Raised Exceptiontrap::Rack::Exception
Feb 22 08:37:28 myApp app/web.1: [Exceptiontrap] Catched Exception: ActionController::RoutingError
Feb 22 08:37:28 myApp app/web.1: Started GET "/aSpecificURL" for 109.242.56.44 at 2014-02-22 13:37:28 +0000
Feb 22 08:37:28 myApp heroku/router: at=info method=GET path=/aSpecificURL host=www.myApp.com request_id=9caeabcf-adcc-417f-940d-0458a81d9c32 fwd="109.242.56.44" dyno=web.1 connect=2ms service=24ms status=404 bytes=1632
You can't block specific requests through heroku. It sounds to me like someone is scanning your app for security vulnerabilities.
You could setup cloudflare to help block some of the requests. But overall, this is pretty common and not something to worry about.

Can't open rails app, even if server is running

I've been developing a rails app with a VM and vagrant. Until yesterday, everything was working fine. However, when I tried to run the server and open the browser, it wasn't working. I tried on Chrome, Firefox and Safari, but the error persists on all three. I checked my dev logs, and this is what it says.
Yesterday's log:
Started GET "/" for 10.0.2.2 at 2013-12-11 06:38:31 +0000 Processing by HomeController#index as HTML Rendered home/index.html.erb within layouts/application (43.6ms) Completed 200 OK in 159ms (Views: 158.0ms | ActiveRecord: 0.0ms)
and today's log says:
Started GET "/" for 127.0.0.1 at 2013-12-12 19:02:04 +0000
[1m[36mActiveRecord::SchemaMigration Load (1.4ms)[0m [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m
Processing by HomeController#index as */*
Rendered home/index.html.erb within layouts/application (212.7ms)
Completed 200 OK in 595ms (Views: 555.4ms | ActiveRecord: 0.0ms)
[1m[36mActiveRecord::SchemaMigration Load (1.5ms)[0m [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m
[1m[35mActiveRecord::SchemaMigration Load (0.2ms)[0m SELECT "schema_migrations".* FROM "schema_migrations"
Could anyone explain why the address it's trying to connect to appears to be different? And what needs to be done in order to be able to connect to it again?
Thanks.

routing error in my console after installing ckeditor

i am using rails 3.0.3 and i recently installed ckeditor to add text editor in my articles.before adding this editor everything was working fine.but after installing ckeditor(followed this : https://github.com/hificreative/rails-3-ckeditor) i am getting text editor in my articles page but i am getting routes error in console and i am not able to save data on clicking save button...
console output :
Started GET "/javascripts/reset.js" for 127.0.0.1 at Fri Mar 23 13:28:19 +0530 2012
ActionController::RoutingError (No route matches "/javascripts/reset.js"):
Rendered /usr/lib/ruby/gems/1.8/gems/actionpack-3.0.3/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (1.5ms)
Started GET "/javascripts/grid.js" for 127.0.0.1 at Fri Mar 23 13:28:19 +0530 2012
ActionController::RoutingError (No route matches "/javascripts/grid.js"):
Rendered /usr/lib/ruby/gems/1.8/gems/actionpack-3.0.3/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (0.8ms)
Started GET "/javascripts/style.js" for 127.0.0.1 at Fri Mar 23 13:28:19 +0530 2012
ActionController::RoutingError (No route matches "/javascripts/style.js"):
Rendered /usr/lib/ruby/gems/1.8/gems/actionpack-3.0.3/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (2.2ms)
ActionController::RoutingError (No route matches "/javascripts/style.js"):
Rendered /usr/lib/ruby/gems/1.8/gems/actionpack-3.0.3/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (2.2ms)
Started GET "/javascripts/messages.js" for 127.0.0.1 at Fri Mar 23 13:28:19 +0530 2012
ActionController::RoutingError (No route matches "/javascripts/messages.js"):
Rendered /usr/lib/ruby/gems/1.8/gems/actionpack-3.0.3/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (1.8ms)
Started GET "/javascripts/forms.js" for 127.0.0.1 at Fri Mar 23 13:28:19 +0530 2012
ActionController::RoutingError (No route matches "/javascripts/forms.js"):
Rendered /usr/lib/ruby/gems/1.8/gems/actionpack-3.0.3/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (1.4ms)
i have included following in my gemfile :
gem 'ckeditor', '3.5.0'
following are the steps that i followed to install ckeditor:
Next step is generate ckeditor js files and configuration file Check “config/initializers/ckeditor.rb” for more configuration options:
rails generate ckeditor:base
Generate ckeditor models for file upload support: For paperclip:
rails generate ckeditor:migration
For attachment_fu:
rails generate ckeditor:migration --backend=attachment_fu
Don’t forget about migration:
rake db:migarte
included following line in my lauout.application.html.haml file
= javascript_include_tag :ckeditor
and wrote following line in my articles _form.html.haml file
form.cktext_area :content
how to solve this error.....
Not sure I can help with that particular gem but I just installed using this one with instant success.
https://github.com/galetahub/ckeditor
Ok, I just restarted my PC and then everything started working normally, without this error.

Resources