Here you can find the source of warn(String message)
Parameter | Description |
---|---|
message | String message |
public static final void warn(String message)
//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 ww . j a v a 2s . co 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; /** * Prints warn message by given logger<br/> * Caller method name will be appended into starting of message * * @param logger {@link Logger} instance * @param message {@link String} message */ protected static final void warn(Logger logger, String message) { if (isWarnEnabled) { logger.warn(getCallerMethodName() + "() " + message); } } /** * Prints given message in warn level by a logger<br/> * instance of caller class. Caller class and method name will be<br/> * appended into message. Logger instance will be created by reflections<br/> * if not created in previous calls. * * @param message {@link String} message * */ public static final void warn(String message) { warn(getLoggerInstance(getCallerClassName()), message); } /** * Returns caller method name using reflections/stacktrace * * @return {@link String} methodname */ protected final static String getCallerMethodName() { return Thread.currentThread().getStackTrace()[4].getMethodName(); } /** * 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; } /** * Returns caller class name using reflections/stacktrace * * @return {@link String} className */ protected final static String getCallerClassName() { return Thread.currentThread().getStackTrace()[3].getClassName(); } /** * Get logger instance for the caller class * @return {@link Logger} */ public static final Logger getLogger() { return getLoggerInstance(getCallerClassName()); } }