For logging purposes, instead of using toString()
method, Jackson's writeValueAsString(object)
method has been used in the project I have been working for.
LOGGER.info(mapper.writeValueAsString(object));
Now, I got the requirement to mask the sensitive information like passwords and credit card numbers in logs. If toString()
is being used, I could have removed those sensitive data from the toString()
method. But in my case, I could not find such simple yet correct way of getting my problem solved. I am not in a situation where I can change the entire thing to use toString()
too.
I read that by using %replace
method, I can replace the data which I don't need to be logged in using a predefined pattern. But all the sensitive data that need to be masked wont follow a single pattern.
I tried by intercepting the log event, look for the particular information and mask them(Using a class which implements LogEventFactory
). Even though it is a working solution, I don't think that it is a good solution since looking for the data in big strings every time is gonna cost.
Is there any way that I haven't come across yet to get my problem resolved? Is the approach with %replace
is the way to go? If so, how?