|
| 1 | +package io.hops.metadata.ndb.wrapper; |
| 2 | +import com.mysql.clusterj.core.util.JDK14LoggerFactoryImpl; |
| 3 | +import com.mysql.clusterj.core.util.Logger; |
| 4 | +import com.mysql.clusterj.core.util.LoggerFactory; |
| 5 | + |
| 6 | +import java.util.HashMap; |
| 7 | +import java.util.Map; |
| 8 | + |
| 9 | +import org.apache.commons.logging.Log; |
| 10 | +import org.apache.commons.logging.LogFactory; |
| 11 | + |
| 12 | +public class HopsLoggerFactory implements LoggerFactory{ |
| 13 | + |
| 14 | + static final Map<String, Logger> loggerMap = new HashMap<String, Logger>(); |
| 15 | + |
| 16 | + public HopsLoggerFactory() { |
| 17 | + // create all the known loggers for the core project |
| 18 | + registerLogger(JDK14LoggerFactoryImpl.CLUSTERJ_LOGGER); |
| 19 | + registerLogger(JDK14LoggerFactoryImpl.CLUSTERJ_METADATA_LOGGER); |
| 20 | + registerLogger(JDK14LoggerFactoryImpl.CLUSTERJ_QUERY_LOGGER); |
| 21 | + registerLogger(JDK14LoggerFactoryImpl.CLUSTERJ_UTIL_LOGGER); |
| 22 | + } |
| 23 | + |
| 24 | + public Logger registerLogger(String loggerName) { |
| 25 | + final Log log = LogFactory.getLog(loggerName); |
| 26 | + Logger result = new HopsClusterJLogger(log); |
| 27 | + loggerMap.put(loggerName, result); |
| 28 | + return result; |
| 29 | + } |
| 30 | + |
| 31 | + @SuppressWarnings("unchecked") |
| 32 | + public Logger getInstance(Class cls) { |
| 33 | + String loggerName = getPackageName(cls); |
| 34 | + return getInstance(loggerName); |
| 35 | + } |
| 36 | + |
| 37 | + public synchronized Logger getInstance(String loggerName) { |
| 38 | + Logger result = loggerMap.get(loggerName); |
| 39 | + if (result == null) { |
| 40 | + result = registerLogger(loggerName); |
| 41 | + } |
| 42 | + return result; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Returns the package portion of the specified class. |
| 47 | + * @param cls the class from which to extract the |
| 48 | + * package |
| 49 | + * @return package portion of the specified class |
| 50 | + */ |
| 51 | + final private static String getPackageName(Class<?> cls) |
| 52 | + { |
| 53 | + String className = cls.getName(); |
| 54 | + int index = className.lastIndexOf('.'); |
| 55 | + return ((index != -1) ? className.substring(0, index) : ""); // NOI18N |
| 56 | + } |
| 57 | +} |
0 commit comments