Skip to content

Commit

Permalink
Merge pull request #67 from github/combined-regex
Browse files Browse the repository at this point in the history
Combined regex
  • Loading branch information
GrantBirki authored Jul 23, 2024
2 parents 9b0d4eb + 1666fc9 commit 49c53e0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 21 deletions.
30 changes: 13 additions & 17 deletions lib/redacting_logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
2 changes: 1 addition & 1 deletion lib/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module RedactingLogger
module Version
VERSION = "1.3.2"
VERSION = "1.4.0"
end
end
11 changes: 8 additions & 3 deletions spec/lib/redacting_logger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,23 @@
)

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

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

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
Expand All @@ -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",
Expand Down

0 comments on commit 49c53e0

Please sign in to comment.