Enabling Logging with Passenger and Sinatra
I wrote about how to deploy a Sinatra application on top of Passenger, with specific instructions for Dreamhost. As it turns out, the logging functionality of Sinatra didn’t work with the instructions, this is the workaround.
The technical details: The trick to this is that Sinatra’s logging uses Rack’s CommonLogger class. If you don’t tell it otherwise, it logs to whatever rack’s env[‘rack.error’] is set to, which is STDERR in this case. Sinatra doesn’t give an easy way to tell it to use a certain logger, but instead, always uses the defaults. The easiest way is to ignore that whole layer off annoyance, and just reopen STDERR and STDOUT to write to the file. That way, no change to Sinatra or your application is needed.
Just add three lines to your config.ru file:
# Add these 3 lines log = File.new("sinatra.log", "w") STDOUT.reopen(log) STDERR.reopen(log)
The config.ru file in it’s entirety:
require 'sinatra/lib/sinatra.rb' require 'rubygems' Sinatra::Application.default_options.merge!( :run => false, :env => :production ) # Add these 3 lines log = File.new("sinatra.log", "w") STDOUT.reopen(log) STDERR.reopen(log) require 'test.rb' run Sinatra.application
June 25th, 2008 at 5:42 am
sure that with :env => :production it will logs?
i tried on dreamhost, and doesnt’ works.
i put some errors in a controller, and it returns
Internal Server Error with empy log.
ciao
June 25th, 2008 at 6:35 am
We’re both right it seems. My solution works perfectly for me doing request logging (ie. who accessed what resources), but apparently doesn’t handle errors. I’ll look into it further later tonight.