Java slf4j Logger getLoggerInstance(String callerCLass)

Here you can find the source of getLoggerInstance(String callerCLass)

Description

Returns a new Logger instance for given class name.
If logger instance already exist in cache returns existing one

License

Open Source License

Parameter

Parameter Description
callerCLass a parameter

Declaration

protected final static Logger getLoggerInstance(String callerCLass) 

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

Related

  1. getLogger(Class clazz)
  2. getLogger(Class clazz)
  3. getLogger(Class clazz)
  4. getLogger(String className)
  5. getLoggerForCurrentClass()
  6. getLoggerLevel(Logger logger)
  7. getLong(Properties config, String key, long defaultValue)
  8. getMessage(String message, Object... args)
  9. getTraceLevel(Logger logger)