integrating sinatra log output with stdlib Logger

820 views
Skip to first unread message

Joel VanderWerf

unread,
Apr 2, 2009, 7:34:40 PM4/2/09
to sina...@googlegroups.com

Currently, I'm using the default setup, which seems to be the thin
webserver, and I'm redirecting stdout, stderr to files.

But I'd really rather use Logger and have control over file size,
rotation, level, etc.

Any suggestions...?

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Andre Lewis

unread,
Apr 6, 2009, 4:44:03 PM4/6/09
to sinatrarb
I use:

require "logger"

configure do
LOGGER = Logger.new(File.join('log', "#
{Sinatra::Application.environment}.log"))
...
end

get "/foo" do
LOGGER.info "log message!"
end

Joel VanderWerf

unread,
Apr 6, 2009, 11:53:10 PM4/6/09
to sina...@googlegroups.com

Thanks, but a couple of problems with that, for me:

The Sinatra::Application.environment expression returns a symbol with a
space in it:

:" production"

It still doesn't capture the web server log messages. I mean the entries
that look like this:

127.0.0.1 - - [06/Apr/2009 20:51:17] "GET
/speedinfo?lat=37.865571&lng=-122.303066 HTTP/1.1" 200 1251 0.0042

These go to stdout, which of course I can redirect, but I'd rather have
control over log size, rotation, etc.

Joel VanderWerf

unread,
Apr 7, 2009, 12:52:26 AM4/7/09
to sina...@googlegroups.com
Joel VanderWerf wrote:
>
> Thanks, but a couple of problems with that, for me:
>
> The Sinatra::Application.environment expression returns a symbol with a
> space in it:
>
> :" production"

sorry, that was a bug in my rakefile, not splitting #exec args enough.

> It still doesn't capture the web server log messages. I mean the entries
> that look like this:
>
> 127.0.0.1 - - [06/Apr/2009 20:51:17] "GET
> /speedinfo?lat=37.865571&lng=-122.303066 HTTP/1.1" 200 1251 0.0042
>
> These go to stdout, which of course I can redirect, but I'd rather have
> control over log size, rotation, etc.

Still can't find how to capture this. I guess that's more of a rack
question?

In rackcommonlogger.rb, it's possible to specify a logger when the
CommonLogger is instantiated:

class CommonLogger
def initialize(app, logger=nil)

but it's not clear how to specify this from a sinatra app.

Surely, someone has needed to do this before? Web server log messages
will otherwise fill some file to infinity....

candlerb

unread,
Apr 7, 2009, 6:25:32 AM4/7/09
to sinatrarb
> In rackcommonlogger.rb, it's possible to specify a logger when the
> CommonLogger is instantiated:
>
>    class CommonLogger
>      def initialize(app, logger=nil)
>
> but it's not clear how to specify this from a sinatra app.

Have you tried this?

set :logging, false
dest = File.open("/tmp/foo","w")
dest.sync = true
use Rack::CommonLogger, dst

Note that looking at the Rack::CommonLogger source, it expects the
target object to implement a "<<" method. I believe that logger.rb
does.

If CommonLogger doesn't do what you want, I suggest you just copy it
(it's very small), modify it appropriately, and insert it into your
middleware stack as above.

Joel VanderWerf

unread,
Apr 7, 2009, 12:32:39 PM4/7/09
to sina...@googlegroups.com

That's perfect, with the substitution of a Logger instance:

configure :production do
set :logging, false
LOGGER = Logger.new(...)
use Rack::CommonLogger, LOGGER
end

Thanks!

Chris Sepic

unread,
Apr 23, 2009, 6:40:12 PM4/23/09
to sinatrarb
I'd also like errors to be logged in the same log. They get written to
whatever env["rack.errors"] is pointing to, which is the Apache error
log by default. Is there a way to set 'rack.errors' in the rackup file
or somewhere? I ended up creating middleware to just set rack.errors,
which is pretty ridiculous.

Also, the above configure block doesn't seem to work, but things work
if they are in the rackup file.
Reply all
Reply to author
Forward
0 new messages