slf4j icon indicating copy to clipboard operation
slf4j copied to clipboard

SLF4J-154 Inferring logger from calling class.

Open thorntonrp opened this issue 8 years ago • 5 comments

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

thorntonrp avatar Dec 17 '16 09:12 thorntonrp

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.

huxi avatar Dec 17 '16 11:12 huxi

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);
    }

thorntonrp avatar Dec 18 '16 05:12 thorntonrp

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);
    }

thomasmueller avatar Dec 21 '16 10:12 thomasmueller

What's the difference between this pull request and https://github.com/qos-ch/slf4j/pull/29 ?

adorokhine avatar Dec 24 '16 05:12 adorokhine

It looks like this PR has the same goal as #29, however, it appears that one has conflicts that need to be resolved.

thorntonrp avatar Dec 28 '16 17:12 thorntonrp