Last active
November 28, 2018 15:31
-
-
Save ryanwood/41c55feba11b41511a577e94ce2dda16 to your computer and use it in GitHub Desktop.
MultiLogger
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
# So we can log to STDOUT and a file at once. ;) | |
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 |
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
require 'multi_io' | |
def logger | |
@logger ||= begin | |
log_file = File.open("my_log_file.log", "a") | |
target = MultiIO.new(STDOUT, log_file) | |
Logger.new(target) | |
end | |
end | |
# Use | |
logger.debug("Would have run: #{sql}") | |
logger.info('Checkpoint!') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment