View Javadoc
1   /*
2    * Licensed under the Apache License, Version 2.0 (the "License");
3    * you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    * http://www.apache.org/licenses/LICENSE-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   * See the License for the specific language governing permissions and
12   * limitations under the License.
13   */
14  
15  package org.basepom.mojo.propertyhelper.util;
16  
17  import static com.google.common.base.Preconditions.checkNotNull;
18  
19  import com.google.common.annotations.VisibleForTesting;
20  import org.slf4j.Logger;
21  import org.slf4j.LoggerFactory;
22  
23  /**
24   * Wraps a Logger into a number of convenience methods such as varargs.
25   */
26  public final class Log {
27  
28      private static final String LOG_NAME = Log.class.getName();
29  
30      private final Logger wrappedLogger;
31  
32      private Log(final Logger wrappedLogger) {
33          this.wrappedLogger = checkNotNull(wrappedLogger, "wrappedLogger is null");
34      }
35  
36      /**
37       * Finds the logger for the current class by using the call stack.
38       */
39      public static Log findLog() {
40          return findLog(0);
41      }
42  
43      /**
44       * Finds the logger for the caller by using the call stack.
45       */
46      public static Log findCallerLog() {
47          return findLog(1);
48      }
49  
50      /**
51       * Returns a Logger for a given class.
52       */
53      public static Log forClass(final Class<?> clazz) {
54          return forName(clazz.getName());
55      }
56  
57      /**
58       * Returns a Logger for a given class or category name.
59       */
60      public static Log forName(final String name) {
61          return new Log(LoggerFactory.getLogger(name));
62      }
63  
64      /**
65       * Returns a Logger for a given log4j logger.
66       */
67      public static Log forLogger(final Logger wrappedLogger) {
68          return new Log(wrappedLogger);
69      }
70  
71      private static Log findLog(final int depth) {
72          final StackTraceElement[] stacktrace = Thread.currentThread().getStackTrace();
73          int i = 1;
74          while (i < stacktrace.length && stacktrace[i].getClassName().equals(LOG_NAME)) {
75              i++;
76          }
77  
78          if (i + depth < stacktrace.length) {
79              return forName(stacktrace[i + depth].getClassName());
80          }
81          throw new IllegalStateException(
82                  String.format("Attempt to generate a logger for an invalid depth (%d vs. %d).", depth,
83                          stacktrace.length - i));
84      }
85  
86      @VisibleForTesting
87      Logger getWrappedLogger() {
88          return wrappedLogger;
89      }
90  
91      // ========================================================================
92      //
93      // Level mgt.
94      //
95      // ========================================================================
96  
97      public boolean isTraceEnabled() {
98          return wrappedLogger.isTraceEnabled();
99      }
100 
101     public boolean isDebugEnabled() {
102         return wrappedLogger.isDebugEnabled();
103     }
104 
105     public boolean isErrorEnabled() {
106         return wrappedLogger.isErrorEnabled();
107     }
108 
109     public boolean isWarnEnabled() {
110         return wrappedLogger.isWarnEnabled();
111     }
112 
113     public boolean isInfoEnabled() {
114         return wrappedLogger.isInfoEnabled();
115     }
116 
117     // ========================================================================
118     //
119     // Trace level methods
120     //
121     // ========================================================================
122 
123     public void trace(final Throwable t, final String message, final Object... args) {
124         if (wrappedLogger.isTraceEnabled()) {
125             wrappedLogger.trace(String.format(message, args), t);
126         }
127     }
128 
129     public void trace(final String message, final Throwable t, final Object... args) {
130         trace(t, message, args);
131     }
132 
133     public void trace(final Throwable t, final String message) {
134         wrappedLogger.trace(message, t);
135     }
136 
137     public void trace(final String message) {
138         wrappedLogger.trace(message);
139     }
140 
141     public void trace(final String message, final Object... args) {
142         if (wrappedLogger.isTraceEnabled()) {
143             wrappedLogger.trace(String.format(message, args));
144         }
145     }
146 
147     // ========================================================================
148     //
149     // Debug level methods
150     //
151     // ========================================================================
152 
153     public void debug(final Throwable t, final String message, final Object... args) {
154         if (wrappedLogger.isDebugEnabled()) {
155             wrappedLogger.debug(String.format(message, args), t);
156         }
157     }
158 
159     public void debug(final String message, final Throwable t, final Object... args) {
160         debug(t, message, args);
161     }
162 
163     public void debug(final Throwable t, final String message) {
164         wrappedLogger.debug(message, t);
165     }
166 
167     public void debug(final String message) {
168         wrappedLogger.debug(message);
169     }
170 
171     public void debug(final String message, final Object... args) {
172         if (wrappedLogger.isDebugEnabled()) {
173             wrappedLogger.debug(String.format(message, args));
174         }
175     }
176 
177     // ========================================================================
178     //
179     // Info level methods
180     //
181     // ========================================================================
182 
183     public void info(final Throwable t, final String message, final Object... args) {
184         if (wrappedLogger.isInfoEnabled()) {
185             wrappedLogger.info(String.format(message, args), t);
186         }
187     }
188 
189     public void info(final String message, final Throwable t, final Object... args) {
190         info(t, message, args);
191     }
192 
193     public void info(final Throwable t, final String message) {
194         wrappedLogger.info(message, t);
195     }
196 
197     public void info(final String message, final Object... args) {
198         if (wrappedLogger.isInfoEnabled()) {
199             wrappedLogger.info(String.format(message, args));
200         }
201     }
202 
203     public void info(final String message) {
204         wrappedLogger.info(message);
205     }
206 
207     public void infoDebug(final Throwable t, final String infoMessage, final Object... args) {
208         if (wrappedLogger.isDebugEnabled()) {
209             wrappedLogger.info(String.format(infoMessage, args), t);
210         } else if (wrappedLogger.isInfoEnabled()) {
211             wrappedLogger.info(summarize(t, infoMessage, args));
212         }
213     }
214 
215     public void infoDebug(final String infoMessage, final Throwable t, final Object... args) {
216         infoDebug(t, infoMessage, args);
217     }
218 
219     public void infoDebug(final Throwable t, final String infoMessage) {
220         if (wrappedLogger.isDebugEnabled()) {
221             wrappedLogger.info(infoMessage, t);
222         } else {
223             wrappedLogger.info(summarize(t, infoMessage));
224         }
225     }
226 
227     // ========================================================================
228     //
229     // Warn level methods
230     //
231     // ========================================================================
232 
233     public void warn(final Throwable t, final String message, final Object... args) {
234         wrappedLogger.warn(String.format(message, args), t);
235     }
236 
237     public void warn(final String message, final Throwable t, final Object... args) {
238         warn(t, message, args);
239     }
240 
241     public void warn(final Throwable t, final String message) {
242         wrappedLogger.warn(message, t);
243     }
244 
245     public void warn(final String message, final Object... args) {
246         wrappedLogger.warn(String.format(message, args));
247     }
248 
249     public void warn(final String message) {
250         wrappedLogger.warn(message);
251     }
252 
253     public void warnDebug(final Throwable t, final String warnMessage, final Object... args) {
254         if (wrappedLogger.isDebugEnabled()) {
255             wrappedLogger.warn(String.format(warnMessage, args), t);
256         } else if (isWarnEnabled()) {
257             wrappedLogger.warn(summarize(t, warnMessage, args));
258         }
259     }
260 
261     public void warnDebug(final String warnMessage, final Throwable t, final Object... args) {
262         warnDebug(t, warnMessage, args);
263     }
264 
265     public void warnDebug(final Throwable t, final String warnMessage) {
266         if (wrappedLogger.isDebugEnabled()) {
267             wrappedLogger.warn(warnMessage, t);
268         } else {
269             wrappedLogger.warn(summarize(t, warnMessage));
270         }
271     }
272 
273     // ========================================================================
274     //
275     // Error level methods
276     //
277     // ========================================================================
278 
279     public void error(final Throwable t, final String message, final Object... args) {
280         wrappedLogger.error(String.format(message, args), t);
281     }
282 
283     public void error(final String message, final Throwable t, final Object... args) {
284         error(t, message, args);
285     }
286 
287     public void error(final Throwable t, final String message) {
288         wrappedLogger.error(message, t);
289     }
290 
291     public void error(final String message, final Object... args) {
292         wrappedLogger.error(String.format(message, args));
293     }
294 
295     public void error(final String message) {
296         wrappedLogger.error(message);
297     }
298 
299     public void errorDebug(final Throwable t, final String errorMessage, final Object... args) {
300         if (wrappedLogger.isDebugEnabled()) {
301             wrappedLogger.error(String.format(errorMessage, args), t);
302         } else if (isErrorEnabled()) {
303             wrappedLogger.error(summarize(t, errorMessage, args));
304         }
305     }
306 
307     public void errorDebug(final String errorMessage, final Throwable t, final Object... args) {
308         errorDebug(t, errorMessage, args);
309     }
310 
311     public void errorDebug(final Throwable t, final String errorMessage) {
312         if (wrappedLogger.isDebugEnabled()) {
313             wrappedLogger.error(errorMessage, t);
314         } else {
315             wrappedLogger.error(summarize(t, errorMessage));
316         }
317     }
318 
319     private String summarize(final Throwable t, final String format, final Object... args) {
320         final String message = (t == null) ? null : t.getMessage();
321 
322         if (message == null) {
323             return format(format, args);
324         }
325 
326         final int index = message.indexOf('\n');
327 
328         if (index == -1) {
329             return format(format, args) + ": " + message;
330         }
331 
332         final String shortMsg = message.substring(0, index);
333         return format(format, args) + " (Switch to DEBUG for full stack trace): " + shortMsg;
334     }
335 
336     private String format(final String format, final Object... args) {
337         if (format != null) {
338             return String.format(format, args);
339         } else {
340             final StringBuilder sb = new StringBuilder();
341             if (args != null) {
342                 for (int i = 0; i < args.length; i++) {
343                     sb.append(args[i]);
344                     if (i < args.length - 1) {
345                         sb.append(", ");
346                     }
347                 }
348             }
349             return sb.toString();
350         }
351     }
352 }