Sunday, March 24, 2013

Java: Bad protected log and no chance to fix it

This post is related to the final question asked in one of mine previous post Java: Bad protected final log. How to resolve the problem of a bad log declaration, like the one below, without editing the code?
    protected final Logger log = LoggerFactory.getLogger(this.getClass());

First of all you should use slf4j , bridge every log framework to slf4j, then use logback as implementation for logging.
Then simply add or edit your logback configuration introducing the caller class as parameter. For example if you have a logback.xml file like this:

    
        
            %-5level %logger - %msg %xEx%n
        
    

    
        
    


change it into:

    
        
            %-5level %class - %msg %xEx%n
        
    

    
        
    



Now the problem is solved but remember: you are paying with performance, finding the caller requires more computational time than printing logger's name.

Related posts:

No comments:

Post a Comment