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

Class Method, % Line, %
Log 100% (19/19) 77.6% (45/58)
Log$1 100% (1/1) 100% (1/1)
Total 100% (20/20) 78% (46/59)


1 package net.sf.persism; 2  3  4 import net.sf.persism.logging.*; 5  6 import java.util.*; 7 import java.util.concurrent.ConcurrentHashMap; 8  9 /** 10  * Logging wrapper to avoid runtime dependencies. 11  * It will use slf4j if available or log4j if available otherwise it falls back to JUL (java.util.logging). 12  * <p/> 13  * JUL will use the default java logging.properties 14  * log4j will use log4j.properties 15  * slf4j will use logback.xml 16  * <p/> 17  * log4j DEBUG maps to JUL FINE 18  * log4j ERROR maps to JUL SEVERE 19  * <p/> 20  * The class includes an overloaded error method to log stack traces as well as the message. 21  * 22  * @author Dan Howard 23  * @since 4/21/12 6:47 AM 24  */ 25 final class Log { 26  27  private AbstractLogger logger; 28  29  private static final Map<String, Log> loggers = new ConcurrentHashMap<String, Log>(12); 30  31  private static final List<String> warnings = new ArrayList<>(32); 32  33  public Log(String logName, LogMode logMode) { 34  switch (logMode) { 35  case SLF4J -> { 36  logger = new Slf4jLogger(logName); 37  } 38  case LOG4J2 -> { 39  logger = new Log4j2Logger(logName); 40  } 41  case LOG4J -> { 42  logger = new Log4jLogger(logName); 43  } 44  case JUL -> { 45  logger = new JulLogger(logName); 46  } 47  } 48  } 49  50  Log(String logName) { 51  try { 52  Class.forName("org.slf4j.Logger"); 53  logger = new Slf4jLogger(logName); 54  } catch (ClassNotFoundException e) { 55  try { 56  Class.forName("org.apache.log4j.Logger"); 57  logger = new Log4jLogger(logName); 58  } catch (ClassNotFoundException e1) { 59  try { 60  Class.forName("org.apache.logging.log4j.Logger"); 61  logger = new Log4j2Logger(logName); 62  } catch (ClassNotFoundException classNotFoundException) { 63  logger = new JulLogger(logName); 64  } 65  } 66  } 67  } 68  69  void warnNoDuplicates(String message) { 70  String additional = ""; 71  72  Throwable throwable = new Throwable(""); 73  // This finds the stack element for the user's package name - should be the source of the call to include in the message 74  Optional<StackTraceElement> opt = Arrays.stream(throwable.getStackTrace()). 75  filter(e -> !e.getClassName().startsWith("net.sf.persism")).findFirst(); 76  77  if (opt.isPresent()) { 78  additional = opt.get().toString().trim(); 79  } 80  81  String msg = message + " " + additional; 82  if (!warnings.contains(msg)) { 83  warnings.add(msg); 84  warn(msg); 85  } 86  } 87  88  public static Log getLogger(Class<?> logName) { 89  return getLogger(logName.getName()); 90  } 91  92  // for unit tests 93  static Log getLogger(Class<?> logName, LogMode logMode) { 94  if (loggers.containsKey(logName.getName())) { 95  return loggers.get(logName.getName()); 96  } 97  Log log = new Log(logName.getName(), logMode); 98  loggers.put(logName.getName(), log); 99  return log; 100  } 101  102  public static Log getLogger(String logName) { 103  if (loggers.containsKey(logName)) { 104  return loggers.get(logName); 105  } 106  Log log = new Log(logName); 107  loggers.put(logName, log); 108  return log; 109  } 110  111  List<String> warnings() { 112  return warnings; 113  } 114  115  116  public boolean isDebugEnabled() { 117  return logger.isDebugEnabled(); 118  } 119  120  public void debug(Object message) { 121  logger.debug(message); 122  } 123  124  public void debug(Object message, Object... params) { 125  logger.debug(message, params); 126  } 127  128  public void info(Object message) { 129  logger.info(message); 130  } 131  132  public void info(Object message, Throwable t) { 133  logger.info(message, t); 134  } 135  136  public void warn(Object message) { 137  logger.warn(message); 138  } 139  140  public void warn(Object message, Throwable t) { 141  logger.warn(message, t); 142  } 143  144  public void error(Object message) { 145  logger.error(message); 146  } 147  148  public void error(Object message, Throwable t) { 149  logger.error(message, t); 150  } 151  152  public LogMode getLogMode() { 153  return logger.getLogMode(); 154  } 155  156  public String getLogName() { 157  return logger.getLogName(); 158  } 159 }