001package org.apache.commons.jcs3.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.jcs3.engine.logging.behavior.ICacheEvent;
023import org.apache.commons.jcs3.engine.logging.behavior.ICacheEventLogger;
024import org.apache.commons.jcs3.log.Log;
025import org.apache.commons.jcs3.log.LogManager;
026
027/**
028 * This implementation simple logs to a 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 = LogManager.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( final String source, final String region, final String eventName,
050            final String optionalDetails, final T key )
051    {
052        final ICacheEvent<T> event = new CacheEvent<>();
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( final String source, final String eventName, final String optionalDetails )
069    {
070        log.debug( "{0} | {1} | {2}", source, eventName, optionalDetails );
071    }
072
073    /**
074     * @param source
075     * @param eventName
076     * @param errorMessage
077     */
078    @Override
079    public void logError( final String source, final String eventName, final String errorMessage )
080    {
081        log.debug( "{0} | {1} | {2}", source, eventName, errorMessage );
082    }
083
084    /**
085     * @param event
086     */
087    @Override
088    public <T> void logICacheEvent( final ICacheEvent<T> event )
089    {
090        log.debug( event );
091    }
092
093    /**
094     * @param logCategoryName
095     */
096    public synchronized void setLogCategoryName( final String logCategoryName )
097    {
098        if ( logCategoryName != null && !logCategoryName.equals( this.logCategoryName ) )
099        {
100            this.logCategoryName = logCategoryName;
101            log = LogManager.getLog( logCategoryName );
102        }
103    }
104}