-
Notifications
You must be signed in to change notification settings - Fork 509
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 #473 from bkeepers/log-everything
Log all changes to ENV
- Loading branch information
Showing
9 changed files
with
241 additions
and
15 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
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
module Dotenv | ||
# Compare two hashes and return the differences | ||
class Diff | ||
attr_reader :a, :b | ||
|
||
def initialize(a, b) | ||
@a, @b = a, b | ||
end | ||
|
||
# Return a Hash of keys added with their new values | ||
def added | ||
@added ||= b.slice(*(b.keys - a.keys)) | ||
end | ||
|
||
# Returns a Hash of keys removed with their previous values | ||
def removed | ||
@removed ||= a.slice(*(a.keys - b.keys)) | ||
end | ||
|
||
# Returns of Hash of keys changed with an array of their previous and new values | ||
def changed | ||
@changed ||= (b.slice(*a.keys).to_a - a.to_a).map do |(k, v)| | ||
[k, [a[k], v]] | ||
end.to_h | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
require "active_support/log_subscriber" | ||
|
||
module Dotenv | ||
class LogSubscriber < ActiveSupport::LogSubscriber | ||
attach_to :dotenv | ||
|
||
def logger | ||
Dotenv::Rails.logger | ||
end | ||
|
||
def load(event) | ||
diff = event.payload[:diff] | ||
env = event.payload[:env] | ||
|
||
# Only show the keys that were added or changed | ||
changed = env.slice(*(diff.added.keys + diff.changed.keys)).keys.map { |key| color_var(key) } | ||
|
||
info "Set #{changed.to_sentence} from #{color_filename(env.filename)}" if changed.any? | ||
end | ||
|
||
def save(event) | ||
info "Saved a snapshot of #{color_env_constant}" | ||
end | ||
|
||
def restore(event) | ||
diff = event.payload[:diff] | ||
|
||
removed = diff.removed.keys.map { |key| color(key, :RED) } | ||
restored = (diff.changed.keys + diff.added.keys).map { |key| color_var(key) } | ||
|
||
if removed.any? || restored.any? | ||
info "Restored snapshot of #{color_env_constant}" | ||
debug "Unset #{removed.to_sentence}" if removed.any? | ||
debug "Restored #{restored.to_sentence}" if restored.any? | ||
end | ||
end | ||
|
||
private | ||
|
||
def color_filename(filename) | ||
color(Pathname.new(filename).relative_path_from(Dotenv::Rails.root.to_s).to_s, :YELLOW) | ||
end | ||
|
||
def color_var(name) | ||
color(name, :CYAN) | ||
end | ||
|
||
def color_env_constant | ||
color("ENV", :GREEN) | ||
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
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
module Dotenv | ||
# A logger that can be used before the apps real logger is initialized. | ||
class ReplayLogger | ||
def initialize | ||
@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 | ||
end | ||
|
||
def replay(logger) | ||
@logs.each { |name, args, block| logger.send(name, *args, &block) } | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
require "spec_helper" | ||
|
||
describe Dotenv::Diff do | ||
let(:before) { {} } | ||
let(:after) { {} } | ||
subject { Dotenv::Diff.new(before, after) } | ||
|
||
context "no changes" do | ||
let(:before) { {"A" => 1} } | ||
let(:after) { {"A" => 1} } | ||
|
||
it { expect(subject.added).to eq({}) } | ||
it { expect(subject.removed).to eq({}) } | ||
it { expect(subject.changed).to eq({}) } | ||
end | ||
|
||
context "key added" do | ||
let(:after) { {"A" => 1} } | ||
|
||
it { expect(subject.added).to eq("A" => 1) } | ||
it { expect(subject.removed).to eq({}) } | ||
it { expect(subject.changed).to eq({}) } | ||
end | ||
|
||
context "key removed" do | ||
let(:before) { {"A" => 1} } | ||
|
||
it { expect(subject.added).to eq({}) } | ||
it { expect(subject.removed).to eq("A" => 1) } | ||
it { expect(subject.changed).to eq({}) } | ||
end | ||
|
||
context "key changed" do | ||
let(:before) { {"A" => 1} } | ||
let(:after) { {"A" => 2} } | ||
|
||
it { expect(subject.added).to eq({}) } | ||
it { expect(subject.removed).to eq({}) } | ||
it { expect(subject.changed).to eq("A" => [1, 2]) } | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
require "spec_helper" | ||
require "active_support/all" | ||
require "rails" | ||
require "dotenv/rails" | ||
|
||
describe Dotenv::LogSubscriber do | ||
let(:logs) { StringIO.new } | ||
|
||
before do | ||
Dotenv.instrumenter = ActiveSupport::Notifications | ||
Dotenv::Rails.logger = Logger.new(logs) | ||
end | ||
|
||
context "set" do | ||
it "logs when a new instance variable is set" do | ||
Dotenv.load(fixture_path("plain.env")) | ||
expect(logs.string).to match(/Set.*PLAIN.*from.*plain.env/) | ||
end | ||
|
||
it "logs when an instance variable is overwritten" do | ||
ENV["PLAIN"] = "nope" | ||
Dotenv.load(fixture_path("plain.env"), overwrite: true) | ||
expect(logs.string).to match(/Set.*PLAIN.*from.*plain.env/) | ||
end | ||
|
||
it "does not log when an instance variable is not overwritten" do | ||
# load everything once and clear the logs | ||
Dotenv.load(fixture_path("plain.env")) | ||
logs.truncate(0) | ||
|
||
# load again | ||
Dotenv.load(fixture_path("plain.env")) | ||
expect(logs.string).not_to match(/Set.*plain.env/i) | ||
end | ||
|
||
it "does not log when an instance variable is unchanged" do | ||
ENV["PLAIN"] = "true" | ||
Dotenv.load(fixture_path("plain.env"), overwrite: true) | ||
expect(logs.string).not_to match(/PLAIN/) | ||
end | ||
end | ||
|
||
context "save" do | ||
it "logs when a snapshot is saved" do | ||
Dotenv.save | ||
expect(logs.string).to match(/Saved/) | ||
end | ||
end | ||
|
||
context "restore" do | ||
it "logs restored keys" do | ||
previous_value = ENV["PWD"] | ||
ENV["PWD"] = "/tmp" | ||
Dotenv.restore | ||
|
||
expect(logs.string).to match(/Restored.*PWD/) | ||
|
||
# Does not log value | ||
expect(logs.string).not_to include(previous_value) | ||
end | ||
|
||
it "logs unset keys" do | ||
ENV["DOTENV_TEST"] = "LogSubscriber" | ||
Dotenv.restore | ||
expect(logs.string).to match(/Unset.*DOTENV_TEST/) | ||
end | ||
|
||
it "does not log if no keys unset or restored" do | ||
Dotenv.restore | ||
expect(logs.string).not_to match(/Restored|Unset/) | ||
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
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