Class: RightSupport::Rack::CustomLogger

Inherits:
Object
  • Object
show all
Defined in:
lib/right_support/rack/custom_logger.rb

Overview

A Rack middleware that allows an arbitrary object to be used as the Rack logger. This is more flexible than Rack’s built-in Logger middleware, which always logs to a file-based Logger and doesn’t allow you to control anything other than the filename.

Instance Method Summary collapse

Constructor Details

#initialize(app, level = ::Logger::INFO, logger = nil) ⇒ CustomLogger

Initialize an instance of the middleware.

Parameters

app(Object)

the inner application or middleware layer; must respond to #call

level(Integer)

one of the Logger constants: DEBUG, INFO, WARN, ERROR, FATAL

logger(Logger)

(optional) the Logger object to use, if other than default



38
39
40
41
42
43
44
# File 'lib/right_support/rack/custom_logger.rb', line 38

def initialize(app, level = ::Logger::INFO, logger = nil)
  @app, @level = app, level

  logger ||= ::Logger.new(env['rack.errors'])
  logger.level = @level
  @logger = logger
end

Instance Method Details

#call(env) ⇒ Object

Add a logger to the Rack environment and call the next middleware.

Parameters

env(Hash)

the Rack environment

Return

always returns whatever value is returned by the next layer of middleware



53
54
55
56
# File 'lib/right_support/rack/custom_logger.rb', line 53

def call(env)
  env['rack.logger'] = @logger
  return @app.call(env)
end