Lawrence I. Siden Lawrence I. Siden - 1 year ago 124
Ruby Question

Use Rack::CommonLogger in Sinatra

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 and, 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.

Answer Source

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:

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:

def call(env)
  began_at =
  status, header, body =
  header =
  log(env, status, header, began_at)
  [status, header, body]

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:

def log(env, status, header, began_at)
  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["QUERY_STRING"].empty? ? "" : "?"+env["QUERY_STRING"],
    now - began_at ]

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 the rack.errors logger (it seems there is one by default)

The way to use it (in your

logger ='log/app.log')

use Rack::CommonLogger, logger
run YourApp

If you want to have a common logger in all your app, you could create a simple logger middleware:

class MyLoggerMiddleware

  def initialize(app, logger)
    @app, @logger = app, logger

  def call(env)
    env['mylogger'] = @logger


To use it, on your

logger ='log/app.log')
use Rack::CommonLogger, logger
use MyLoggerMiddleware, logger
run MyApp

Hope this helps.