slf4j
slf4j copied to clipboard
SLF4J-154 Inferring logger from calling class.
Added an operation to get logger inferred from the calling class. The implementation uses the same utility that is used to detect a logger name mismatch, resulting in no extra overhead.
Tests included.
See http://jira.qos.ch/browse/SLF4J-154
You should replace
String className = callingClass.getName().split("\\$")[0];
with
String className = callingClass.getName();
int dollarIndex = className.indexOf('$');
if(dollarIndex > -1) {
className = className.substring(0, dollarIndex);
}
It's way faster.
This is the final version as of 363afac:
public static Logger getLogger() { // SLF4J-154
Class<?> autoComputedCallingClass = Util.getCallingClass();
if (autoComputedCallingClass != null) {
return getLogger(Util.getCallingClass().getName());
}
Util.report("Failed to detect logger name from caller.");
return getLogger(Logger.ROOT_LOGGER_NAME);
}
why not reuse the variable?
I would say, because of the custom warning "Failed to detect logger name from caller.". But getCallingClass() should only be called once (untested):
public static Logger getLogger() { // SLF4J-154
String name;
Class<?> autoComputedCallingClass = Util.getCallingClass();
if (autoComputedCallingClass == null) {
Util.report("Failed to detect logger name from caller.");
name = Logger.ROOT_LOGGER_NAME;
} else {
name = autoComputedCallingClass.getName();
}
return getLogger(name);
}
What's the difference between this pull request and https://github.com/qos-ch/slf4j/pull/29 ?
It looks like this PR has the same goal as #29, however, it appears that one has conflicts that need to be resolved.