-
Notifications
You must be signed in to change notification settings - Fork 508
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #492 from bkeepers/real-logger
Refactor ReplayLogger to extend Logger
- Loading branch information
Showing
2 changed files
with
23 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,20 @@ | ||
module Dotenv | ||
# A logger that can be used before the apps real logger is initialized. | ||
class ReplayLogger | ||
class ReplayLogger < Logger | ||
def initialize | ||
super(nil) # Doesn't matter what this is, it won't be used. | ||
@logs = [] | ||
end | ||
|
||
def method_missing(name, *args, &block) | ||
@logs.push([name, args, block]) | ||
end | ||
|
||
def respond_to_missing?(name, include_private = false) | ||
(include_private ? Logger.instance_methods : Logger.public_instance_methods).include?(name) || super | ||
# Override the add method to store logs so we can replay them to a real logger later. | ||
def add(*args, &block) | ||
@logs.push([args, block]) | ||
end | ||
|
||
# Replay the store logs to a real logger. | ||
def replay(logger) | ||
@logs.each { |name, args, block| logger.send(name, *args, &block) } | ||
@logs.each { |args, block| logger.add(*args, &block) } | ||
@logs.clear | ||
end | ||
end | ||
end |
This file contains 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