View Javadoc
1   package org.apache.commons.jcs.engine.logging;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.commons.jcs.engine.logging.behavior.ICacheEvent;
23  import org.apache.commons.jcs.engine.logging.behavior.ICacheEventLogger;
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  
27  /**
28   * This implementation simple logs to a commons logger at debug level, for all events. It's mainly
29   * for testing. It isn't very useful otherwise.
30   */
31  public class CacheEventLoggerDebugLogger
32      implements ICacheEventLogger
33  {
34      /** This is the name of the category. */
35      private String logCategoryName = CacheEventLoggerDebugLogger.class.getName();
36  
37      /** The logger. This is recreated on set logCategoryName */
38      private Log log = LogFactory.getLog( logCategoryName );
39  
40      /**
41       * @param source
42       * @param region
43       * @param eventName
44       * @param optionalDetails
45       * @param key
46       * @return ICacheEvent
47       */
48      @Override
49      public <T> ICacheEvent<T> createICacheEvent( String source, String region, String eventName,
50              String optionalDetails, T key )
51      {
52          ICacheEvent<T> event = new CacheEvent<T>();
53          event.setSource( source );
54          event.setRegion( region );
55          event.setEventName( eventName );
56          event.setOptionalDetails( optionalDetails );
57          event.setKey( key );
58  
59          return event;
60      }
61  
62      /**
63       * @param source
64       * @param eventName
65       * @param optionalDetails
66       */
67      @Override
68      public void logApplicationEvent( String source, String eventName, String optionalDetails )
69      {
70          if ( log.isDebugEnabled() )
71          {
72              log.debug( source + " | " + eventName + " | " + optionalDetails );
73          }
74      }
75  
76      /**
77       * @param source
78       * @param eventName
79       * @param errorMessage
80       */
81      @Override
82      public void logError( String source, String eventName, String errorMessage )
83      {
84          if ( log.isDebugEnabled() )
85          {
86              log.debug( source + " | " + eventName + " | " + errorMessage );
87          }
88      }
89  
90      /**
91       * @param event
92       */
93      @Override
94      public <T> void logICacheEvent( ICacheEvent<T> event )
95      {
96          if ( log.isDebugEnabled() )
97          {
98              log.debug( event );
99          }
100     }
101 
102     /**
103      * @param logCategoryName
104      */
105     public synchronized void setLogCategoryName( String logCategoryName )
106     {
107         if ( logCategoryName != null && !logCategoryName.equals( this.logCategoryName ) )
108         {
109             this.logCategoryName = logCategoryName;
110             log = LogFactory.getLog( logCategoryName );
111         }
112     }
113 }