Class: ThreeScale::Backend::Logging::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/3scale/backend/logging/middleware.rb,
lib/3scale/backend/logging/middleware/writer.rb,
lib/3scale/backend/logging/middleware/json_writer.rb,
lib/3scale/backend/logging/middleware/text_writer.rb

Defined Under Namespace

Modules: Writer Classes: JsonWriter, TextWriter, UnsupportedLoggerType

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, writers: DEFAULT_WRITERS) ⇒ Middleware

writers is an array of symbols. WRITERS contains the accepted values



22
23
24
25
# File 'lib/3scale/backend/logging/middleware.rb', line 22

def initialize(app, writers: DEFAULT_WRITERS)
  @app = app
  @writers = writers
end

Class Method Details

.writers(loggers) ⇒ Object

Returns the Writer instances that correspond to the loggers given. If no loggers are given, returns the default writers.



50
51
52
53
54
55
56
57
58
# File 'lib/3scale/backend/logging/middleware.rb', line 50

def self.writers(loggers)
  writers = Array(loggers).map do |logger|
    writer_class =  WRITERS[logger]
    raise UnsupportedLoggerType.new(logger) unless writer_class
    writer_class.new
  end

  writers.empty? ? DEFAULT_WRITERS : writers
end

Instance Method Details

#call(env) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/3scale/backend/logging/middleware.rb', line 27

def call(env)
  began_at = Time.now
  begin
    status, header, body = @app.call(env)
  rescue Exception => e
    @writers.each do |writer|
      writer.log_error(env, 500, e.message, began_at)
    end
    raise e
  end

  header = ::Rack::Utils::HeaderHash.new(header)
  body = ::Rack::BodyProxy.new(body) do
    @writers.each do |writer|
      writer.log(env, status, header, began_at)
    end
  end

  [status, header, body]
end