KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > tools > testrecorder > shared > Logger


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
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  * $Header:$
17  */

18
19 package org.apache.beehive.netui.tools.testrecorder.shared;
20
21 import java.io.PrintWriter JavaDoc;
22 import java.io.StringWriter JavaDoc;
23
24 import org.apache.log4j.*;
25 import org.apache.commons.logging.Log;
26
27 public class Logger implements Log {
28
29     private static final String JavaDoc STRUTS_APPENDER = "commons-logging";
30
31     static {
32         // Need to get rid of the appender that Struts adds so
33
// that we don't spam the console with all messages
34
Category root = Category.getRoot();
35
36         if ( root.getAppender( STRUTS_APPENDER ) != null ) {
37             root.removeAppender( STRUTS_APPENDER );
38         }
39     }
40
41
42     public static Logger getInstance( Class JavaDoc clazz ) {
43         return new Logger( clazz );
44     }
45
46     private org.apache.log4j.Logger logInstance;
47
48     public Logger( Class JavaDoc clazz ) {
49         logInstance = org.apache.log4j.Logger.getLogger( clazz.getName() );
50     }
51
52     public boolean isDebugEnabled() {
53         return logInstance.isEnabledFor( Level.DEBUG );
54     }
55
56     public boolean isErrorEnabled() {
57         return logInstance.isEnabledFor( Level.ERROR );
58     }
59
60     public boolean isFatalEnabled() {
61         return logInstance.isEnabledFor( Level.FATAL );
62     }
63
64     public boolean isInfoEnabled() {
65         return logInstance.isEnabledFor( Level.INFO );
66     }
67
68     public boolean isTraceEnabled() {
69         return logInstance.isEnabledFor( Level.DEBUG );
70     }
71
72     public boolean isWarnEnabled() {
73         return logInstance.isEnabledFor( Level.WARN );
74     }
75
76     public void debug( Object JavaDoc message ) {
77         if ( logInstance.isEnabledFor( Level.DEBUG ) ) {
78             logInstance.debug( message );
79         }
80     }
81
82     public void debug( Object JavaDoc message, Throwable JavaDoc t ) {
83         if ( logInstance.isEnabledFor( Level.DEBUG ) ) {
84             logInstance.debug( format( message, t ) );
85         }
86     }
87
88     public void trace( Object JavaDoc message ) {
89         if ( logInstance.isEnabledFor( Level.DEBUG ) ) {
90             logInstance.debug( message );
91         }
92     }
93
94     public void trace( Object JavaDoc message, Throwable JavaDoc t ) {
95         if ( logInstance.isEnabledFor( Level.DEBUG ) ) {
96             logInstance.debug( format( message, t ) );
97         }
98     }
99
100     public void info( Object JavaDoc message ) {
101         if ( logInstance.isEnabledFor( Level.INFO ) ) {
102             logInstance.info( message );
103         }
104     }
105
106     public void info( Object JavaDoc message, Throwable JavaDoc t ) {
107         if ( logInstance.isEnabledFor( Level.INFO ) ) {
108             logInstance.info( format( message, t ) );
109         }
110     }
111
112     public void warn( Object JavaDoc message ) {
113         if ( logInstance.isEnabledFor( Level.WARN ) ) {
114             logInstance.warn( message );
115         }
116     }
117
118     public void warn( Object JavaDoc message, Throwable JavaDoc t ) {
119         if ( logInstance.isEnabledFor( Level.WARN ) ) {
120             logInstance.warn( format( message, t ) );
121         }
122     }
123
124     public void error( Object JavaDoc message ) {
125         if ( logInstance.isEnabledFor( Level.ERROR ) ) {
126             logInstance.error( message );
127         }
128     }
129
130     public void error( Object JavaDoc message, Throwable JavaDoc t ) {
131         if ( logInstance.isEnabledFor( Level.ERROR ) ) {
132             logInstance.error( format( message, t ) );
133         }
134     }
135
136     public void fatal( Object JavaDoc message ) {
137         if ( logInstance.isEnabledFor( Level.FATAL ) ) {
138             logInstance.fatal( message );
139         }
140     }
141
142     public void fatal( Object JavaDoc message, Throwable JavaDoc t ) {
143         if ( logInstance.isEnabledFor( Level.FATAL ) ) {
144             logInstance.fatal( format( message, t ) );
145         }
146     }
147
148     public static String JavaDoc format( final Object JavaDoc obj, final Throwable JavaDoc t ) {
149         if ( t == null ) {
150             return obj.toString();
151         }
152         Throwable JavaDoc ex = t;
153         StringBuffer JavaDoc sb = new StringBuffer JavaDoc( 512 );
154         sb.append( "object( " + obj + " )\n" );
155         while ( ex != null ) {
156             sb.append( format( ex ) + "\n");
157             ex = ex.getCause();
158         }
159         return sb.toString();
160     }
161
162     public static String JavaDoc format( Throwable JavaDoc t ) {
163         if ( t == null ) {
164             return "NULL";
165         }
166         StringWriter JavaDoc sw = new StringWriter JavaDoc();
167         t.printStackTrace( new PrintWriter JavaDoc( sw ) );
168         return "throwable: msg( " + t.getMessage() + " )\ntoString( " + t.toString() +
169                 " )\nstack trace:\n" + sw.toString();
170     }
171
172 }
173
Popular Tags