diff --git a/lib/redacting_logger.rb b/lib/redacting_logger.rb index de3b640..9526f38 100644 --- a/lib/redacting_logger.rb +++ b/lib/redacting_logger.rb @@ -30,6 +30,8 @@ def initialize( @redact_patterns = redact_patterns @redacted_msg = redacted_msg @redact_patterns += Patterns::DEFAULT if use_default_patterns + + @redact_patterns = Regexp.union(@redact_patterns) end # Adds a message to the log. @@ -40,30 +42,24 @@ def initialize( def add(severity, message = nil, progname = nil) message, progname = yield if block_given? - @redact_patterns.each do |pattern| - case message - - when String, Symbol, Numeric - message = message.to_s.gsub(pattern, @redacted_msg) + case message - when Array - message = message.map do |m| - m.to_s.gsub(pattern, @redacted_msg) - end + when String, Symbol, Numeric + message = message.to_s.gsub(@redact_patterns, @redacted_msg) - when Hash - message = message.transform_values do |v| - v.to_s.gsub(pattern, @redacted_msg) - end + when Array + message = message.map do |m| + m.to_s.gsub(@redact_patterns, @redacted_msg) end - end - if progname - @redact_patterns.each do |pattern| - progname = progname.to_s.gsub(pattern, @redacted_msg) + when Hash + message = message.transform_values do |v| + v.to_s.gsub(@redact_patterns, @redacted_msg) end end + progname = progname.to_s.gsub(@redact_patterns, @redacted_msg) if progname + super end end diff --git a/lib/version.rb b/lib/version.rb index 3b357a5..19dc913 100644 --- a/lib/version.rb +++ b/lib/version.rb @@ -2,6 +2,6 @@ module RedactingLogger module Version - VERSION = "1.3.2" + VERSION = "1.4.0" end end diff --git a/spec/lib/redacting_logger_spec.rb b/spec/lib/redacting_logger_spec.rb index fc4b907..88294bb 100644 --- a/spec/lib/redacting_logger_spec.rb +++ b/spec/lib/redacting_logger_spec.rb @@ -19,7 +19,7 @@ ) expect(logger.level).to eq(level) - expect(logger.instance_variable_get(:@redact_patterns)).to eq(redact_patterns) + expect(logger.instance_variable_get(:@redact_patterns)).to eq(/secret|password/) expect(logger.instance_variable_get(:@logdev).dev).to eq($stdout) expect(logger.instance_variable_get(:@redacted_msg)).to eq("!!!REDACTED!!!") end @@ -27,7 +27,7 @@ it "ensures the class is initialized properly with default values" do logger = RedactingLogger.new(use_default_patterns: false) expect(logger.level).to eq(Logger::DEBUG) - expect(logger.instance_variable_get(:@redact_patterns)).to eq([]) + expect(logger.instance_variable_get(:@redact_patterns)).to eq(/(?!)/) expect(logger.instance_variable_get(:@logdev).dev).to eq($stdout) expect(logger.instance_variable_get(:@redacted_msg)).to eq("[REDACTED]") end @@ -35,7 +35,7 @@ it "ensures the class is initialized properly with default values and uses built-in patterns" do logger = RedactingLogger.new($stdout, use_default_patterns: true) expect(logger.level).to eq(Logger::DEBUG) - expect(logger.instance_variable_get(:@redact_patterns).length).to be > 0 + expect(logger.instance_variable_get(:@redact_patterns).to_s.length).to be > 0 expect(logger.instance_variable_get(:@logdev).dev).to eq($stdout) expect(logger.instance_variable_get(:@redacted_msg)).to eq("[REDACTED]") end @@ -46,6 +46,11 @@ let(:logger) { RedactingLogger.new(logdev, redact_patterns: [/secret/, /password/, /token_[A-Z]{5}/, /999999999/]) } [ + { + case: "does not redact anything", + message: "This is a non-sensitive message with no redacted patterns", + expected_message: "This is a non-sensitive message with no redacted patterns" + }, { case: "secret message", message: "This is a secret password",