001package org.apache.commons.jcs.engine.logging;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.commons.jcs.engine.logging.behavior.ICacheEvent;
023import org.apache.commons.jcs.engine.logging.behavior.ICacheEventLogger;
024import org.apache.commons.logging.Log;
025import org.apache.commons.logging.LogFactory;
026
027/**
028 * This implementation simple logs to a commons logger at debug level, for all events. It's mainly
029 * for testing. It isn't very useful otherwise.
030 */
031public class CacheEventLoggerDebugLogger
032    implements ICacheEventLogger
033{
034    /** This is the name of the category. */
035    private String logCategoryName = CacheEventLoggerDebugLogger.class.getName();
036
037    /** The logger. This is recreated on set logCategoryName */
038    private Log log = LogFactory.getLog( logCategoryName );
039
040    /**
041     * @param source
042     * @param region
043     * @param eventName
044     * @param optionalDetails
045     * @param key
046     * @return ICacheEvent
047     */
048    @Override
049    public <T> ICacheEvent<T> createICacheEvent( String source, String region, String eventName,
050            String optionalDetails, T key )
051    {
052        ICacheEvent<T> event = new CacheEvent<T>();
053        event.setSource( source );
054        event.setRegion( region );
055        event.setEventName( eventName );
056        event.setOptionalDetails( optionalDetails );
057        event.setKey( key );
058
059        return event;
060    }
061
062    /**
063     * @param source
064     * @param eventName
065     * @param optionalDetails
066     */
067    @Override
068    public void logApplicationEvent( String source, String eventName, String optionalDetails )
069    {
070        if ( log.isDebugEnabled() )
071        {
072            log.debug( source + " | " + eventName + " | " + optionalDetails );
073        }
074    }
075
076    /**
077     * @param source
078     * @param eventName
079     * @param errorMessage
080     */
081    @Override
082    public void logError( String source, String eventName, String errorMessage )
083    {
084        if ( log.isDebugEnabled() )
085        {
086            log.debug( source + " | " + eventName + " | " + errorMessage );
087        }
088    }
089
090    /**
091     * @param event
092     */
093    @Override
094    public <T> void logICacheEvent( ICacheEvent<T> event )
095    {
096        if ( log.isDebugEnabled() )
097        {
098            log.debug( event );
099        }
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}