Skip to content

Instantly share code, notes, and snippets.

@copiousfreetime
Created February 16, 2009 17:02
Show Gist options
  • Save copiousfreetime/65249 to your computer and use it in GitHub Desktop.
Save copiousfreetime/65249 to your computer and use it in GitHub Desktop.
capture per test logging with logging and rspec
require 'rubygems'
require 'spec'
require 'stringio'
require 'logging'
Logging::Logger['root'].level = :all
module Spec
module Log
def self.io
@io ||= StringIO.new
end
def self.appender
@appender ||= Logging::Appenders::IO.new( "speclog", io )
end
Logging::Logger['root'].add_appenders( Log.appender )
def self.layout
@layout ||= Logging::Layouts::Pattern.new(
:pattern => "[%d] %5l %6p %c : %m\n",
:date_pattern => "%Y-%m-%d %H:%M:%S"
)
end
Log.appender.layout = layout
end
module Helpers
# the logging output from the test, if that class has any logging
def spec_log
Log.io.string
end
end
end
Spec::Runner.configure do |config|
include Spec::Helpers
config.before do
Spec::Log.io.rewind
Spec::Log.io.truncate( 0 )
end
config.after do
Spec::Log.io.rewind
Spec::Log.io.truncate( 0 )
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment