Class: Deas::BaseLogging

Inherits:
Object
  • Object
show all
Defined in:
lib/deas/logging.rb

Direct Known Subclasses

SummaryLogging, VerboseLogging

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ BaseLogging

Returns a new instance of BaseLogging.



14
15
16
17
# File 'lib/deas/logging.rb', line 14

def initialize(app)
  @app    = app
  @logger = @app.settings.logger
end

Instance Method Details

#call(env) ⇒ Object

The Rack call interface. The receiver acts as a prototype and runs each request in a clone object unless the rack.run_once variable is set in the environment. Ripped from: github.com/rtomayko/rack-cache/blob/master/lib/rack/cache/context.rb



23
24
25
26
27
28
29
# File 'lib/deas/logging.rb', line 23

def call(env)
  if env['rack.run_once']
    call! env
  else
    clone.call! env
  end
end

#call!(env) ⇒ Object

The real Rack call interface. This is the common behavior for both the verbose and summary logging middlewares. It sets rack’s logger, times the response and returns it as is.



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/deas/logging.rb', line 34

def call!(env)
  env['rack.logger'] = @logger

  status, headers, body = nil, nil, nil
  benchmark = Benchmark.measure do
    status, headers, body = @app.call(env)
  end
  log_error(env['sinatra.error'])
  env['deas.time_taken'] = RoundedTime.new(benchmark.real)

  [ status, headers, body ]
end

#log(message) ⇒ Object



47
48
49
# File 'lib/deas/logging.rb', line 47

def log(message)
  @logger.info "[Deas] #{message}"
end

#log_error(exception) ⇒ Object



51
52
53
54
55
# File 'lib/deas/logging.rb', line 51

def log_error(exception)
  return if !exception || exception.kind_of?(Sinatra::NotFound)
  log "#{exception.class}: #{exception.message}\n" \
      "#{exception.backtrace.join("\n")}"
end