Class: ConvenientService::Dependencies::Extractions::RubyMiddleware::Middleware::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/convenient_service/dependencies/extractions/ruby_middleware/middleware/logger.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, logger, name = nil) ⇒ Logger

Returns a new instance of Logger.



20
21
22
23
24
# File 'lib/convenient_service/dependencies/extractions/ruby_middleware/middleware/logger.rb', line 20

def initialize app, logger, name = nil
  @app = app
  @write_to = logger
  @middleware_name = name
end

Instance Method Details

#call(env) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/convenient_service/dependencies/extractions/ruby_middleware/middleware/logger.rb', line 26

def call env
  write(
      way_in_message(
          next_middleware_name, env
  ))

  time = Time.now

  @app.call(env).tap { |env|
    write(
        way_out_message(
            next_middleware_name, (Time.now - time) * 1000.0, env
    ))
  }
end

#next_middleware_nameObject



42
43
44
# File 'lib/convenient_service/dependencies/extractions/ruby_middleware/middleware/logger.rb', line 42

def next_middleware_name
  @app.class.name
end

#pretty_print(item) ⇒ Object



46
47
48
# File 'lib/convenient_service/dependencies/extractions/ruby_middleware/middleware/logger.rb', line 46

def pretty_print item
  ->(out){ PP.pp(item, out) }.('')
end

#way_in_message(name, env) ⇒ Object



50
51
52
# File 'lib/convenient_service/dependencies/extractions/ruby_middleware/middleware/logger.rb', line 50

def way_in_message name, env
  ' %s has been called with: %s' % [name, pretty_print(env)]
end

#way_out_message(name, time, value) ⇒ Object



54
55
56
# File 'lib/convenient_service/dependencies/extractions/ruby_middleware/middleware/logger.rb', line 54

def way_out_message name, time, value
  ' %s finished in %.0f ms and returned: %s' % [name, time, pretty_print(value)]
end

#write(msg) ⇒ Object



58
59
60
# File 'lib/convenient_service/dependencies/extractions/ruby_middleware/middleware/logger.rb', line 58

def write msg
  @write_to.add(::Logger::INFO, msg.slice(0, 255).strip!, @middleware_name)
end