Coverage Summary for Class: Log (net.sf.persism)

Class Class, % Method, % Line, %
Log 100% (1/1) 81.2% (13/16) 65% (26/40)


1 package net.sf.persism; 2  3  4 import net.sf.persism.logging.AbstractLogger; 5 import net.sf.persism.logging.LogMode; 6 import net.sf.persism.logging.implementation.JulLogger; 7 import net.sf.persism.logging.implementation.Log4j2Logger; 8 import net.sf.persism.logging.implementation.Log4jLogger; 9 import net.sf.persism.logging.implementation.Slf4jLogger; 10  11 import java.util.ArrayList; 12 import java.util.Arrays; 13 import java.util.List; 14 import java.util.Map; 15 import java.util.concurrent.ConcurrentHashMap; 16  17 /** 18  * Logging wrapper to avoid runtime dependencies. 19  * It will use slf4j if available or log4j if available otherwise it falls back to JUL (java.util.logging). 20  * <p/> 21  * JUL will use the default java logging.properties 22  * log4j will use log4j.properties 23  * slf4j will use logback.xml 24  * <p/> 25  * log4j DEBUG maps to JUL FINE 26  * log4j ERROR maps to JUL SEVERE 27  * <p/> 28  * The class includes an overloaded error method to log stack traces as well as the message. 29  * 30  * @author Dan Howard 31  * @since 4/21/12 6:47 AM 32  */ 33 final class Log { 34  35  private AbstractLogger logger; 36  37  private static final Map<String, Log> loggers = new ConcurrentHashMap<String, Log>(12); 38  39  private static final List<String> warnings = new ArrayList<>(32); 40  41  private Log() { 42  } 43  44  Log(String logName) { 45  try { 46  Class.forName("org.slf4j.Logger"); 47  logger = new Slf4jLogger(logName); 48  } catch (ClassNotFoundException e) { 49  try { 50  Class.forName("org.apache.log4j.Logger"); 51  logger = new Log4jLogger(logName); 52  } catch (ClassNotFoundException e1) { 53  try { 54  Class.forName("org.apache.logging.log4j.Logger"); 55  logger = new Log4j2Logger(logName); 56  } catch (ClassNotFoundException classNotFoundException) { 57  logger = new JulLogger(logName); 58  } 59  } 60  } 61  assert logger != null; 62  } 63  64  void warnNoDuplicates(String message) { 65  //noinspection OptionalGetWithoutIsPresent 66  String additional = Arrays.stream(new Throwable().getStackTrace()).skip(2).findFirst().get().toString().trim(); 67  String msg = message + " " + additional; 68  if (!warnings.contains(msg)) { 69  warnings.add(msg); 70  warn(msg); 71  } 72  } 73  74  public static Log getLogger(Class logName) { 75  return getLogger(logName.getName()); 76  } 77  78  public static Log getLogger(String logName) { 79  if (loggers.containsKey(logName)) { 80  return loggers.get(logName); 81  } 82  Log log = new Log(logName); 83  loggers.put(logName, log); 84  return log; 85  } 86  87  88  public boolean isDebugEnabled() { 89  return logger.isDebugEnabled(); 90  } 91  92  public void debug(Object message, Object... params) { 93  logger.debug(message, params); 94  } 95  96  public void info(Object message) { 97  logger.info(message); 98  } 99  100  public void info(Object message, Throwable t) { 101  logger.info(message, t); 102  } 103  104  public void warn(Object message) { 105  logger.warn(message); 106  } 107  108  public void warn(Object message, Throwable t) { 109  logger.warn(message, t); 110  } 111  112  113  public void error(Object message) { 114  logger.error(message); 115  } 116  117  public void error(Object message, Throwable t) { 118  logger.error(message, t); 119  } 120  121  public LogMode getLogMode() { 122  return logger.getLogMode(); 123  } 124  125  public String getLogName() { 126  return logger.getLogName(); 127  } 128 }