When I am developing complex logic across multiple classes, I often log entities (often with toString()
underneath) to check the status of the object, which field has what value. But I always find logs unnecessarily verbose, printing all fields. It is hard to track what has been changed and what has not when an entity goes a long way through a bunch of classes.
I would like to see only “tainted” fields(the fields which are already changed/set before reaching the logging line of code). I imagine a solution like this:
- set a value to a field
- add this field to a map, for example
taintedFields
; key is field name and value is actual value. Ideally, do this in every setter. Better yet, an abstract setter class template in a base class for all value classes. All model classes/DTOs should inherit it. - when printing, only print this map; other null/empty values are ignored
The idea is not hard to implement, but I wonder is this practical? Is it good practice? Is there any existing solution? I cannot be the first person ever to think about this. Any better way to do it?