Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable Rack::CommonLogger without monkey patching

So, I want to have completely custom logging for my sinatra application, but I can't seem to disable the Rack::CommonLogger.

As per the sinatra docs all I should need to do is add the following line (tried setting it to false as well):

set :logging, nil

to my configuration. This does not work however, and I still receive the Apache-like log messages in my terminal. So the only solution I've found so far is to monkey patch the damn thing.

module Rack
  class CommonLogger
    def call(env)
      # do nothing
      @app.call(env)
    end
  end
end

Anyone got any ideas if it's possible to disable this without restorting to such matters?

like image 203
nicohvi Avatar asked Mar 06 '14 08:03

nicohvi


3 Answers

I monkey patched the log(env, status, header, began_at) function, which is what gets called by rack to produce the apache style logs. We use jruby with logback so we have no use for all the custom logging that seems to pervade the ruby ecosystem. I suspect fishwife is initalizing the CommonLogger, which might explain why all my attempts to disable it or to configure it with a custom logger fail. Probably puma does something similar. I actually had two instances at one point. One logging with my custom logger (yay) and another one still doing its silly puts statements on stderr. I must say, I'm pretty appalled with the logging hacks in the rack ecosystem. Seems somebody needs a big cluebat to their heads.

Anyway, putting this in our config.ru works for us:

module Rack
  class CommonLogger
    def log(env, status, header, began_at)
      # make rack STFU; our logging middleware takes care of this      
    end
  end
end

In addition to that, I wrote my own logging middleware that uses slf4j with a proper MDC so we get more meaningful request logging.

like image 79
Jilles van Gurp Avatar answered Nov 10 '22 15:11

Jilles van Gurp


Puma adds logging middleware to your app if you are in development mode and haven’t set the --quiet option.

To stop Puma logging in development, pass the -q or --quiet option on the command line:

puma -p 3001 -q

or if you are using a Puma config file, add quiet to it.

like image 36
matt Avatar answered Nov 10 '22 13:11

matt


Rack includes a few middlewares by default when you rackup your application. It is defined in this file.

By default, as you mention, the logging middleware is enabled.

To disable it, just pass the option --quiet to rackup:

rackup --quiet

And the default loggin middleware will not be enabled.

Then, you can enable your own logging middleware without pollution by the default logger (named CommonLogger).

You can also add #\ --quiet to the top of your config.ru file to avoir always typing --quiet, but this behaviour is now deprecated.

like image 1
Qortex Avatar answered Nov 10 '22 14:11

Qortex