Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>Rack::CommonLogger</code> won't provide a logger to your main app, it will just logs the request like Apache would do.</p> <p>Check the code by yourself: <a href="https://github.com/rack/rack/blob/master/lib/rack/common_logger.rb" rel="nofollow noreferrer">https://github.com/rack/rack/blob/master/lib/rack/common_logger.rb</a></p> <p>All <code>Rack</code> 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:</p> <pre><code>def call(env) began_at = Time.now status, header, body = @app.call(env) header = Utils::HeaderHash.new(header) log(env, status, header, began_at) [status, header, body] end </code></pre> <p>The <code>@app</code> 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.</p> <p>The <code>logger</code> method goes like: </p> <pre><code>def log(env, status, header, began_at) now = Time.now length = extract_content_length(header) logger = @logger || env['rack.errors'] logger.write FORMAT % [ env['HTTP_X_FORWARDED_FOR'] || env["REMOTE_ADDR"] || "-", env["REMOTE_USER"] || "-", now.strftime("%d/%b/%Y %H:%M:%S"), env["REQUEST_METHOD"], env["PATH_INFO"], env["QUERY_STRING"].empty? ? "" : "?"+env["QUERY_STRING"], env["HTTP_VERSION"], status.to_s[0..3], length, now - began_at ] end </code></pre> <p>As you can tell, the <code>log</code> 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 the <code>rack.errors</code> logger (it seems there is one by default)</p> <p>The way to use it (in your <code>config.ru</code>):</p> <pre><code>logger = Logger.new('log/app.log') use Rack::CommonLogger, logger run YourApp </code></pre> <p>If you want to have a common logger in all your app, you could create a simple logger middleware:</p> <pre><code>class MyLoggerMiddleware def initialize(app, logger) @app, @logger = app, logger end def call(env) env['mylogger'] = @logger @app.call(env) end end </code></pre> <p>To use it, on your <code>config.ru</code>:</p> <pre><code>logger = Logger.new('log/app.log') use Rack::CommonLogger, logger use MyLoggerMiddleware, logger run MyApp </code></pre> <p>Hope this helps.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload