Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the log level in Sinatra

Tags:

ruby

sinatra

I am using this code to enable logging in my Sinatra app:

log_file = File.new('my_log_file.log', "a") 
$stdout.reopen(log_file)
$stderr.reopen(log_file)    
$stdout.sync=true
$stderr.sync=true

The actual logging is done using:

logger.debug("Starting call. Params = #{params.inspect}")

It turns out that only INFO or higher level log messages are logged and DEBUG messages are not logged. I am looking for a way to set up the log level to DEBUG.

like image 217
randomuser Avatar asked Dec 08 '11 10:12

randomuser


1 Answers

You can set the log level using

configure :development do
  set :logging, Logger::DEBUG
end

Sinatra sets you up with Rack::Logger in its default middleware, which can be initialized with a log level (see http://rack.rubyforge.org/doc/Rack/Logger.html). Sinatra initializes it with your logging setting, so you can put a number (or Logger constant) there instead of just true.

FYI, here is the relevant method from the Sinatra::Base source code that initializes the Rack::Logger middleware (found here)

def setup_custom_logger(builder)
  if logging.respond_to? :to_int
    builder.use Rack::Logger, logging
  else
    builder.use Rack::Logger
  end
end

This is on Sinatra 1.3.2, I don't know if it was different in earlier versions

like image 118
David Faber Avatar answered Oct 11 '22 20:10

David Faber