Sha256: 81b426004ecace549168be1ef29cd422096e19fb4576bdedbeb8257c102a7be6

Contents?: true

Size: 1.03 KB

Versions: 1

Compression:

Stored size: 1.03 KB

Contents

module Sinatra
  module RequestLogger
    def self.registered(app)
      app.set :proxy_logger, ProxyLogger.new(app.logger)
      app.use Rack::CommonLogger, app.proxy_logger
      app.use ErrorLogger, app.proxy_logger
    end

    class ProxyLogger
      def initialize(logger)
        @buffer = StringIO.new("")
        @logger = logger
      end

      def write(msg)
        @buffer.write(msg)
        @logger.info(msg)
      end

      def flush
        @buffer.flush
      end

      def puts(msg)
        @buffer.puts(msg)
        if msg.is_a?(Array)
          @logger.info(msg.map(&:chomp).join("\n"))
        elsif msg.is_a?(String)
          @logger.info(msg.chomp)
        else
          @logger.info(msg)
        end
      end

      def method_missing(method, *args)
        @logger.send(method, *args)
      end
    end

    class ErrorLogger
      def initialize(app, logger)
        @app = app
        @logger = logger
      end

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sinatra-request-logger-0.2.1 lib/sinatra/request-logger.rb