The proper solution is to use slf4j/logback, which support passing in raw data without up-front string generation. With Commons Logging this can't be done and performance won't improve if you replace +
with StringBuilder
since string concatenation expressions are already compiled into code that involves a StringBuilder
.
isInfoEnabled () for Commons Logging
-
15-01-2022 - |
Question
I am using logger.info("statement" + string)
in lot of places in my applications including for loops and if statements.
I checked and found out that the string message get created first even when "INFO
" level is not enabled.
How to check if INFO
is enabled before the string gets created without adding the isInfoEnabled()
at each place where I implemented it.
Also, what's the best way to reduce overheads while using logger.
Instead of +
how would it be better if we use a utility method like returning a concatenated string using a Stringbuilder
.
I am using log4j.xml
for configuring the logger lever.
Solution
OTHER TIPS
I'd recommend you to create Utility call for that, your own architecture over Logger
something like this (pseudocode, I don't know about Logger implementation)
class OwnLogger extends Logger {
public static function info(Object o){
if(isInfoEnabled()){
super.info(o);
}
}
}