View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.geronimo.genesis.logging;
21  
22  import java.io.Serializable;
23  import java.lang.reflect.Constructor;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.impl.SimpleLog;
27  
28  /**
29   * Jakarta Commons Logging Log which delegates to another Log type.
30   *
31   * @version $Rev: 470159 $ $Date: 2006-11-01 17:19:16 -0800 (Wed, 01 Nov 2006) $
32   */
33  public class DelegatingLog
34      implements Log, Serializable
35  {
36      private static Constructor factory;
37  
38      public static void setDelegateType(final Class type) {
39          assert type != null;
40  
41          try {
42              factory = type.getConstructor(new Class[] { String.class });
43          }
44          catch (NoSuchMethodException e) {
45              throw new RuntimeException("Failed to lookup (String) constructor for type: " + type, e);
46          }
47      }
48  
49      public static Log getDelegate(final String name) {
50          // If no factory is set, then use a SimpleLog... so logging always works
51          if (factory == null) {
52              return new SimpleLog(name);
53          }
54  
55          try {
56              return (Log) factory.newInstance(new Object[] { name });
57          }
58          catch (Exception e) {
59              throw new RuntimeException("Failed to construct delegate logger using factory: " + factory, e);
60          }
61      }
62  
63      private String name;
64  
65      public DelegatingLog(final String name) {
66          assert name != null;
67  
68          this.name = name;
69      }
70  
71      private Log getLog() {
72          return getDelegate(name);
73      }
74  
75      public boolean isDebugEnabled() {
76          return getLog().isDebugEnabled();
77      }
78  
79      public boolean isErrorEnabled() {
80          return getLog().isErrorEnabled();
81      }
82  
83      public boolean isFatalEnabled() {
84          return getLog().isErrorEnabled();
85      }
86  
87      public boolean isInfoEnabled() {
88          return getLog().isInfoEnabled();
89      }
90  
91      public boolean isTraceEnabled() {
92          return getLog().isTraceEnabled();
93      }
94  
95      public boolean isWarnEnabled() {
96          return getLog().isWarnEnabled();
97      }
98  
99      private String createMessage(final Object object) {
100         return String.valueOf(object);
101     }
102 
103     public void trace(final Object object) {
104         getLog().trace(object);
105     }
106 
107     public void trace(final Object object, final Throwable throwable) {
108         getLog().trace(object, throwable);
109     }
110 
111     public void debug(final Object object) {
112         getLog().debug(createMessage(object));
113     }
114 
115     public void debug(final Object object, final Throwable throwable) {
116         getLog().debug(createMessage(object), throwable);
117     }
118 
119     public void info(final Object object) {
120         getLog().info(createMessage(object));
121     }
122 
123     public void info(final Object object, final Throwable throwable) {
124         getLog().info(createMessage(object), throwable);
125     }
126 
127     public void warn(final Object object) {
128         getLog().warn(createMessage(object));
129     }
130 
131     public void warn(final Object object, final Throwable throwable) {
132         getLog().warn(createMessage(object), throwable);
133     }
134 
135     public void error(final Object object) {
136         getLog().error(createMessage(object));
137     }
138 
139     public void error(final Object object, final Throwable throwable) {
140         getLog().error(createMessage(object), throwable);
141     }
142 
143     public void fatal(final Object object) {
144         error(object);
145     }
146 
147     public void fatal(final Object object, final Throwable throwable) {
148         error(object, throwable);
149     }
150 }