2009/05/20 - Apache Shale has been retired.

For more information, please explore the Attic.

View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to you under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.shale.remoting.logger;
19  
20  import org.apache.shale.remoting.logger.Logger;
21  
22  /***
23   * <p>Default {@link Logger} implementation that delegates to Apache Commons
24   * Logging (if present), or JDK <code>java.util.Logging</code> (if present),
25   * or simply writes to <code>System.out</code>.</p>
26   */
27  public class DefaultLogger extends AbstractLogger {
28  
29  
30      // ------------------------------------------------------------ Constructors
31  
32  
33      /***
34       * <p>Construct a new DefaultLogger instance, deciding which
35       * {@link Logger} implementation, if any, we should delegate to.</p>
36       */
37      public DefaultLogger() {
38  
39          ClassLoader loader = Thread.currentThread().getContextClassLoader();
40  
41          // Delegate to Apache Commons Logging if possible
42          try {
43              Class clazz = loader.loadClass("org.apache.shale.remoting.logger.CommonsLogger");
44              this.logger = (Logger) clazz.newInstance();
45              return;
46          } catch (Exception e) {
47              ; // Fall through
48          }
49  
50          // Delegate to JDK java.util.logging Logging if possible
51          try {
52              Class clazz = loader.loadClass("org.apache.shale.remoting.logger.JdkLogger");
53              this.logger = (Logger) clazz.newInstance();
54              return;
55          } catch (Exception e) {
56              ; // Fall through
57          }
58  
59          // We will do the logging stuff ourself
60  
61      }
62  
63  
64      // ------------------------------------------------------ Instance Variables
65  
66  
67      /***
68       * <p>The {@link Logger} instance we should delegate to (if any).</p>
69       */
70      protected Logger logger = null;
71  
72  
73      // ------------------------------------------------------ Condition Checking
74  
75  
76      /*** {@inheritDoc} */
77      public boolean isTraceEnabled(String name) {
78          if (logger != null) {
79              return logger.isTraceEnabled(name);
80          }
81          return false;
82      }
83  
84  
85      /*** {@inheritDoc} */
86      public boolean isDebugEnabled(String name) {
87          if (logger != null) {
88              return logger.isDebugEnabled(name);
89          }
90          return false;
91      }
92  
93  
94      /*** {@inheritDoc} */
95      public boolean isInfoEnabled(String name) {
96          if (logger != null) {
97              return logger.isInfoEnabled(name);
98          }
99          return false;
100     }
101 
102 
103     /*** {@inheritDoc} */
104     public boolean isWarnEnabled(String name) {
105         if (logger != null) {
106             return logger.isWarnEnabled(name);
107         }
108         return false;
109     }
110 
111 
112     /*** {@inheritDoc} */
113     public boolean isErrorEnabled(String name) {
114         if (logger != null) {
115             return logger.isErrorEnabled(name);
116         }
117         return false;
118     }
119 
120 
121     /*** {@inheritDoc} */
122     public boolean isFatalEnabled(String name) {
123         if (logger != null) {
124             return logger.isFatalEnabled(name);
125         }
126         return false;
127     }
128 
129 
130     // ------------------------------------------------------------- Log Methods
131 
132 
133     /*** {@inheritDoc} */
134     public void trace(String name, String message,
135                       Throwable exception, Object[] params) {
136         if (logger != null) {
137             trace(name, message, exception, params);
138             return;
139         }
140         System.out.println("TRACE: " + message(message, params));
141         if (exception != null) {
142             exception.printStackTrace(System.out);
143         }
144     }
145 
146 
147     /*** {@inheritDoc} */
148     public void debug(String name, String message,
149                       Throwable exception, Object[] params) {
150         if (logger != null) {
151             debug(name, message, exception, params);
152             return;
153         }
154         System.out.println("DEBUG: " + message(message, params));
155         if (exception != null) {
156             exception.printStackTrace(System.out);
157         }
158     }
159 
160 
161     /*** {@inheritDoc} */
162     public void info(String name, String message,
163                      Throwable exception, Object[] params) {
164         if (logger != null) {
165             info(name, message, exception, params);
166             return;
167         }
168         System.out.println("INFO: " + message(message, params));
169         if (exception != null) {
170             exception.printStackTrace(System.out);
171         }
172     }
173 
174 
175     /*** {@inheritDoc} */
176     public void warn(String name, String message,
177                      Throwable exception, Object[] params) {
178         if (logger != null) {
179             warn(name, message, exception, params);
180             return;
181         }
182         System.out.println("WARN: " + message(message, params));
183         if (exception != null) {
184             exception.printStackTrace(System.out);
185         }
186     }
187 
188 
189     /*** {@inheritDoc} */
190     public void error(String name, String message,
191                       Throwable exception, Object[] params) {
192         if (logger != null) {
193             error(name, message, exception, params);
194             return;
195         }
196         System.out.println("ERROR: " + message(message, params));
197         if (exception != null) {
198             exception.printStackTrace(System.out);
199         }
200     }
201 
202 
203     /*** {@inheritDoc} */
204     public void fatal(String name, String message,
205                       Throwable exception, Object[] params) {
206         if (logger != null) {
207             fatal(name, message, exception, params);
208             return;
209         }
210         System.out.println("FATAL: " + message(message, params));
211         if (exception != null) {
212             exception.printStackTrace(System.out);
213         }
214     }
215 
216 
217 }