Skip to content

Instantly share code, notes, and snippets.

@JoshAshby
Created April 29, 2016 00:49
Show Gist options
  • Save JoshAshby/69f750fef1e8b02622144e1932b17555 to your computer and use it in GitHub Desktop.
Save JoshAshby/69f750fef1e8b02622144e1932b17555 to your computer and use it in GitHub Desktop.
class MylarLogger
attr_accessor :logger, :log, :saved_lines
def initialize logger, saved_lines: 20
@logger, @log, @saved_lines = logger, [], saved_lines
end
def method_missing name, *args, &block
@log.push({ method: name, args: args })
@log.shift if @log.length > @saved_lines
@logger.send name, *args, &block
end
def has_logged? regex
regex = Regexp.compile(Regexp.quote(regex)) if regex.kind_of? String
regex =~ @log.map{ |e| e[:args] }.flatten.join(" ")
end
def respond_to_missing? name, include_all=false
@logger.respond_to? name, include_all
end
end
module MylarLoggerHelper
def assert_logged regex
yield if block_given?
assert Rails.logger.has_logged?(regex), <<STRING
Expected:
#{ regex }
To have been logged. Only logged:
#{ Rails.logger.log.join("\n") }
STRING
end
end
Rails.logger = MylarLogger.new Rails.logger
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment