1

I am implementing an Interface which holds a logging routine. Therefore i want to "cache" the logger inside of the interface.

It looks like this.

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.function.Supplier;

interface ILogger {


    public static final Logger logger = LoggerFactory.getLogger(this.getClass().getName());


    default void debug() {
        // do sth with logger
    }
}

But i can't use this in a static method.

How can i store my logger in field, so i do not have to look it up everytime i use the debug method?

1 Answer 1

4

Use a static class reference to get the name:

interface ILogger {
    Logger logger = LoggerFactory.getLogger(ILogger.class.getName());

    default void debug(String message) {
        logger.debug(message);
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Isn't that public static final?
@JinKwon public static final is the default for fields. See stackoverflow.com/questions/31144210/…
That's not what I'm actually asking. Thanks. I'm talking about exposing the field as public static final via interface.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.