slf4j without toString() -


when log.debug("exported {}.", product) in slf4j call tostring() on arguments, e.g. product.

for reasons, can't override tostring() on classes want use arguments. classes come third party jars, others have tostring() called in other contexts, too, information want print in log statement isn't available.

however, have class debugging purposes has method debugformatter.format(object) has long cascade of instanceofs selects routine find useful debugging information object.

my question is: possible configure slf4j calls such static method instead of tostring()?

of course, call format method on object before passing parameter logger.debug() executed when respective logger not enabled. had surround if (log.isdebugenabled()) means whole point of having arguments in debug() missed.

you create shim, taking object , calling debugformatter.format() tostring() function. this:

class debugformatobject {   private final object o;    public static debugformatobject fordebug(object o) {     return new debugformatobject(o);   }    private debugformatobject(object o) {     this.o = o;   }    @override   public string tostring() {     return debugformatter.format(o);   } } 

with appropriate static import, logging statement becomes this:

log.debug("exported {}.", fordebug(product)); 

this have more overhead passing object in straight, it's small, constant overhead -- , object created short-lived.


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -