View Javadoc

1   /**
2    * Copyright 2005-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.core.util;
17  
18  import org.apache.log4j.Logger;
19  import static org.apache.log4j.Level.WARN;
20  import static org.apache.log4j.Level.ERROR;
21  import static org.apache.log4j.Level.FATAL;
22  
23  
24  /**
25   * Class with static methods wrapping {@link Logger} methods. Automatically sets up logger for you. It's called the <code>BufferedLogger</code> because
26   * it handles everything in a {@link StringBuilder} using {@link StringBuilder#append(CharSequence)} method<br/>
27   * <br/>
28   *  
29   * To use these just do
30   * <code>
31   * import BufferedLogger.*
32   * </code>
33   * 
34   * @see org.apache.log4j.Logger
35   */
36  public class BufferedLogger {
37      
38      /**
39       * Applies a pattern with parameters to create a {@link String} used as a logging message
40       *  
41       * 
42       * @param pattern to format against
43       * @param objs an array of objects used as parameters to the <code>pattern</code>
44       * @return Logging Message
45       */
46      private static final CharSequence getMessage(Object ... objs) {
47          StringBuilder retval = new StringBuilder();
48          
49          for (Object obj : objs) {
50              retval.append(obj);
51          }
52          
53          return retval;
54      }
55      
56      /**
57       * Uses {@link StackTraceElement[]} from {@link Throwable} to determine the calling class. Then, the {@link Logger} is retrieved for it by
58       * convention
59       * 
60       * 
61       * @return Logger for the calling class
62       */
63      private static final Logger getLogger() {
64          try {
65              return Logger.getLogger(Class.forName(new Throwable().getStackTrace()[2].getClassName()));
66          }
67          catch (Exception e) {
68              // This will never happen unless Java is broken
69              return Logger.getLogger(BufferedLogger.class);
70          }
71      }
72  
73      /**
74       * Uses {@link StackTraceElement[]} from {@link Throwable} to determine the calling class. Then, the {@link Logger} is retrieved for it by
75       * convention. Just like {@link #getLogger()} except this is intended to be called directly from classes.
76       * 
77       * 
78       * @return Logger for the calling class
79       */
80      public static final Logger logger() {
81          try {
82              return Logger.getLogger(Class.forName(new Throwable().getStackTrace()[1].getClassName()));
83          }
84          catch (Exception e) {
85              // This will never happen unless Java is broken
86              return Logger.getLogger(BufferedLogger.class);
87          }
88      }
89  
90      /**
91       * Wraps {@link Logger#trace(String)}
92       * 
93       * @param pattern to format against
94       * @param objs an array of objects used as parameters to the <code>pattern</code>
95       */
96      public static final void trace(Object ... objs) {
97          Logger log = getLogger();
98          if (log.isTraceEnabled()) {
99              log.trace(getMessage(objs));
100         }
101     }
102 
103     /**
104      * Wraps {@link Logger#debug(String)}
105      * 
106      * @param pattern to format against
107      * @param objs an array of objects used as parameters to the <code>pattern</code>
108      */
109     public static final void debug(Object ... objs) {
110         Logger log = getLogger();
111         if (log.isDebugEnabled()) {
112             log.debug(getMessage(objs));
113         }
114     }
115 
116     /**
117      * Wraps {@link Logger#info(String)}
118      * 
119      * @param pattern to format against
120      * @param objs an array of objects used as parameters to the <code>pattern</code>
121      */
122     public static final void info(Object ... objs) {
123         Logger log = getLogger();
124         if (log.isInfoEnabled()) {
125             log.info(getMessage(objs));
126         }
127     }
128 
129     /**
130      * Wraps {@link Logger#warn(String)}
131      * 
132      * @param pattern to format against
133      * @param objs an array of objects used as parameters to the <code>pattern</code>
134      */
135     public static final void warn(Object ... objs) {
136         Logger log = getLogger();
137         if (log.isEnabledFor(WARN)) {
138             log.warn(getMessage(objs));
139         }
140     }
141 
142     /**
143      * Wraps {@link Logger#error(String)}
144      * 
145      * @param pattern to format against
146      * @param objs an array of objects used as parameters to the <code>pattern</code>
147      */
148     public static final void error(Object ... objs) {
149         Logger log = getLogger();
150         if (log.isEnabledFor(ERROR)) {
151             getLogger().error(getMessage(objs));
152         }
153     }
154     
155     /**
156      * Wraps {@link Logger#fatal(String)}
157      * 
158      * @param pattern to format against
159      * @param objs an array of objects used as parameters to the <code>pattern</code>
160      */
161     public static final void fatal(Object ... objs) {
162         Logger log = getLogger();
163         if (log.isEnabledFor(FATAL)) {
164             log.fatal(getMessage(objs));
165         }
166     }
167 }