3

We are running a rails project behind haproxy. There is a keep-alive sent to the application every second. This is causing very noisy log files which makes it a bit of a pain to dig through and is making them unnecessarily large.

My first thought was to change the logging level for that action to debug, but someone else proposed changing the logging level in an around_filter. I am not crazy about that idea, but it could just be how I implemented it. I am open to different solutions, but the general requirements are that I can quiet those actions, but I could change the logging level if I needed to see them for whatever reason.

Geoff Lanotte
  • 7,490
  • 1
  • 38
  • 50
  • I see your problem, but I think the filter option is the easiest solution in my eyes. Use the `before_filter` to higher the log level och the `after_filter` to reset it. Everything else sees quite complicated... – klump Apr 10 '12 at 01:33
  • I know "log4j" could do this very easyly, however I am not sure if 'log4r' could do this. waiting for others suggestions. – Siwei Apr 10 '12 at 01:34
  • Mhh, you could always use a lower log level than the default and just lower the default log level if you need the output. If there is no lower log level try hacking your own on into the system. – klump Apr 10 '12 at 02:07

1 Answers1

4

Another solution is to insert some Rack middleware which handles the keep-alive check before it gets to the Rails ApplicationController lifecycle.

Step 1: make some middleware which respondes to the keep-alive check. In my example the keep-alive request is a GET /health-check so it would look like:

class HealthCheckMiddleware
  def initialize(app)
    @app = app
  end

  def call(env)
    if env['PATH_INFO'] == '/health-check'
      return [200, {}, ['healthy']]
    end
    @app.call(env)
  end
end

Of course, adjust this health check as necessary. Maybe you need to check other Request / CGI variables...

Step 2: make sure you insert this middleware before Rails::Rack::Logger:

config.middleware.insert_before Rails::Rack::Logger, "HealthCheckMiddleware"

Now your middleware will handle the health check and your logs have been totally by-passed.

Cody Caughlan
  • 32,456
  • 5
  • 63
  • 68