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.commons.proxy.interceptor.logging;
19  
20  import java.lang.reflect.Method;
21  import java.util.logging.Level;
22  import java.util.logging.Logger;
23  
24  /**
25   * An interceptor which logs method invocations using a {@link Logger JDK logger} using the "finer" logging level.
26   * 
27   * @auothor James Carman
28   * @since 1.1
29   */
30  public class JdkLoggingInterceptor extends AbstractLoggingInterceptor
31  {
32  //**********************************************************************************************************************
33  // Fields
34  //**********************************************************************************************************************
35  
36      private final String loggerName;
37  
38  //**********************************************************************************************************************
39  // Constructors
40  //**********************************************************************************************************************
41  
42      public JdkLoggingInterceptor( Class clazz )
43      {
44          this(clazz.getName());
45      }
46  
47      public JdkLoggingInterceptor( String loggerName )
48      {
49          this.loggerName = loggerName;
50      }
51  
52  //**********************************************************************************************************************
53  // Other Methods
54  //**********************************************************************************************************************
55  
56      private Logger getLogger()
57      {
58          return Logger.getLogger(loggerName);
59      }
60  
61      protected void entering( Method method, Object[] args )
62      {
63          getLogger().entering(method.getDeclaringClass().getName(), method.getName(), args);
64      }
65  
66      protected void exiting( Method method, Object result )
67      {
68          getLogger().exiting(method.getDeclaringClass().getName(), method.getName(), result);
69      }
70  
71      protected boolean isLoggingEnabled()
72      {
73          return getLogger().isLoggable(Level.FINER);
74      }
75  
76      protected void logMessage( String message )
77      {
78          // Do nothing!
79      }
80  
81      protected void logMessage( String message, Throwable t )
82      {
83          // Do nothing!
84      }
85  
86      protected void throwing( Method method, Throwable t )
87      {
88          getLogger().throwing(method.getDeclaringClass().getName(), method.getName(), t);
89      }
90  }