Java slf4j Logger warn(String message)

Here you can find the source of warn(String message)

Description

Prints given message in warn level by a logger
instance of caller class.

License

Open Source License

Parameter

Parameter Description
message String message

Declaration

public static final void warn(String message) 

Method Source Code


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

Related

  1. testLogError(String errorMessage, Exception ex)
  2. traceCall(Class objectClass)
  3. traceLog(final Logger log, final String logMethodName, String logMsg)
  4. warn(Logger log, Object... args)
  5. warn(String format, Object... arguments)
  6. warn(String msg)
  7. warn(String msg)
  8. warnOrDebug(Logger logger, String msg)
  9. wrapWithMDC(Runnable r)