Logging in Sinatra?
Sinatra 1.3 will ship with such a logger object, exactly usable as above. You can use edge Sinatra as described in "The Bleeding Edge". Won't be that long until we'll release 1.3, I guess.
To use it with Sinatra 1.2, do something like this:
require 'sinatra'use Rack::Loggerhelpers do def logger request.logger endend
I personally log in Sinatra via:
require 'sinatra'require 'sequel'require 'logger'class MyApp < Sinatra::Application configure :production do set :haml, { :ugly=>true } set :clean_trace, true Dir.mkdir('logs') unless File.exist?('logs') $logger = Logger.new('logs/common.log','weekly') $logger.level = Logger::WARN # Spit stdout and stderr to a file during production # in case something goes wrong $stdout.reopen("logs/output.log", "w") $stdout.sync = true $stderr.reopen($stdout) end configure :development do $logger = Logger.new(STDOUT) endend# Log all DB commands that take more than 0.2sDB = Sequel.postgres 'mydb', user:'dbuser', password:'dbpass', host:'localhost'DB << "SET CLIENT_ENCODING TO 'UTF8';"DB.loggers << $logger if $loggerDB.log_warn_duration = 0.2
If you are using something like unicorn logging or other middleware that tails IO streams, you can easily set up a logger to STDOUT or STDERR
# unicorn.rbstderr_path "#{app_root}/shared/log/unicorn.stderr.log"stdout_path "#{app_root}/shared/log/unicorn.stdout.log"# sinatra_app.rbset :logger, Logger.new(STDOUT) # STDOUT & STDERR is captured by unicornlogger.info('some info') # also accessible as App.settings.logger
this allows you to intercept messages at application scope, rather than just having access to logger as request helper