Last Updated: April 21, 2016
·
10.9K
· lukaszkorecki

Log to stdout and a file at the same time

Ruby's Logger class is really useful and handy - you should definitely use it instead of puts or STDOUT <<.

One limitation is that you can't log to two destinations at once. Or can you?


log = Logger.new("| tee test.log") # note the pipe ( '|' )
log.info "hi" # will log to both STDOUT and test.log

Handy if you want to track progress of a script while it is running and have a permanent (well...) record of that progress for later inspection.

3 Responses
Add your response

+1

over 1 year ago ·

It doesn't append the logfile btw, It always overrides... :(

over 1 year ago ·