View Javadoc

1   /**
2      This file is part of GoldenGate Project (named also GoldenGate or GG).
3   
4      Copyright 2009, Frederic Bregier, and individual contributors by the @author
5      tags. See the COPYRIGHT.txt in the distribution for a full listing of
6      individual contributors.
7   
8      All GoldenGate Project is free software: you can redistribute it and/or 
9      modify it under the terms of the GNU General Public License as published 
10     by the Free Software Foundation, either version 3 of the License, or
11     (at your option) any later version.
12  
13     GoldenGate is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17  
18     You should have received a copy of the GNU General Public License
19     along with GoldenGate .  If not, see <http://www.gnu.org/licenses/>.
20   */
21  package goldengate.common.logging;
22  
23  import org.jboss.netty.logging.InternalLogLevel;
24  import org.jboss.netty.logging.InternalLogger;
25  
26  /**
27   * Logger inspired from Netty implementation, adding some extra commands that
28   * allow to limit the overhead of some ignored logger calls (toString or string
29   * construction is called only if necessary).
30   *
31   * Based on The Netty Project (netty-dev@lists.jboss.org)
32   *
33   * @author Trustin Lee (tlee@redhat.com)
34   *
35   * @author Frederic Bregier
36   *
37   */
38  public abstract class GgInternalLogger implements InternalLogger {
39      private static int BASELEVEL;
40      /**
41       * Determine the good level
42       * @return the default base level
43       */
44      private static int detectLoggingBaseLevel() {
45          StackTraceElement []elt = Thread.currentThread().getStackTrace();
46          int i = 0;
47          for (i = 0; i < elt.length ; i++) {
48              if (elt[i].getMethodName().equalsIgnoreCase("detectLoggingBaseLevel")) {
49                  break;
50              }
51          }
52          return i;
53      }
54      {
55          BASELEVEL = detectLoggingBaseLevel();
56      }
57      /**
58       * To be used in message for logger (rank 2) like
59       * logger.warn(code,"message:"+getImmediateMethodAndLine(),null);
60       *
61       * @return "ClassAndMethodName(FileName:LineNumber)"
62       */
63      public static String getImmediateMethodAndLine() {
64          StackTraceElement elt = Thread.currentThread().getStackTrace()[BASELEVEL+1];
65          return getMethodAndLine(elt);
66      }
67  //FIXME TODO for JDK6 IBM add 1 (2->3 and 3->4)
68      /**
69       * To be used only by Logger (rank 5)
70       *
71       * @return "MethodName(FileName:LineNumber)"
72       */
73      protected static String getLoggerMethodAndLine() {
74          StackTraceElement elt = Thread.currentThread().getStackTrace()[BASELEVEL+2];
75          return getMethodAndLine(elt);
76      }
77  
78      /**
79       * @param rank
80       *            is the current depth of call+1 (immediate = 1+1=2)
81       * @return "ClassAndMethodName(FileName:LineNumber)"
82       */
83      public static String getRankMethodAndLine(int rank) {
84          StackTraceElement elt = Thread.currentThread().getStackTrace()[rank];
85          return getMethodAndLine(elt);
86      }
87      /**
88       *
89       * @param elt
90       * @return "MethodName(FileName:LineNumber) " from elt
91       */
92      private static String getMethodAndLine(StackTraceElement elt) {
93          StringBuilder builder = new StringBuilder(elt.getClassName());
94          builder.append('.');
95          builder.append(elt.getMethodName());
96          builder.append('(');
97          builder.append(elt.getFileName());
98          builder.append(':');
99          builder.append(elt.getLineNumber());
100         builder.append(") : ");
101         return builder.toString();
102     }
103     /**
104      * @param level
105      * @return True if the level is enabled
106      */
107     public boolean isEnabled(InternalLogLevel level) {
108         switch (level) {
109             case DEBUG:
110                 return isDebugEnabled();
111             case INFO:
112                 return isInfoEnabled();
113             case WARN:
114                 return isWarnEnabled();
115             case ERROR:
116                 return isErrorEnabled();
117             default:
118                 throw new Error();
119         }
120     }
121 
122     public void log(InternalLogLevel level, String msg, Throwable cause) {
123         switch (level) {
124             case DEBUG:
125                 debug(msg, cause);
126                 break;
127             case INFO:
128                 info(msg, cause);
129                 break;
130             case WARN:
131                 warn(msg, cause);
132                 break;
133             case ERROR:
134                 error(msg, cause);
135                 break;
136             default:
137                 throw new Error();
138         }
139     }
140 
141     public void log(InternalLogLevel level, String msg) {
142         switch (level) {
143             case DEBUG:
144                 debug(msg);
145                 break;
146             case INFO:
147                 info(msg);
148                 break;
149             case WARN:
150                 warn(msg);
151                 break;
152             case ERROR:
153                 error(msg);
154                 break;
155             default:
156                 throw new Error();
157         }
158     }
159 
160     /**
161      *
162      * @param format
163      * @param arg1
164      */
165     public abstract void debug(String format, String arg1);
166 
167     /**
168      *
169      * @param format
170      * @param arg1
171      */
172     public abstract void info(String format, String arg1);
173 
174     /**
175      *
176      * @param format
177      * @param arg1
178      */
179     public abstract void warn(String format, String arg1);
180 
181     /**
182      *
183      * @param format
184      * @param arg1
185      */
186     public abstract void error(String format, String arg1);
187 
188     /**
189      *
190      * @param format
191      * @param arg1
192      * @param arg2
193      */
194     public abstract void debug(String format, String arg1, String arg2);
195 
196     /**
197      *
198      * @param format
199      * @param arg1
200      * @param arg2
201      */
202     public abstract void info(String format, String arg1, String arg2);
203 
204     /**
205      *
206      * @param format
207      * @param arg1
208      * @param arg2
209      */
210     public abstract void warn(String format, String arg1, String arg2);
211 
212     /**
213      *
214      * @param format
215      * @param arg1
216      * @param arg2
217      */
218     public abstract void error(String format, String arg1, String arg2);
219 
220     /**
221      *
222      * @param format
223      * @param arg1
224      * @param arg2
225      */
226     public abstract void debug(String format, Object arg1, Object arg2);
227 
228     /**
229      *
230      * @param format
231      * @param arg1
232      * @param arg2
233      */
234     public abstract void info(String format, Object arg1, Object arg2);
235 
236     /**
237      *
238      * @param format
239      * @param arg1
240      * @param arg2
241      */
242     public abstract void warn(String format, Object arg1, Object arg2);
243 
244     /**
245      *
246      * @param format
247      * @param arg1
248      * @param arg2
249      */
250     public abstract void error(String format, Object arg1, Object arg2);
251 
252     /**
253      *
254      * @param format
255      * @param arg1
256      */
257     public abstract void debug(String format, Object arg1);
258 
259     /**
260      *
261      * @param format
262      * @param arg1
263      */
264     public abstract void info(String format, Object arg1);
265 
266     /**
267      *
268      * @param format
269      * @param arg1
270      */
271     public abstract void warn(String format, Object arg1);
272 
273     /**
274      *
275      * @param format
276      * @param arg1
277      */
278     public abstract void error(String format, Object arg1);
279 }