简体   繁体   中英

Rails 5.2 / Ruby 2.5: How to silence spammy WEBrick request logs?

I just upgraded an app from Ruby 2.3 / Rails 4.2 to Ruby 2.5.3 / Rails 5.2.2. The upgrade went smoothly but now when I test making calls in my development server, I notice that each page request generates two extra log lines in STDOUT (but not in my Rails log/development.log ) that I infer are coming from WEBrick, not Rails. I'd like to remove them. See below for example, the first line is the Rails request log (I want to keep that one) and the following two are ugly and redundant:

...
■ [GET /guiding_principles] params={} user=51 (Topher Hunt) status=200 duration=505.81ms
::1 - - [14/Jan/2019:21:22:45 CET] "GET /guiding_principles HTTP/1.1" 200 8840
http://localhost:3000/guiding_principles -> /guiding_principles
...

If you're starting a fresh WEBrick server, it looks like there's ways to configure or probably silence logging, see eg here and here . But I don't see a clear way (or any guidance on how) to do that in a Rails context.

How can I tell WEBrick not to log anything on each request?

I found a monkeypatch that does an adequate job of this. With this patch in place, no per-request log lines will be produced, but the init lines (reminding you that you're running WEBrick) are still shown.

# (config/initializers/webrick.rb)
# Silence WEBrick's verbose per-request logging
# See https://github.com/ruby/ruby : /lib/webrick/httpserver.rb
if Rails.env.development?
  require 'webrick'

  module WEBrick
    class HTTPServer
      def access_log(a, b, c)
        nil
      end
    end
  end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM