Here you can find the source of getLoggerInstance(String callerCLass)
Parameter | Description |
---|---|
callerCLass | a parameter |
protected final static Logger getLoggerInstance(String callerCLass)
//package com.java2s; //License from project: Open Source License import java.util.HashMap; import java.util.Map; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class Main { /**/*w w w .jav a 2 s . c o m*/ * Local cache to hold logger instances instead of Factory call to get rid of additional conditional checks */ private static final Map<String, Logger> LOGGER_INSTANCES = new HashMap<String, Logger>(); /** * Local caches to check if related logging level activated<br/> * @see http://logging.apache.org/log4j/2.x/performance.html */ private static boolean isTraceEnabled = false; private static boolean isDebugEnabled = false; private static boolean isInfoEnabled = false; private static boolean isWarnEnabled = false; private static boolean isErrorEnabled = false; /** * Returns a new {@link Logger} instance for given class name.<br/> * If logger instance already exist in cache returns existing one * * @param callerCLass * @return */ protected final static Logger getLoggerInstance(String callerCLass) { // check if we have logger already if (LOGGER_INSTANCES.containsKey(callerCLass)) { return LOGGER_INSTANCES.get(callerCLass); } // if we reach here we dont have yet, so create one Logger logger = LoggerFactory.getLogger(callerCLass); // predefine log levels isTraceEnabled = logger.isTraceEnabled(); isDebugEnabled = logger.isDebugEnabled(); isInfoEnabled = logger.isInfoEnabled(); isWarnEnabled = logger.isWarnEnabled(); isErrorEnabled = logger.isErrorEnabled(); // add into cache LOGGER_INSTANCES.put(callerCLass, logger); // return return logger; } /** * Get logger instance for the caller class * @return {@link Logger} */ public static final Logger getLogger() { return getLoggerInstance(getCallerClassName()); } /** * Returns caller class name using reflections/stacktrace * * @return {@link String} className */ protected final static String getCallerClassName() { return Thread.currentThread().getStackTrace()[3].getClassName(); } }