View Javadoc

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      public JULLogger(String name) {
27          this.julLogger = java.util.logging.Logger.getLogger(name);
28      }
29  
30      public void debug(String msg) {
31          this.julLogger.fine(msg);
32      }
33  
34      public void debug(String msg, Object... args) {
35          if (isDebugEnabled()) {
36              this.julLogger.fine(format(msg, args));
37          }
38      }
39      
40      public void debug(String msg, Throwable thrown)
41      {
42        logException(Level.FINE, msg, thrown);
43      }
44  
45      public void info(String msg) {
46          this.julLogger.info(msg);
47      }
48  
49      public void info(String msg, Object... args) {
50          if (isInfoEnabled()) {
51              this.julLogger.info(format(msg, args));
52          }
53      }
54  
55      public void info(String msg, Throwable thrown)
56      {
57        logException(Level.INFO, msg, thrown);
58      }
59  
60      public void warn(String msg)
61      {
62        this.julLogger.warning(msg);
63      }
64      
65      public void warn(String msg, Object... args) {
66          this.julLogger.warning(format(msg, args));
67      }
68  
69      public void warn(String msg, Throwable thrown)
70      {
71        logException(Level.WARNING, msg, thrown);
72      }
73  
74      public void error(String msg)
75      {
76        this.julLogger.severe(msg);
77      }
78  
79      public void error(String msg, Object... args) {
80          this.julLogger.severe(format(msg, args));
81      }
82  
83      public void error(String msg, Throwable thrown) {
84          logException(Level.SEVERE, msg, thrown);
85      }
86      
87      public boolean isDebugEnabled() {
88          return this.julLogger.isLoggable(Level.FINE);
89      }
90  
91      public boolean isInfoEnabled() {
92          return this.julLogger.isLoggable(Level.INFO);
93      }
94  
95      public boolean isWarnEnabled()
96      {
97        return this.julLogger.isLoggable(Level.WARNING);
98      }
99  
100     public boolean isErrorEnabled()
101     {
102       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         int length = format.length();
121         StringBuilder tgt = new StringBuilder(length);
122         int prevPos = 0;
123         int argIndex = 0;
124         while (prevPos < length) {
125             int pos = format.indexOf("{}", prevPos);
126             if (pos >= 0) {
127                 Object arg = args[argIndex++];
128                 tgt.append(format.substring(prevPos, pos));
129                 tgt.append(arg == null ? "" : arg.toString());
130                 prevPos = pos + 2;
131             } else {
132                 tgt.append(format.substring(prevPos));
133                 prevPos = length;
134             }
135         }
136         return tgt.toString();
137     }
138 
139     private void logException(Level level, String msg, Throwable thrown) {
140         this.julLogger.log(level, msg, thrown);
141     }
142 }