Fork me on GitHub

Rack::CommonLogger

Sinatra has logging support, but it's nearly impossible to log to a file and to the stdout (like Rails does).

However, there is a little trick you can use to log to stdout and to a file:

require 'sinatra'

configure do
  # logging is enabled by default in classic style applications,
  # so `enable :logging` is not needed
  file = File.new("#{settings.root}/log/#{settings.environment}.log", 'a+')
  file.sync = true
  use Rack::CommonLogger, file
end

get '/' do
  'Hello World'
end

You can use the same configuration for modular style applications, but you have to enable :logging first:

require 'sinatra/base'

class SomeApp < Sinatra::Base
  configure do
    enable :logging
    file = File.new("#{settings.root}/log/#{settings.environment}.log", 'a+')
    file.sync = true
    use Rack::CommonLogger, file
  end

  get '/' do
    'Hello World'
  end

  run!
end
Top