<commit_before>module Sinatra
  module RequestLogger
    def self.registered(app)
      app.use RequestLogger, app.logger
      app.set :show_exceptions, false
      app.error do
        settings.logger.info("#{env['sinatra.error'].class} (#{env['sinatra.error'].message})")
        settings.logger.info(env['sinatra.error'].backtrace.join("\n"))
      end
    end

    class RequestLogger
      # Common Log Format: http://httpd.apache.org/docs/1.3/logs.html#common
      # lilith.local - - [07/Aug/2006 23:58:02] "GET / HTTP/1.1" 500 -
      # %{%s - %s [%s] "%s %s%s %s" %d %s\n} %
      FORMAT = %{%s - %s [%s] "%s %s%s %s" %d %s %0.4f}

      def initialize(app, logger=nil)
        @app = app
        @logger = logger
      end

      def call(env)
        began_at = Time.now
        status, header, body = @app.call(env)
        header = Rack::Utils::HeaderHash.new(header)
        log(env, status, header, began_at)
        [status, header, body]
      end

      private

      def log(env, status, header, began_at)
        now = Time.now
        length = extract_content_length(header)

        logger = @logger || env['rack.errors']
        logger.info FORMAT % [
          env['HTTP_X_FORWARDED_FOR'] || env["REMOTE_ADDR"] || "-",
          env["REMOTE_USER"] || "-",
          now.strftime("%d/%b/%Y %H:%M:%S"),
          env["REQUEST_METHOD"],
          env["PATH_INFO"],
          env["QUERY_STRING"].empty? ? "" : "?"+env["QUERY_STRING"],
          env["HTTP_VERSION"],
          status.to_s[0..3],
          length,
          now - began_at ]
      end

      def extract_content_length(headers)
        value = headers['Content-Length'] or return '-'
        value.to_s == '0' ? '-' : value
      end
    end
  end
end<commit_msg>All refactgoring. Better using of rack. Add CommonLogger rackmiddleware<commit_after>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)
        @logger = logger
      end

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

      def flush
        #nothing
      end

      def 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
{'fraction_non_alphanumeric': 0.13810375670840788, 'fraction_numerical': 0.008586762075134168, 'mean_word_length': 2.0064516129032257, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '217710', 'n_tokens_mistral': 941, 'n_tokens_neox': 905, 'n_words': 194}