nicohvi
nicohvi

Reputation: 2270

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?

Upvotes: 7

Views: 2799

Answers (5)

Dishcandanty
Dishcandanty

Reputation: 410

This is one of the top results. So this probably more of a message to my future self the next time I'm annoyed to death about sinatra/puma not shutting up. But to actually get a silent start up:

class MyApp < Sinatra::Base
  configure do
    set :server, :puma
    set :quiet, true
    set :server_settings, Silent: true
  end
end

Upvotes: 0

Qortex
Qortex

Reputation: 7456

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.

Upvotes: 1

lzap
lzap

Reputation: 17174

It's probably not Sinatra what is writing to STDOUT or STDERR, but your webserver. Puma can be started with -q (quiet) option as noted by @matt. When using webrick make sure the AccessLog configuration variable is an empty array, otherwise it will also be logged on your standard output.

Disabling echo from webrick

Upvotes: 0

Jilles van Gurp
Jilles van Gurp

Reputation: 8284

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.

Upvotes: 9

matt
matt

Reputation: 79723

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.

Upvotes: 6

Related Questions