Coverage Report - com.aragost.javahg.log.JULLogger
 
Classes in this File Line Coverage Branch Coverage Complexity
JULLogger
64%
33/51
80%
8/10
1.263
 
 1  
 package com.aragost.javahg.log;
 2  
 
 3  
 import java.util.logging.Level;
 4  
 
 5  
 import com.google.common.annotations.VisibleForTesting;
 6  
 
 7  
 /**
 8  
  * This simulates the Logger class from slf4j.
 9  
  * <p>
 10  
  * Only the methods that is actually used by JavaHg is implemented,
 11  
  * but others will be added as needed.
 12  
  * <p>
 13  
  * Implementation note: In general for log level abc there is an
 14  
  * <code>void abc(String msg)</code> and
 15  
  * <code>void abc(String msg, Object[] args)</code>. The first simply
 16  
  * writes the message to the backend with no formatting, the second
 17  
  * will do a formatting similar to slf4j. The first is strictly not
 18  
  * needed, but the consequence would be that for even simple log
 19  
  * messages a temp array would be created for the varargs.
 20  
  * 
 21  
  */
 22  
 public class JULLogger implements Logger {
 23  
 
 24  
     private final java.util.logging.Logger julLogger;
 25  
 
 26  8
     public JULLogger(String name) {
 27  8
         this.julLogger = java.util.logging.Logger.getLogger(name);
 28  8
     }
 29  
 
 30  
     public void debug(String msg) {
 31  0
         this.julLogger.fine(msg);
 32  0
     }
 33  
 
 34  
     public void debug(String msg, Object... args) {
 35  3789
         if (isDebugEnabled()) {
 36  0
             this.julLogger.fine(format(msg, args));
 37  
         }
 38  3789
     }
 39  
     
 40  
     public void debug(String msg, Throwable thrown)
 41  
     {
 42  0
       logException(Level.FINE, msg, thrown);
 43  0
     }
 44  
 
 45  
     public void info(String msg) {
 46  26
         this.julLogger.info(msg);
 47  26
     }
 48  
 
 49  
     public void info(String msg, Object... args) {
 50  401
         if (isInfoEnabled()) {
 51  0
             this.julLogger.info(format(msg, args));
 52  
         }
 53  401
     }
 54  
 
 55  
     public void info(String msg, Throwable thrown)
 56  
     {
 57  0
       logException(Level.INFO, msg, thrown);
 58  0
     }
 59  
 
 60  
     public void warn(String msg)
 61  
     {
 62  29
       this.julLogger.warning(msg);
 63  29
     }
 64  
     
 65  
     public void warn(String msg, Object... args) {
 66  12
         this.julLogger.warning(format(msg, args));
 67  12
     }
 68  
 
 69  
     public void warn(String msg, Throwable thrown)
 70  
     {
 71  0
       logException(Level.WARNING, msg, thrown);
 72  0
     }
 73  
 
 74  
     public void error(String msg)
 75  
     {
 76  0
       this.julLogger.severe(msg);
 77  0
     }
 78  
 
 79  
     public void error(String msg, Object... args) {
 80  19
         this.julLogger.severe(format(msg, args));
 81  19
     }
 82  
 
 83  
     public void error(String msg, Throwable thrown) {
 84  0
         logException(Level.SEVERE, msg, thrown);
 85  0
     }
 86  
     
 87  
     public boolean isDebugEnabled() {
 88  10299
         return this.julLogger.isLoggable(Level.FINE);
 89  
     }
 90  
 
 91  
     public boolean isInfoEnabled() {
 92  7997
         return this.julLogger.isLoggable(Level.INFO);
 93  
     }
 94  
 
 95  
     public boolean isWarnEnabled()
 96  
     {
 97  0
       return this.julLogger.isLoggable(Level.WARNING);
 98  
     }
 99  
 
 100  
     public boolean isErrorEnabled()
 101  
     {
 102  0
       return this.julLogger.isLoggable(Level.SEVERE);
 103  
     }
 104  
     
 105  
 
 106  
     /**
 107  
      * Simulate the slf4j formatting of messages. This does not have
 108  
      * all the features of slf4j, it is not possible to escape {} in
 109  
      * the message. It will always be interpreted as an anchore.
 110  
      * <p>
 111  
      * If there is too few args compared to anchors in the format then
 112  
      * the method fails with IndexOutOfBoundException
 113  
      * 
 114  
      * @param format
 115  
      * @param args
 116  
      * @return
 117  
      */
 118  
     @VisibleForTesting
 119  
     static String format(String format, Object[] args) {
 120  35
         int length = format.length();
 121  35
         StringBuilder tgt = new StringBuilder(length);
 122  35
         int prevPos = 0;
 123  35
         int argIndex = 0;
 124  74
         while (prevPos < length) {
 125  39
             int pos = format.indexOf("{}", prevPos);
 126  39
             if (pos >= 0) {
 127  37
                 Object arg = args[argIndex++];
 128  37
                 tgt.append(format.substring(prevPos, pos));
 129  37
                 tgt.append(arg == null ? "" : arg.toString());
 130  37
                 prevPos = pos + 2;
 131  37
             } else {
 132  2
                 tgt.append(format.substring(prevPos));
 133  2
                 prevPos = length;
 134  
             }
 135  39
         }
 136  35
         return tgt.toString();
 137  
     }
 138  
 
 139  
     private void logException(Level level, String msg, Throwable thrown) {
 140  0
         this.julLogger.log(level, msg, thrown);
 141  0
     }
 142  
 }