replay_logger.rb 553 B

1234567891011121314151617181920
  1. module Dotenv
  2. # A logger that can be used before the apps real logger is initialized.
  3. class ReplayLogger < Logger
  4. def initialize
  5. super(nil) # Doesn't matter what this is, it won't be used.
  6. @logs = []
  7. end
  8. # Override the add method to store logs so we can replay them to a real logger later.
  9. def add(*args, &block)
  10. @logs.push([args, block])
  11. end
  12. # Replay the store logs to a real logger.
  13. def replay(logger)
  14. @logs.each { |args, block| logger.add(*args, &block) }
  15. @logs.clear
  16. end
  17. end
  18. end