How to change log level in Sinatra

I use this code to enable registration in my Sinatra application:

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

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 configure the log level for DEBUG.

+6
source share
3 answers

I think there might be a better way, but you can always do something like setting the log level in the before filter:

 before do logger.level = 0 end 
+6
source

You can set the log level with

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

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

FYI, here is the corresponding 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 in previous versions

+26
source

In my situation, the only way I could work with reliable work was as follows: (simplified example)

First I set the log and log folder as follows:

 require 'logger' configure do log_dir = "#{root}/log" Dir.mkdir(log_dir) unless Dir.exists?(log_dir) file = File.new("#{log_dir}/#{environment}.log", 'a+') file.sync = true use Rack::CommonLogger, file end 

Then in a separate environment configuration

 configure :test do set :logging, Logger::ERROR end configure :development do set :logging, Logger::DEBUG end configure :production do set :logging, Logger::INFO end 

It works.

+4
source

Source: https://habr.com/ru/post/903363/


All Articles