Created
April 30, 2013 15:15
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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