Reputation: 211
I need to mask the password before it gets displayed in the log file.
the format of the password is "password":"pswd123". it's alphanumeric only. After masking, it'd be "password":"*"
in my custom Pattern class, I've the following reg expression but it's not being picked up. any idea how it should be? thx
@Override
public String format(LoggingEvent event) {
String msg = super.format(event);
// regexp not being picked up
msg = msg.replace("\"password\":\"[^\"]*", "password:\"***\"");
return msg;
}
Upvotes: 2
Views: 954
Reputation: 2533
Are you making use of any logging library like log4j or slf4j? These libraries have features to "replace strings" using regular expressions. You can use and apply this globally by changing in logging configuration file. You would still need to come up with a regular expression for which use a regular expression builder utility such as http://myregexp.com/ and build a regular expression on your own.
Upvotes: 0
Reputation: 4766
If you want it to just show 3 * that's easy, if you want it to show 1 * for each character in the password, that's a little harder.
Msg = Regex.Replace(Msg, "\"Password\":\"[^\"]+?\"", "\"Password\":\"***\"")
Upvotes: 0
Reputation: 4164
Upvotes: 0
Reputation: 500227
String.replace()
takes a CharSequence
, not a regex. You're probably looking for replaceAll()
or replaceFirst()
.
Upvotes: 4