We are already using lo4j, but because it is a dead project and sl4j is a good facade option for other logging frameworks, we want to switch to sl4j and log4j. We also plan to use sl4j + androidlog4j. Anyway, our current application calls the logger class with the following manner: (hundred of such calls are present)
// 5 parameters, 2 of them variable
CustomLogger.log("calculated result is ", nResult, " took ", nDuration, " sec");
or
// 2 parameters, one is variable
CustomLogger.log("the error code is ", nErrorCode, " after operation");
or
// 6 parameters, 3 variable
CustomLogger.log("the user path for file ", strFile, " generated under folder ", strFolder, " with the reuslt code ", nCode);
As you see, we always generate an array of objects, before sending the call to log4j we generated only a string (by generating a stringbuilder in a for loop [] of the array, huge performance problem even if logger is disabled, no lazy evaluation) as example:
class CustomLogger {
public static log(Object ...arry) {
StringBuilder sb = new StringBuilder();
for (Object o : arry) {
sb.append(o);
}
log4JLogger.log(sb.toString());
Now we have to bind the calls to a single sl4j call, but we have to convert to { } notation. Do we have to change each log call manually? For example:
CustomLogger.log("calculated result is ", nResult, " took ", nDuration, " sec");
should be converted to:
SL4JLogger.log("calculated result {} took {} sec", nResult, nDuration);
Is there any simpler solution you suggest instead of changing all underlying calls without performance loss? We use latest version of sl4j, log4j. The following call solves the problem partially, if limit the variable parameter count to 5, but we have always remaining {} in the output if the parameter count is less than 5:
// suppose we will have 5 variable args at maximum
SL4jLogger.log("{} {} {} {} {}", arry);
But when we have a call for example with 3 variables, we will have extra 2 {} {} in the output.