You can write a pseudo IO
class that will write to multiple IO
objects. Something like:
class MultiIO
def initialize(*targets)
@targets = targets
end
def write(*args)
@targets.each {|t| t.write(*args)}
end
def close
@targets.each(&:close)
end
end
Then set that as your log file:
log_file = File.open("log/debug.log", "a")
Logger.new MultiIO.new(STDOUT, log_file)
Every time Logger
calls puts
on your MultiIO
object, it will write to both STDOUT
and your log file.
Edit: I went ahead and figured out the rest of the interface. A log device must respond to write
and close
(not puts
). As long as MultiIO
responds to those and proxies them to the real IO objects, this should work.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…