0

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 ?

1 Answers1

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