简体   繁体   中英

Java “Cache” SLF4J Logger in Interface

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM