When the log is printed, I want to block the middle of the phone number and ID card. I don't want to change the toString method in each entity class.Is there some way to replace the phone number from a long String ?
Asked
Active
Viewed 126 times
0
-
2Possible duplicate of https://stackoverflow.com/questions/25277930/mask-sensitive-data-in-logs-with-logback – Mohit Mutha Nov 18 '19 at 09:01
-
What logging framework do you use? – keuleJ Nov 18 '19 at 09:20
-
We use the logback framework. – zhaohongxin Nov 18 '19 at 09:27
1 Answers
0
You might have some success by decorating the object you're logging with a wrapper that is aware of the sensitivities of various fields.
A very basic example...
class NumberHidingDecorator {
private final Object decorated;
public NumberHidingDecorator(Object decorated) {
this.decorated = decorated;
}
@Override
public String toString() {
return decorated.toString().replaceAll("(\d{2})\d+(\d{2})", "$1...$2");
}
}
And pass the decorated object to your logging facility
log.info("Invalid credit card {}", new NumberHidingDecorator(creditCard));

ptomli
- 11,730
- 4
- 40
- 68
-
Thank you , but I want do something in the logger class but not the place of logging, because there is a lot of entity need to be decorated. – zhaohongxin Nov 18 '19 at 09:19