Log output too verbose - how to change log level? - ruby-on-rails

I'm trying to debug my simple Rails app and find navigating and printing with puts works well but the log in Bash is way too verbose. I've looked through lots of questions/responses here and have tried some of them but without success. I tried importing the quiet_assets Gem with
gem 'quiet_assets', group: :development
and have tried changing log levels to :error in production.rb
config.log_level = Logger::error
But I still get all the GET requests of Assets when navigating making review of logs difficult.
Worth noting I've tried restarting the server and ran bundle just in case. I did hard refresh even though browser cache shouldn't have anything to do with this.
Here's example navigation:
20:08:55 web.1 | 127.0.0.1 - - [22/Oct/2014 20:08:55] "GET /application.js HTTP/1.1" 304 - 0.0185
20:08:55 web.1 | 127.0.0.1 - - [22/Oct/2014 20:08:55] "GET /application.css HTTP/1.1" 304 - 0.0790
20:08:56 web.1 | 127.0.0.1 - - [22/Oct/2014 20:08:56] "GET /brands/46 HTTP/1.1" 304 - 0.0111
20:08:56 web.1 | 127.0.0.1 - - [22/Oct/2014 20:08:56] "GET /application.js HTTP/1.1" 304 - 0.0062
20:08:56 web.1 | 127.0.0.1 - - [22/Oct/2014 20:08:56] "GET /application.css HTTP/1.1" 304 - 0.0066
20:08:57 web.1 | 127.0.0.1 - - [22/Oct/2014 20:08:57] "GET /brands/46 HTTP/1.1" 304 - 0.0105
20:08:57 web.1 | 127.0.0.1 - - [22/Oct/2014 20:08:57] "GET /application.js HTTP/1.1" 304 - 0.0056
20:08:57 web.1 | 127.0.0.1 - - [22/Oct/2014 20:08:57] "GET /application.css HTTP/1.1" 304 - 0.0063
20:09:13 web.1 | E, [2014-10-22T20:09:13.695412 #5880] ERROR -- : worker=0 PID:5916 timeout (16s > 15s), killing
20:09:13 web.1 | E, [2014-10-22T20:09:13.704183 #5880] ERROR -- : reaped #<Process::Status: pid 5916 SIGKILL (signal 9)> worker=0
20:09:13 web.1 | I, [2014-10-22T20:09:13.710805 #5923] INFO -- : worker=0 ready

It seems you are seeing production log, are you? if that is the case quiet_assets should be included in production too.
gem 'quiet_assets'

Related

Flasgger execution is a success but not showing the XML body

While Deploying iris prediction model using flasgger API, i can enter the lengths and widths of the iris flower with http/200 ok , but i do not see an xml output with the prediction results.
From ipython notebook:
Running on http://127.0.0.1:5001/ (Press CTRL+C to quit)
127.0.0.1 - - [20/Jan/2020 17:16:23] "GET /predict?s_length=3.2&s_width=2.0&p_length=3.0&p_width=4.5 HTTP/1.1" 200 -
127.0.0.1 - - [20/Jan/2020 17:16:23] "GET /favicon.ico HTTP/1.1" 404 -
127.0.0.1 - - [20/Jan/2020 17:16:47] "GET /apidocs/ HTTP/1.1" 200 -
127.0.0.1 - - [20/Jan/2020 17:16:47] "GET /flasgger_static/swagger-ui.css HTTP/1.1" 200 -
127.0.0.1 - - [20/Jan/2020 17:16:47] "GET /flasgger_static/swagger-ui-bundle.js HTTP/1.1" 200 -
127.0.0.1 - - [20/Jan/2020 17:16:47] "GET /flasgger_static/swagger-ui-standalone-preset.js HTTP/1.1" 200 -
127.0.0.1 - - [20/Jan/2020 17:16:48] "GET /flasgger_static/lib/jquery.min.js HTTP/1.1" 200 -
127.0.0.1 - - [20/Jan/2020 17:16:48] "GET /apispec_1.json HTTP/1.1" 200 -
127.0.0.1 - - [20/Jan/2020 17:16:48] "GET /flasgger_static/favicon-32x32.png HTTP/1.1" 200 -
127.0.0.1 - - [20/Jan/2020 17:17:03] "GET /predict?s_length=4.2&s_width=5.6&p_length=2.6&p_width=6.3 HTTP/1.1" 200 -
Hope you all are keeping well.
So i found the problem after few days of struggling with it, looking for solution ,I guess i was too lazy to post it on GitHub or maybe got busy , the error indicated above was a result of incomplete XML missing 200 body for an HTTP Response , which did not output any XML Result when I entered the input parameters and run the prediction from the GUI , which is expected in my case.
So it is important that we provide a complete XML body with 200 response , we can also add the kind of results you would expect under the 200 XML Body for the prediction based on your Project.
Adding the 200 in the XML body as shown below, fixed the problem.
def predict_iris_file():
"""Example file endpoint returning a prediction of iris
---
parameters:
- name: input_file
in: formData
type: file
required: true
responses:
200:
description: "0: Iris-setosa, 1: Iris-versicolor, 2: Iris-virginica"
"""
Regards
akudnaver
Sorted the issue.
Responses should be in the same indent as parameters and 200 should be indented below responses

Duplicated request info log in Rails 5 server only app

I have a Rails 5.2.3 application with Ruby 2.4.5.
I found a weird issue that the request info are logged twice in stdout.
Here is the log config in config/environments/product.rb
config.logger = ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new(STDOUT))
config.log_tags = [ lambda {|req| "#{req.cookie_jar["_session_id"]}" }, :remote_ip, :uuid ]
Supposedly it will tag every log with the remote ip and request uuid, and it does for most of logs, except there is a weird additional log for the request. In following example, the last line is a duplicated log for the request without the tag:
[INFO] [2019-10-28 06:11:45 UTC] [127.0.0.1] [f6de1900-a7e5-4486-8b73-7095d0cacb35] Started GET "/api/v1/nodes?pageSize=20&pageNumber=1" for 127.0.0.1 at 2019-10-28 06:11:45 +0000
[INFO] [2019-10-28 06:11:45 UTC] [127.0.0.1] [f6de1900-a7e5-4486-8b73-7095d0cacb35] Processing by Api::V1::NodesController#index as XML
...
...
[INFO] [2019-10-28 06:20:02 UTC] [127.0.0.1] [993e0db4-3995-41ef-851a-bfea1bc25781] Completed 200 OK in 1084ms (Views: 341.9ms | ActiveRecord: 150.6ms)
127.0.0.1 - - [28/Oct/2019:06:20:02 +0000] "GET /api/v1/nodes?pageSize=20&pageNumber=1 HTTP/1.1" 200 - 1.1347
I checked configurations, there is no any other logger configured.
The consequence is that there is a timer in client side to check notification status by every 5 seconds. I added a log silencer to avoid log such request:
# config/application.rb
config.middleware.insert_before Rails::Rack::Logger, LogSilencer, silenced: /notification_messages/
# lib/log_silencer.rb
class LogSilencer
def initialize(app, opts = {})
#app = app
#silenced = opts.delete(:silenced)
end
def call(env)
if #silenced.match(env['PATH_INFO'])
Rails.logger.silence do
#app.call(env)
end
else
#app.call(env)
end
end
end
It does avoid logging from the tagged logger, but the duplicated request logs are still there, then the stdout is full of this request
127.0.0.1 - - [28/Oct/2019:07:04:21 +0000] "GET /api/v1/notification_messages/to_notify HTTP/1.1" 200 - 0.0118
127.0.0.1 - - [28/Oct/2019:07:04:26 +0000] "GET /api/v1/notification_messages/to_notify HTTP/1.1" 200 - 0.0140
127.0.0.1 - - [28/Oct/2019:07:04:31 +0000] "GET /api/v1/notification_messages/to_notify HTTP/1.1" 200 - 0.0137
127.0.0.1 - - [28/Oct/2019:07:04:36 +0000] "GET /api/v1/notification_messages/to_notify HTTP/1.1" 200 - 0.0149
...
Spent a whole day try to find out who generated this log, but no any clue...
Would like to ask for help on how to turn off this log so make the log data clear...
Thanks!
Seems the logs come from the web server used when up the Rails app.
When use WEBrick, the log is like
=> Booting WEBrick
=> Rails 5.2.3 application starting in development on http://localhost:4000
=> Run `rails server -h` for more startup options
[2019-10-30 07:01:13] INFO WEBrick 1.3.1
[2019-10-30 07:01:13] INFO ruby 2.4.5 (2018-10-18) [x86_64-linux]
[2019-10-30 07:01:13] INFO WEBrick::HTTPServer#start: pid=5812 port=4000
127.0.0.1 - - [30/Oct/2019:07:01:17 UTC] "GET /test HTTP/1.1" 304 0
- -> /test
When use unicorn, the log is like:
I, [2019-10-30T07:02:47.626962 #5956] INFO -- : Refreshing Gem list
I, [2019-10-30T07:02:48.289373 #5956] INFO -- : listening on addr=0.0.0.0:4000 fd=17
I, [2019-10-30T07:02:48.393598 #5956] INFO -- : master process ready
I, [2019-10-30T07:02:48.394853 #5965] INFO -- : worker=0 ready
I, [2019-10-30T07:02:48.399878 #5968] INFO -- : worker=1 ready
127.0.0.1 - - [30/Oct/2019:07:02:52 +0000] "GET /test HTTP/1.1" 304 - 0.0724
I can find that the request log format is a bit different (the time zone info, the time consumed etc).
I use Unicorn, its logger by default uses stderr, but seems the logger configuration does not work
logger Logger.new("#{rails_root}/log/unicorn.log")
so I have to set the stderr path
stderr_path "#{rails_root}/log/unicorn.stderr.log
Then the request logs are in the unicorn.stderr.log file, and STDOUT are the rails app logs.
But still dont know how to turn it off since it is a kind of duplicated and useless log...

rails 4.2 can't see db queries in heroku logs even though log_level set to :debug

Here is a project I'm working on: https://github.com/homanchou/the_drawing_challenge
I'm having trouble seeing any db queries in the server logs on heroku or even locally in the terminal when running Puma with Foreman.
All I see are asset or web requests
21:54:34 web.1 | started with pid 54784
21:54:35 web.1 | [54784] Puma starting in cluster mode...
21:54:35 web.1 | [54784] * Version 2.10.2 (ruby 2.1.5-p273), codename: Robots on Comets
21:54:35 web.1 | [54784] * Min threads: 1, max threads: 4
21:54:35 web.1 | [54784] * Environment: development
21:54:35 web.1 | [54784] * Process workers: 2
21:54:35 web.1 | [54784] * Preloading application
21:54:36 web.1 | [54784] * Listening on tcp://0.0.0.0:5000
21:54:36 web.1 | [54784] Use Ctrl-C to stop
21:54:36 web.1 | [54784] - Worker 0 (pid: 54785) booted, phase: 0
21:54:36 web.1 | [54784] - Worker 1 (pid: 54786) booted, phase: 0
21:54:43 web.1 | [54785] 127.0.0.1 - - [02/Apr/2015:21:54:43 -0700] "GET / HTTP/1.1" 200 - 0.3120
21:54:43 web.1 | [54785] 127.0.0.1 - - [02/Apr/2015:21:54:43 -0700] "GET /logo_header.png HTTP/1.1" 304 - 0.0079
21:54:45 web.1 | [54785] 127.0.0.1 - - [02/Apr/2015:21:54:45 -0700] "GET /challenges/8 HTTP/1.1" 200 - 0.0443
21:54:45 web.1 | [54785] 127.0.0.1 - - [02/Apr/2015:21:54:45 -0700] "GET /logo_header.png HTTP/1.1" 304 - 0.0067
I had to add another line to the Procfile log: tail -f -n 0 log/development.log to see logs or errors in stdout of foreman start like I normally do, but that only works locally and not on heroku unless I want to scale up a process for that tailing.
In my other rails project I can see the db queries fine, even on heroku I can see the db queries in the log:
2015-04-03T03:46:40.052067+00:00 app[web.1]: Started GET "/api/notifications/unread_count" for 97.93.34.8 at 2015-04-03 03:46:40 +0000
2015-04-03T03:46:40.070865+00:00 app[web.1]: Merchant Load (2.7ms) SELECT "merchants".* FROM "merchants" WHERE "merchants"."id" = $1 ORDER BY "merchants"."id" ASC LIMIT 1 [["id", 7502]]
2015-04-03T03:46:40.085454+00:00 app[web.1]: SQL (2.3ms) UPDATE "merchants" SET "updated_at" = '2015-04-03 03:46:40.074442', "last_active_at" = '2015-04-03 03:46:40.074442' WHERE "merchants"."id" = $1 [["id", 7502]]
2015-04-03T03:46:40.101382+00:00 app[web.1]: (2.3ms) SELECT COUNT(*) FROM "notifications" WHERE "notifications"."merchant_id" = $1 AND "notifications"."state" = $2 [["merchant_id", 7502], ["state", "unread"]]
2015-04-03T03:46:40.058083+00:00 app[web.1]: Processing by NotificationsController#unread_count as JSON
2015-04-03T03:46:40.073916+00:00 app[web.1]: (1.0ms) BEGIN
2015-04-03T03:46:40.093737+00:00 app[web.1]: (7.4ms) COMMIT
2015-04-03T03:46:40.102858+00:00 app[web.1]: Completed 200 OK in 45ms (Views: 0.2ms | ActiveRecord: 15.6ms)
I tried comparing the two and even copying over some environment configurations but I still can't figure it out. I've tried setting config.log_level = :debug on the production environment. I can see db queries in the ruby console, but not the heroku or local puma stdout in the terminal. What am I missing?
If you need to use puma for websockets etc try adding
ActiveRecord::Base.logger.level = Logger::DEBUG
to your environments.rb or environments/production

Rails shows incorrect time spent in a request

When I do-
$ time curl 'http://localhost:3000'
I get this: 0.00s user 0.00s system 0% cpu 1.270 total
Gem 'rack-mini-profiler' also show time taken almost same as curl.
But in the rails log, I see-
Completed 200 OK in 103ms (Views: 72.7ms | ActiveRecord: 5.0ms)
Why there is time difference of about 1 second?
Curl and rails server both are running on my development machine.
"time curl" is timing how long the curl client takes to fetch Rails content, ie it's the client time.
The Rails log is showing you the server time.
You did not specify that but if you are running development mode, there is classes reloading and assets compilation, which heavily impacts response time. But it does not affect production.
Here is what I got in my rails 4.2 application (first curl, than corresponding excerpts from log):
curl 'http://localhost:3000' 0.01s user 0.00s system 0% cpu 1.817 total
curl 'http://localhost:3000' 0.01s user 0.00s system 2% cpu 0.366 total
Completed 200 OK in 1254ms (Views: 1206.2ms | ActiveRecord: 13.9ms)
127.0.0.1 - - [04/Oct/2014:19:48:52 +0200] "GET / HTTP/1.1" 200 - 1.6863
Completed 200 OK in 279ms (Views: 278.7ms | ActiveRecord: 0.0ms)
127.0.0.1 - - [04/Oct/2014:19:49:41 +0200] "GET / HTTP/1.1" 200 - 0.2895
While in production:
curl 'http://localhost:3000' 0.01s user 0.00s system 3% cpu 0.329 total
curl 'http://localhost:3000' 0.00s user 0.01s system 11% cpu 0.090 total
I, [2014-10-04T19:54:02.739988 #27830] INFO -- : Completed 200 OK in 143ms (Views: 123.0ms | ActiveRecord: 6.3ms)
I, [2014-10-04T19:54:17.521175 #27830] INFO -- : Completed 200 OK in 41ms (Views: 40.9ms | ActiveRecord: 0.0ms)
So differences persist there, but it's probably due to time for send request from curl to server and then to receive the response. I still think that bigger differences in development are caused by reloading and compiling.

How to resolve Rack Lint errors?

I have noticed that certain requests, particularly from Uptime Robot to test page uptime have recently started generated the following Rack Lint error on my Rails 3.1.x app:
21:47:05 web.1 | Started HEAD "/" for 74.86.158.106 at 2012-03-22 21:47:05 -0400
21:47:05 web.1 | Processing by HomeController#index as */*
...
21:47:05 web.1 | /Users/username/.rvm/gems/ruby-1.9.3-p125#project/gems/activesupport-3.1.4/lib/active_support/core_ext/string/output_safety.rb:23: warning: regexp match /.../n against to UTF-8 string
...
21:47:05 web.1 | Completed 200 OK in 81ms (Views: 60.3ms | ActiveRecord: 0.0ms)
21:47:05 web.1 | 74.86.158.106 - - [22/Mar/2012 21:47:05] "GET / HTTP/1.1" 200 31842 0.1993
21:47:05 web.1 | E, [2012-03-22T21:47:05.546848 #18938] ERROR -- : app error: Content-Length header was 31842, but should be 0 (Rack::Lint::LintError)
21:47:05 web.1 | E, [2012-03-22T21:47:05.546978 #18938] ERROR -- : /Users/username/.rvm/gems/ruby-1.9.3-p125#project/gems/rack-1.3.6/lib/rack/lint.rb:19:in `assert'
21:47:05 web.1 | E, [2012-03-22T21:47:05.547079 #18938] ERROR -- : /Users/username/.rvm/gems/ruby-1.9.3-p125#project/gems/rack-1.3.6/lib/rack/lint.rb:501:in `verify_content_length'
21:47:05 web.1 | E, [2012-03-22T21:47:05.547178 #18938] ERROR -- : /Users/username/.rvm/gems/ruby-1.9.3-p125#project/gems/rack-1.3.6/lib/rack/lint.rb:525:in `each'
21:47:05 web.1 | E, [2012-03-22T21:47:05.547277 #18938] ERROR -- : /Users/username/.rvm/gems/ruby-1.9.3-p125#project/gems/rack-1.3.6/lib/rack/body_proxy.rb:23:in `method_missing'
21:47:05 web.1 | E, [2012-03-22T21:47:05.547375 #18938] ERROR -- : /Users/username/.rvm/gems/ruby-1.9.3-p125#project/gems/unicorn-4.2.0/lib/unicorn/http_response.rb:41:in `http_response_write'
21:47:05 web.1 | E, [2012-03-22T21:47:05.547476 #18938] ERROR -- : /Users/username/.rvm/gems/ruby-1.9.3-p125#project/gems/unicorn-4.2.0/lib/unicorn/http_server.rb:538:in `process_client'
21:47:05 web.1 | E, [2012-03-22T21:47:05.547574 #18938] ERROR -- : /Users/username/.rvm/gems/ruby-1.9.3-p125#project/gems/unicorn-4.2.0/lib/unicorn/http_server.rb:604:in `worker_loop'
21:47:05 web.1 | E, [2012-03-22T21:47:05.547672 #18938] ERROR -- : /Users/username/.rvm/gems/ruby-1.9.3-p125#project/gems/unicorn-4.2.0/lib/unicorn/http_server.rb:487:in `spawn_missing_workers'
21:47:05 web.1 | E, [2012-03-22T21:47:05.547770 #18938] ERROR -- : /Users/username/.rvm/gems/ruby-1.9.3-p125#project/gems/unicorn-4.2.0/lib/unicorn/http_server.rb:137:in `start'
21:47:05 web.1 | E, [2012-03-22T21:47:05.547867 #18938] ERROR -- : /Users/username/.rvm/gems/ruby-1.9.3-p125#project/gems/unicorn-4.2.0/bin/unicorn:121:in `<top (required)>'
21:47:05 web.1 | E, [2012-03-22T21:47:05.547966 #18938] ERROR -- : /Users/username/.rvm/gems/ruby-1.9.3-p125#project/bin/unicorn:19:in `load'
21:47:05 web.1 | E, [2012-03-22T21:47:05.548063 #18938] ERROR -- : /Users/username/.rvm/gems/ruby-1.9.3-p125#project/bin/unicorn:19:in `<main>'
Any ideas as to what may be going on here? I'm really not finding a solution right now that specifically addresses what to do in this scenario as far as poking and hunting down what really may be going on.
I know it's an old question, but I have the exact same issue. Requests from UptimeRobot and New Relic (and only from IPs from those two) give these log messages.
Both UptimeRobot and NewRelic reports 'OK'.
It seems that it is not the request that Rack::Lint is complaining about, but the response. Look at the stacktrace. It comes through http_response_write of unicorn (I too use unicorn).
I will investigate more and add any findings here.
UPDATE:
I am able to reproduce the issue with
$ curl https://example.com -X HEAD -i
HTTP/1.1 200 OK
Server: nginx/1.6.2 (Ubuntu)
Date: Wed, 18 Nov 2015 09:17:11 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 18422
Connection: keep-alive
Status: 200 OK
...
curl: (18) transfer closed with 18422 bytes remaining to read
So it seems that my old Rails is not supporting HEAD requests properly.
UPDATE: I am not able to reproduce on my vagrant box or on the staging server. Only difference from production I can think of is SSL.
Looks like your Uptime robot is sending a header with the wrong value. It should be sending the Content-length header with a value of 0 (or not sending it at all) but it's sending it with 31842, which is probably wrong.
You could direct it to some other http server, dump all headers and see why it is sending this or just change your uptime robot to not send this header.

Resources