require 'logger' module Rack class Escapee ESCAPED_TAG = /(<\w+>)/ ESCAPED_ENTITY = /(&\w+;)/ def initialize(app, options = {}) @app = app @options = { :logfile => false }.merge(options) if @options[:logfile] @logger = ::Logger.new(@options[:logfile]) else @logger = ::Logger.new(STDOUT) end @logger.datetime_format = "%Y-%m-%d %H:%M:%S" end def call(env) req = Request.new(env) path = req.path_info status, headers, response = @app.call(env) response_body = [] response.each do |part| if part =~ ESCAPED_TAG @logger.warn("rendering escaped tag: #{$1} - #{path}") elsif part =~ ESCAPED_ENTITY @logger.warn("rendering escaped entity: #{$1} - #{path}") end response_body << part end [status, headers, response_body] end end end