I have a small web-server that I wrote with Sinatra. I want to be able to log messages to a log file. I've read through http://www.sinatrarb.com/api/index.html and www.sinatrarb.com/intro.html, and I see that Rack has something called Rack::CommonLogger, but I can't find any examples of how it can be accessed and used to log messages. My app is simple so I wrote it as a top-level DSL, but I can switch to subclassing it from SinatraBase if that's part of what's required.
相关问题
- I want to trace logs using a Macro multi parameter
- How to specify memcache server to Rack::Session::M
- How to specify memcache server to Rack::Session::M
- Why am I getting a “C compiler cannot create execu
- reference to a method?
相关文章
- how do I log requests and responses for debugging
- Ruby using wrong version of openssl
- Difference between Thread#run and Thread#wakeup?
- how to call a active record named scope with a str
- “No explicit conversion of Symbol into String” for
- Segmentation fault with ruby 2.0.0p247 leading to
- How to detect if an element exists in Watir
- uninitialized constant Mysql2::Client::SECURE_CONN
Rack::CommonLogger
won't provide a logger to your main app, it will just logs the request like Apache would do.Check the code by yourself: https://github.com/rack/rack/blob/master/lib/rack/common_logger.rb
All
Rack
apps have the call method that get's invoked with the HTTP Request env, if you check the call method of this middleware this is what happens:The
@app
in this case is the main app, the middleware is just registering the time the request began at, then it class your middleware getting the [status, header, body] triple, and then invoke a private log method with those parameters, returning the same triple that your app returned in the first place.The
logger
method goes like:As you can tell, the
log
method just grabs some info from the request env, and logs in on a logger that is specified on the constructor call, if there is no logger instance then it goes to therack.errors
logger (it seems there is one by default)The way to use it (in your
config.ru
):If you want to have a common logger in all your app, you could create a simple logger middleware:
To use it, on your
config.ru
:Hope this helps.
I followed what I found on this blog post - excerpted below
then use
puts
orprint
. It worked for me.In your
config.ru
:Reopening STDOUT and redirecting it to a file is not a good idea if you use Passenger. It causes in my case that Passenger does not start. Read https://github.com/phusion/passenger/wiki/Debugging-application-startup-problems#stdout-redirection for this issue.
This would be the right way instead: