Ehcache is using SLF4j logging, to log stuff, put a slf4j implementation in the project classpath, in this example, we use logback.
Tools used :
- Ehcache 2.9
- Maven 3
- logback 1.0.13
1. Project Directory Structure
.
├── build.gradle
├── pom.xml
├── README.md
├── src
│ └── main
│ ├── java
│ │ └── com
│ │ └── favtuts
│ │ └── cache
│ │ └── HelloEhCache.java
│ └── resources
│ └── logback.xml
└── target
2. Project Dependencies
pom.xml
<dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> <version>2.9.0</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.0.13</version> </dependency>
3. Logback.xml
Create a logback.xml
file and put it into the src/main/resources
folder.
logback.xml
<?xml version="1.0" encoding="UTF-8"?> <configuration> //Log everything to console <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <layout class="ch.qos.logback.classic.PatternLayout"> <Pattern> %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n </Pattern> </layout> </appender> <logger name="com.favtuts.cache" level="debug" additivity="false"> <appender-ref ref="STDOUT" /> </logger> <root level="error"> <appender-ref ref="STDOUT" /> </root> </configuration>
4. Logging
A simple Java Ehcache example.
HelloEhCache.java
package com.favtuts.cache; import net.sf.ehcache.Cache; import net.sf.ehcache.CacheManager; import net.sf.ehcache.Element; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class HelloEhCache{ private final static Logger logger = LoggerFactory.getLogger(HelloEhCache.class); public static void main(String[] args) { logger.debug("Starting Ehcache..."); CacheManager cm = CacheManager.getInstance(); cm.addCache("cache1"); Cache cache = cm.getCache("cache1"); cache.put(new Element("1","Jan")); cache.put(new Element("2","Feb")); cache.put(new Element("3","Mar")); logger.debug("cache : {}", cache); Element ele = cache.get("2"); String output = (ele == null ? null : ele.getObjectValue().toString()); System.out.println(output); logger.debug("element : {}", ele); cm.shutdown(); logger.debug("Shutting down Ehcache..."); } }
Output
2022-08-07 11:48:49 [main] DEBUG com.favtuts.cache.HelloEhCache - Starting Ehcache...
2022-08-07 11:48:49 [main] DEBUG com.favtuts.cache.HelloEhCache - cache : [ name = cache1 status = STATUS_ALIVE eternal = false overflowToDisk = true maxEntriesLocalHeap = 10000 maxEntriesLocalDisk = 10000000 memoryStoreEvictionPolicy = LRU timeToLiveSeconds = 120 timeToIdleSeconds = 120 persistence = LOCALTEMPSWAP diskExpiryThreadIntervalSeconds = 120 cacheEventListeners: ; orderedCacheEventListeners: maxBytesLocalHeap = 0 overflowToOffHeap = false maxBytesLocalOffHeap = 0 maxBytesLocalDisk = 0 pinned = false ]
Feb
2022-08-07 11:48:49 [main] DEBUG com.favtuts.cache.HelloEhCache - element : [ key = 2, value=Feb, version=1, hitCount=1, CreationTime = 1659847729243, LastAccessTime = 1659847729244 ]
2022-08-07 11:48:49 [main] DEBUG com.favtuts.cache.HelloEhCache - Shutting down Ehcache...
Changed to total debug mode :
logback.xml
<root level="debug"> <appender-ref ref="STDOUT" /> </root>
Output
1:49:42,587 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
11:49:42,588 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
11:49:42,589 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [file:/home/tuts.heomi.net/github/java-core-tutorials-examples/java-caching/ehcache-logging/target/classes/logback.xml]
11:49:42,683 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - debug attribute not set
11:49:42,686 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
11:49:42,692 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [STDOUT]
11:49:42,776 |-WARN in ch.qos.logback.core.ConsoleAppender[STDOUT] - This appender no longer admits a layout as a sub-component, set an encoder instead.
11:49:42,776 |-WARN in ch.qos.logback.core.ConsoleAppender[STDOUT] - To ensure compatibility, wrapping your layout in LayoutWrappingEncoder.
11:49:42,776 |-WARN in ch.qos.logback.core.ConsoleAppender[STDOUT] - See also http://logback.qos.ch/codes.html#layoutInsteadOfEncoder for details
11:49:42,777 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [com.favtuts.cache] to DEBUG
11:49:42,777 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting additivity of logger [com.favtuts.cache] to false
11:49:42,777 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [STDOUT] to Logger[com.favtuts.cache]
11:49:42,778 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to DEBUG
11:49:42,778 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [STDOUT] to Logger[ROOT]
11:49:42,778 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
11:49:42,779 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@74650e52 - Registering current configuration as safe fallback point
2022-08-07 11:49:42 [main] DEBUG com.favtuts.cache.HelloEhCache - Starting Ehcache...
2022-08-07 11:49:42 [main] WARN n.s.e.config.ConfigurationFactory - No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath: jar:file:/home/favtuts/.m2/repository/net/sf/ehcache/ehcache/2.9.0/ehcache-2.9.0.jar!/ehcache-failsafe.xml
2022-08-07 11:49:42 [main] DEBUG n.s.e.config.ConfigurationFactory - Configuring ehcache from URL: jar:file:/home/favtuts/.m2/repository/net/sf/ehcache/ehcache/2.9.0/ehcache-2.9.0.jar!/ehcache-failsafe.xml
2022-08-07 11:49:42 [main] DEBUG n.s.e.config.ConfigurationFactory - Configuring ehcache from InputStream
2022-08-07 11:49:42 [main] DEBUG net.sf.ehcache.config.BeanHandler - Ignoring ehcache attribute xmlns:xsi
2022-08-07 11:49:42 [main] DEBUG net.sf.ehcache.config.BeanHandler - Ignoring ehcache attribute xsi:noNamespaceSchemaLocation
2022-08-07 11:49:42 [main] DEBUG n.s.e.config.DiskStoreConfiguration - Disk Store Path: /tmp
2022-08-07 11:49:42 [main] DEBUG net.sf.ehcache.CacheManager - Creating new CacheManager with default config
2022-08-07 11:49:42 [main] DEBUG net.sf.ehcache.util.PropertyUtil - propertiesString is null.
2022-08-07 11:49:42 [main] DEBUG n.s.e.config.ConfigurationHelper - No CacheManagerEventListenerFactory class specified. Skipping...
2022-08-07 11:49:42 [main] DEBUG net.sf.ehcache.Cache - No BootstrapCacheLoaderFactory class specified. Skipping...
2022-08-07 11:49:42 [main] DEBUG net.sf.ehcache.Cache - CacheWriter factory not configured. Skipping...
2022-08-07 11:49:42 [main] DEBUG n.s.e.config.ConfigurationHelper - No CacheExceptionHandlerFactory class specified. Skipping...
2022-08-07 11:49:42 [main] DEBUG net.sf.ehcache.DiskStorePathManager - Using diskstore path /tmp
2022-08-07 11:49:42 [main] DEBUG net.sf.ehcache.DiskStorePathManager - Holding exclusive lock on /tmp/.ehcache-diskstore.lock
2022-08-07 11:49:42 [main] DEBUG n.s.e.store.disk.DiskStorageFactory - Failed to delete file cache1.data
2022-08-07 11:49:42 [main] DEBUG n.s.e.store.disk.DiskStorageFactory - Failed to delete file cache1.index
2022-08-07 11:49:42 [main] DEBUG n.s.e.store.disk.DiskStorageFactory - Matching data file missing (or empty) for index file. Deleting index file /tmp/cache1.index
2022-08-07 11:49:42 [main] DEBUG n.s.e.store.disk.DiskStorageFactory - Failed to delete file cache1.index
2022-08-07 11:49:43 [main] DEBUG n.s.e.s.e.ExtendedStatisticsImpl - Mocking Pass-Through Statistic: LOCAL_OFFHEAP_SIZE
2022-08-07 11:49:43 [main] DEBUG n.s.e.s.e.ExtendedStatisticsImpl - Mocking Pass-Through Statistic: LOCAL_OFFHEAP_SIZE_BYTES
2022-08-07 11:49:43 [main] DEBUG n.s.e.s.e.ExtendedStatisticsImpl - Mocking Pass-Through Statistic: WRITER_QUEUE_LENGTH
2022-08-07 11:49:43 [main] DEBUG n.s.e.s.e.ExtendedStatisticsImpl - Mocking Pass-Through Statistic: REMOTE_SIZE
2022-08-07 11:49:43 [main] DEBUG n.s.e.s.e.ExtendedStatisticsImpl - Mocking Pass-Through Statistic: LAST_REJOIN_TIMESTAMP
2022-08-07 11:49:43 [main] DEBUG n.s.e.s.e.ExtendedStatisticsImpl - Mocking Operation Statistic: OFFHEAP_GET
2022-08-07 11:49:43 [main] DEBUG n.s.e.s.e.ExtendedStatisticsImpl - Mocking Operation Statistic: OFFHEAP_PUT
2022-08-07 11:49:43 [main] DEBUG n.s.e.s.e.ExtendedStatisticsImpl - Mocking Operation Statistic: OFFHEAP_REMOVE
2022-08-07 11:49:43 [main] DEBUG n.s.e.s.e.ExtendedStatisticsImpl - Mocking Operation Statistic: XA_COMMIT
2022-08-07 11:49:43 [main] DEBUG n.s.e.s.e.ExtendedStatisticsImpl - Mocking Operation Statistic: XA_ROLLBACK
2022-08-07 11:49:43 [main] DEBUG n.s.e.s.e.ExtendedStatisticsImpl - Mocking Operation Statistic: XA_RECOVERY
2022-08-07 11:49:43 [main] DEBUG n.s.e.s.e.ExtendedStatisticsImpl - Mocking Operation Statistic: CLUSTER_EVENT
2022-08-07 11:49:43 [main] DEBUG n.s.e.s.e.ExtendedStatisticsImpl - Mocking Operation Statistic: NONSTOP
2022-08-07 11:49:43 [main] DEBUG net.sf.ehcache.Cache - Initialised cache: cache1
2022-08-07 11:49:43 [main] DEBUG n.s.e.config.ConfigurationHelper - CacheDecoratorFactory not configured for defaultCache. Skipping for 'cache1'.
2022-08-07 11:49:43 [main] DEBUG net.sf.ehcache.store.disk.Segment - put added 0 on heap
2022-08-07 11:49:43 [main] DEBUG net.sf.ehcache.store.disk.Segment - put added 0 on heap
2022-08-07 11:49:43 [main] DEBUG net.sf.ehcache.store.disk.Segment - put added 0 on heap
2022-08-07 11:49:43 [main] DEBUG com.favtuts.cache.HelloEhCache - cache : [ name = cache1 status = STATUS_ALIVE eternal = false overflowToDisk = true maxEntriesLocalHeap = 10000 maxEntriesLocalDisk = 10000000 memoryStoreEvictionPolicy = LRU timeToLiveSeconds = 120 timeToIdleSeconds = 120 persistence = LOCALTEMPSWAP diskExpiryThreadIntervalSeconds = 120 cacheEventListeners: ; orderedCacheEventListeners: maxBytesLocalHeap = 0 overflowToOffHeap = false maxBytesLocalOffHeap = 0 maxBytesLocalDisk = 0 pinned = false ]
Feb
2022-08-07 11:49:43 [main] DEBUG com.favtuts.cache.HelloEhCache - element : [ key = 2, value=Feb, version=1, hitCount=1, CreationTime = 1659847783059, LastAccessTime = 1659847783061 ]
2022-08-07 11:49:43 [main] DEBUG net.sf.ehcache.store.disk.Segment - fault removed 0 from heap
2022-08-07 11:49:43 [main] DEBUG net.sf.ehcache.store.disk.Segment - fault added 0 on disk
2022-08-07 11:49:43 [cache1.data] DEBUG net.sf.ehcache.store.disk.Segment - fault removed 0 from heap
2022-08-07 11:49:43 [cache1.data] DEBUG net.sf.ehcache.store.disk.Segment - fault added 0 on disk
2022-08-07 11:49:43 [cache1.data] DEBUG net.sf.ehcache.store.disk.Segment - fault installation failed, deleted 0 from heap
2022-08-07 11:49:43 [cache1.data] DEBUG net.sf.ehcache.store.disk.Segment - fault installation failed deleted 0 from disk
2022-08-07 11:49:43 [cache1.data] DEBUG net.sf.ehcache.store.disk.Segment - fault removed 0 from heap
2022-08-07 11:49:43 [cache1.data] DEBUG net.sf.ehcache.store.disk.Segment - fault added 0 on disk
2022-08-07 11:49:43 [main] DEBUG net.sf.ehcache.store.disk.Segment - fault removed 0 from heap
2022-08-07 11:49:43 [main] DEBUG net.sf.ehcache.store.disk.Segment - fault added 0 on disk
2022-08-07 11:49:43 [cache1.data] DEBUG net.sf.ehcache.store.disk.Segment - fault removed 0 from heap
2022-08-07 11:49:43 [cache1.data] DEBUG net.sf.ehcache.store.disk.Segment - fault added 0 on disk
2022-08-07 11:49:43 [cache1.data] DEBUG net.sf.ehcache.store.disk.Segment - fault installation failed, deleted 0 from heap
2022-08-07 11:49:43 [cache1.data] DEBUG net.sf.ehcache.store.disk.Segment - fault installation failed deleted 0 from disk
2022-08-07 11:49:43 [main] DEBUG com.favtuts.cache.HelloEhCache - Shutting down Ehcache...
Download Source Code
$ git clone https://github.com/favtuts/java-core-tutorials-examples.git
$ cd java-caching/ehcache-logging