Here's a code snippet from my log library.

gegroet,
Erik V. - http://www.erikveen.dds.nl/

----------------------------------------------------------------

 ["$stdout", "$stderr"].each do |std|
   io           = eval(std)
   old_write    = io.method(:write)

   class << io
     self
   end.module_eval do
     define_method(:write) do |text|
       unless text =~ /^[\r\n]+$/       # Because puts calls twice.
         File.open("logfile.log", "a") do |f|
           f.puts [std[1..-1].upcase, caller[2], text].join(" ")
         end
       end

       old_write.call(text)
     end
   end
 end

 $stdout.puts "text on stdout"
 $stderr.puts "text on stderr"

----------------------------------------------------------------