Sha256: e573b9c30a4c524eef5880b3cf29cc4feba2efec1b361b8deb6986b143ed2361

Contents?: true

Size: 866 Bytes

Versions: 1

Compression:

Stored size: 866 Bytes

Contents

module Logput
  class Middleware
    def initialize(app, options = {})
      @app = app
      @path_to_log_file = options[:path_to_log_file]
      @lines_to_read = options[:lines_to_read] || 500
    end

    def call(env)
      @path_to_log_file ||= default_path_to_log_file(env)

      raise 'Log file does not exist' unless File.exists? @path_to_log_file

      if env['PATH_INFO'] == '/logput'
        out = `tail -n #{@lines_to_read} #{@path_to_log_file}`
        [200, {"Content-Type" => "text/html"}, ["<pre>", out, "</pre>"]]
      else
        @app.call(env)
      end
    end

    private

    def default_path_to_log_file(env)
      if defined? Rails
        env['action_dispatch.logger'].instance_variable_get(:@logger).instance_variable_get(:@log_dest).path
      else
        raise Exception, 'Must specify path to log file'
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
logput-0.0.1 lib/logput/middleware.rb