Skip to content

Instantly share code, notes, and snippets.

@Peeja
Created April 30, 2013 15:15
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Peeja/5489388 to your computer and use it in GitHub Desktop.
Save Peeja/5489388 to your computer and use it in GitHub Desktop.
TeeIO: A method for logging to a file and STDOUT at the same time. Or to any two or more IOs. Greatly inspired by James Edward Grey II.
class TeeIO
# TeeIO will write to each of these IOs when it is written to.
def initialize(*ios)
@ios = ios
end
def write(data)
@ios.each { |io| io.write(data) }
end
def close
@ios.each { |io| io.close }
end
end
log_filename = Rails.root.join('log', 'doinstuff.txt')
file = open(log_filename, 'w')
Rails.logger = Logger.new(TeeIO.new($stdout, file))
Rails.logger.info "It works!"
file.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment