Sha256: 0a53c35ad02a8e5c87d2833fdb955cc6735636c251204529c43c3e4e6e1ddd34

Contents?: true

Size: 1.75 KB

Versions: 9

Compression:

Stored size: 1.75 KB

Contents

require 'logger'
require 'pp'

module ConvenientService
  module Dependencies
    module Extractions
      module RubyMiddleware
        ##
        # @internal
        #   NOTE:
        #     Copied from `Ibsciss/ruby-middleware` without any logic modification.
        #     Version: v0.4.2.
        #     - Wrapped in a namespace `ConvenientService::Dependencies::Extractions::RubyMiddleware`.
        #
        #   - https://github.com/marian13/ruby-middleware/blob/v0.4.2/lib/middleware/logger.rb
        #
        module Middleware

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

            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

            def next_middleware_name
              @app.class.name
            end

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

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

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

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

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
convenient_service-0.19.1 lib/convenient_service/dependencies/extractions/ruby_middleware/middleware/logger.rb
convenient_service-0.19.0 lib/convenient_service/dependencies/extractions/ruby_middleware/middleware/logger.rb
convenient_service-0.18.0 lib/convenient_service/dependencies/extractions/ruby_middleware/middleware/logger.rb
convenient_service-0.17.0 lib/convenient_service/dependencies/extractions/ruby_middleware/middleware/logger.rb
convenient_service-0.16.0 lib/convenient_service/dependencies/extractions/ruby_middleware/middleware/logger.rb
convenient_service-0.15.0 lib/convenient_service/dependencies/extractions/ruby_middleware/middleware/logger.rb
convenient_service-0.14.0 lib/convenient_service/dependencies/extractions/ruby_middleware/middleware/logger.rb
convenient_service-0.13.0 lib/convenient_service/dependencies/extractions/ruby_middleware/middleware/logger.rb
convenient_service-0.12.0 lib/convenient_service/dependencies/extractions/ruby_middleware/middleware/logger.rb