Skip to content
Browse files

Merge pull request #43 from patriciomacadden/logging

Add a recipe about Rack::CommonLogger
  • Loading branch information...
2 parents 35c519e + 18ed44c commit 0adda9611c2d9fb5c2bad236d557eb1b652ba3b6 @zzak zzak committed Mar 17, 2013
Showing with 45 additions and 0 deletions.
  1. +45 −0 middleware/rack_commonlogger.md
View
45 middleware/rack_commonlogger.md
@@ -0,0 +1,45 @@
+## Rack::CommonLogger
+
+Sinatra has [logging support](http://www.sinatrarb.com/intro.html#Logging), but
+it's [nearly impossible to log to a file and to the stdout](https://github.com/sinatra/sinatra/issues/484)
+(like Rails does).
+
+However, there is a little trick you can use to log to stdout *and* to a file:
+
+```ruby
+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:
+
+```ruby
+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
+```

0 comments on commit 0adda96

Please sign in to comment.
Something went wrong with that request. Please try again.