How to log ehcache cache events in Liferay 7.1

Overview

This article outlines how to configure EHCache cache event logging in Liferay 7.1.

Scenario

We want to log and monitor EHCache cache events in a clustered environment.

This is helpful when monitoring cache replication of data across nodes in a cluster to ensure caches on slave nodes are updated with cache data from the master node.

We can monitor ehcache portal cache events by using the Liferay 7.1 PortalCacheCacheEventListener  with logging configuration for one or many caches.

The PortalCacheCacheEventListener implements an EHCache Cache Event Listener and logs cache events with logging level DEBUG, hence we must enable DEBUG level logging.

Configuration

Configure ehcache cache event logging for all caches

To log all cache events for all caches, add or update the parent portal logger namespace for PortalCacheCacheEventListener.

e.g.

com.liferay.portal.cache.ehcache.internal.event.PortalCacheCacheEventListener = ALL
 

Configure ehcache event logging for a specific cache

To log all cache events for an individual cache, use the following logger name format:

com.liferay.portal.cache.ehcache.internal.event.PortalCacheCacheEventListener.EHCACHE_NAME

where EHCACHE_NAME is the fully-qualified name of your cache.

For example, I want to log all cache events for a cache named "net.mitnet.demo.service.impl.MyServiceImpl".

The ehcache definition for the cache could be similar to the following snippet:

e.g.

<cache
    eternal="false"
    maxElementsInMemory="100"
    name="net.mitnet.demo.service.impl.MyServiceImpl"
    overflowToDisk="false"
    timeToIdleSeconds="600"
/>

The cache logger name will be as follows:

com.liferay.portal.cache.ehcache.internal.event.PortalCacheCacheEventListener.net.mitnet.demo.service.impl.MyServiceImpl

Hence, the portal logger definition will be as follows:

com.liferay.portal.cache.ehcache.internal.event.PortalCacheCacheEventListener.net.mitnet.demo.service.impl.MyServiceImpl = DEBUG

Sample EHCache Cache Event Log Entries

Based on the previous sample cache logger configuration, the portal log could contain cache event entries for cache events propagated across the cluster.

e.g. cache element "Put" event
2020-01-21 16:45:31,920 DEBUG [liferay/cache_replication-2][MyServiceImpl:173] Put SOME_VALUE in net.mitnet.demo.service.impl.MyServiceImpl

e.g. cache element "Update" event
2020-01-21 16:45:31,920 DEBUG [liferay/cache_replication-2][MyServiceImpl:173] Updated SOME_VALUE in net.mitnet.demo.service.impl.MyServiceImpl

e.g. cache element "Removed" event
2020-01-21 16:45:31,920 DEBUG [liferay/cache_replication-2][MyServiceImpl:173] Removed SOME_VALUE in net.mitnet.demo.service.impl.MyServiceImpl

References