Skip to content

Commit 7bcf234

Browse files
committed
use typesafe logging for all L2 cache stuff
1 parent 87627eb commit 7bcf234

File tree

4 files changed

+121
-57
lines changed

4 files changed

+121
-57
lines changed

hibernate-core/src/main/java/org/hibernate/cache/internal/CollectionCacheInvalidator.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@
3232

3333
import org.jboss.logging.Logger;
3434

35+
import static org.hibernate.cache.spi.SecondLevelCacheLogger.L2CACHE_LOGGER;
3536
import static org.hibernate.internal.util.StringHelper.isEmpty;
3637
import static org.hibernate.internal.util.collections.CollectionHelper.isEmpty;
38+
import static org.hibernate.pretty.MessageHelper.collectionInfoString;
3739

3840
/**
3941
* Allows the collection cache to be automatically evicted if an element is inserted/removed/updated *without* properly
@@ -127,8 +129,8 @@ private void evictCollection(
127129
handleInverseOneToMany( entity, persister, oldState, collectionPersister, session );
128130
}
129131
else {
130-
if ( LOG.isTraceEnabled() ) {
131-
LOG.trace( "Evict CollectionRegion " + collectionPersister.getRole() );
132+
if ( L2CACHE_LOGGER.isTraceEnabled() ) {
133+
L2CACHE_LOGGER.autoEvictingCollectionCacheByRole( collectionPersister.getRole() );
132134
}
133135
final CollectionDataAccess cacheAccessStrategy = collectionPersister.getCacheAccessStrategy();
134136
final SoftLock softLock = cacheAccessStrategy.lockRegion();
@@ -184,8 +186,9 @@ private Object getIdentifier(SharedSessionContractImplementor session, Object ob
184186
}
185187

186188
private void evict(Object id, CollectionPersister collectionPersister, EventSource session) {
187-
if ( LOG.isTraceEnabled() ) {
188-
LOG.trace( "Evict CollectionRegion " + collectionPersister.getRole() + " for id " + id );
189+
if ( L2CACHE_LOGGER.isTraceEnabled() ) {
190+
L2CACHE_LOGGER.autoEvictingCollectionCache(
191+
collectionInfoString( collectionPersister, id, collectionPersister.getFactory() ) );
189192
}
190193
final CollectionEvictCacheAction evictCacheAction =
191194
new CollectionEvictCacheAction( collectionPersister, null, id, session );

hibernate-core/src/main/java/org/hibernate/cache/internal/EnabledCaching.java

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,11 @@
3434
import org.hibernate.cache.spi.access.EntityDataAccess;
3535
import org.hibernate.cache.spi.access.NaturalIdDataAccess;
3636
import org.hibernate.engine.spi.SessionFactoryImplementor;
37-
import org.hibernate.internal.CoreLogging;
38-
import org.hibernate.internal.CoreMessageLogger;
3937
import org.hibernate.metamodel.model.domain.NavigableRole;
4038
import org.hibernate.metamodel.spi.MappingMetamodelImplementor;
4139
import org.hibernate.persister.collection.CollectionPersister;
4240
import org.hibernate.persister.entity.EntityPersister;
41+
import org.hibernate.cache.spi.SecondLevelCacheLogger;
4342

4443
import static org.hibernate.internal.util.StringHelper.qualifyConditionally;
4544
import static org.hibernate.pretty.MessageHelper.collectionInfoString;
@@ -53,7 +52,7 @@
5352
* @author Gail Badner
5453
*/
5554
public class EnabledCaching implements CacheImplementor, DomainDataRegionBuildingContext {
56-
private static final CoreMessageLogger LOG = CoreLogging.messageLogger( EnabledCaching.class );
55+
private static final SecondLevelCacheLogger LOG = SecondLevelCacheLogger.L2CACHE_LOGGER;
5756

5857
private final SessionFactoryImplementor sessionFactory;
5958
private final RegionFactory regionFactory;
@@ -259,10 +258,7 @@ public void evictEntityData(String entityName, Object identifier) {
259258
final EntityPersister persister = getEntityDescriptor( entityName );
260259
final EntityDataAccess cacheAccess = persister.getCacheAccessStrategy();
261260
if ( cacheAccess != null ) {
262-
if ( LOG.isTraceEnabled() ) {
263-
LOG.trace( "Evicting entity second-level cache: "
264-
+ infoString( persister, identifier, sessionFactory ) );
265-
}
261+
LOG.evictingEntityCache( infoString( persister, identifier, sessionFactory ) );
266262

267263
final Object cacheKey =
268264
cacheAccess.generateCacheKey( identifier, persister, sessionFactory, null );
@@ -305,9 +301,7 @@ protected void evictEntityData(EntityPersister entityDescriptor) {
305301

306302
private void evictEntityData(NavigableRole navigableRole, EntityDataAccess cacheAccess) {
307303
if ( cacheAccess != null ) {
308-
if ( LOG.isTraceEnabled() ) {
309-
LOG.trace( "Evicting entity second-level cache: " + navigableRole.getFullPath() );
310-
}
304+
LOG.evictingEntityCacheByRole( navigableRole.getFullPath() );
311305
cacheAccess.evictAll();
312306
}
313307
}
@@ -344,9 +338,7 @@ public void evictNaturalIdData() {
344338

345339
private void evictNaturalIdData(NavigableRole rootEntityRole, NaturalIdDataAccess cacheAccess) {
346340
if ( cacheAccess != null ) {
347-
if ( LOG.isTraceEnabled() ) {
348-
LOG.trace( "Evicting natural-id cache: " + rootEntityRole.getFullPath() );
349-
}
341+
LOG.evictingNaturalIdCache( rootEntityRole.getFullPath() );
350342
cacheAccess.evictAll();
351343
}
352344
}
@@ -375,10 +367,7 @@ public void evictCollectionData(String role, Object ownerIdentifier) {
375367
final CollectionPersister persister = getCollectionDescriptor( role );
376368
final CollectionDataAccess cacheAccess = persister.getCacheAccessStrategy();
377369
if ( cacheAccess != null ) {
378-
if ( LOG.isTraceEnabled() ) {
379-
LOG.trace( "Evicting collection second-level cache: "
380-
+ collectionInfoString( persister, ownerIdentifier, sessionFactory ) );
381-
}
370+
LOG.evictingCollectionCache( collectionInfoString( persister, ownerIdentifier, sessionFactory ) );
382371

383372
final Object cacheKey =
384373
cacheAccess.generateCacheKey( ownerIdentifier, persister, sessionFactory, null );
@@ -398,9 +387,7 @@ private void evictCollectionData(CollectionPersister collectionDescriptor) {
398387

399388
private void evictCollectionData(NavigableRole navigableRole, CollectionDataAccess cacheAccess) {
400389
if ( cacheAccess != null ) {
401-
if ( LOG.isTraceEnabled() ) {
402-
LOG.trace( "Evicting collection second-level cache: " + navigableRole.getFullPath() );
403-
}
390+
LOG.evictingCollectionCacheByRole( navigableRole.getFullPath() );
404391
cacheAccess.evictAll();
405392
}
406393
}
@@ -435,18 +422,14 @@ public void evictQueryRegion(String regionName) {
435422

436423
private void evictQueryResultRegion(QueryResultsCache cache) {
437424
if ( cache != null ) {
438-
if ( LOG.isTraceEnabled() ) {
439-
LOG.trace( "Evicting query cache region: " + cache.getRegion().getName() );
440-
}
425+
LOG.evictingQueryCacheRegion( cache.getRegion().getName() );
441426
cache.clear();
442427
}
443428
}
444429

445430
@Override
446431
public void evictQueryRegions() {
447-
if ( LOG.isTraceEnabled() ) {
448-
LOG.trace( "Evicting cache of all query regions" );
449-
}
432+
LOG.evictingAllQueryRegions();
450433

451434
evictQueryResultRegion( defaultQueryResultsCache );
452435
for ( QueryResultsCache cache : namedQueryResultsCacheMap.values() ) {

hibernate-core/src/main/java/org/hibernate/cache/internal/TimestampsCacheEnabledImpl.java

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,16 @@
1616
import org.hibernate.event.monitor.spi.DiagnosticEvent;
1717
import org.hibernate.stat.spi.StatisticsImplementor;
1818

19-
import org.jboss.logging.Logger;
19+
import static org.hibernate.cache.spi.SecondLevelCacheLogger.L2CACHE_LOGGER;
20+
import static org.hibernate.event.monitor.spi.EventMonitor.CacheActionDescription.TIMESTAMP_INVALIDATE;
21+
import static org.hibernate.event.monitor.spi.EventMonitor.CacheActionDescription.TIMESTAMP_PRE_INVALIDATE;
2022

2123
/**
2224
* Standard implementation of TimestampsCache
2325
*
2426
* @author Steve Ebersole
2527
*/
2628
public class TimestampsCacheEnabledImpl implements TimestampsCache {
27-
private static final Logger log = Logger.getLogger( TimestampsCacheEnabledImpl.class );
28-
29-
public static final boolean TRACE_ENABLED = log.isTraceEnabled();
3029

3130
private final TimestampsRegion timestampsRegion;
3231

@@ -45,20 +44,18 @@ public void preInvalidate(
4544
SharedSessionContractImplementor session) {
4645
final SessionFactoryImplementor factory = session.getFactory();
4746
final RegionFactory regionFactory = factory.getCache().getRegionFactory();
48-
4947
final StatisticsImplementor statistics = factory.getStatistics();
5048
final boolean stats = statistics.isStatisticsEnabled();
5149

5250
final Long timestamp = regionFactory.nextTimestamp() + regionFactory.getTimeout();
5351

5452
final SessionEventListenerManager eventListenerManager = session.getEventListenerManager();
55-
final boolean traceEnabled = log.isTraceEnabled();
56-
53+
final EventMonitor eventMonitor = session.getEventMonitor();
54+
final boolean traceEnabled = L2CACHE_LOGGER.isTraceEnabled();
5755
for ( String space : spaces ) {
5856
if ( traceEnabled ) {
59-
log.tracef( "Pre-invalidating space [%s], timestamp: %s", space, timestamp );
57+
L2CACHE_LOGGER.preInvalidatingSpace( space, timestamp );
6058
}
61-
final EventMonitor eventMonitor = session.getEventMonitor();
6259
final DiagnosticEvent cachePutEvent = eventMonitor.beginCachePutEvent();
6360
try {
6461
eventListenerManager.cachePutStart();
@@ -73,7 +70,7 @@ public void preInvalidate(
7370
session,
7471
timestampsRegion,
7572
true,
76-
EventMonitor.CacheActionDescription.TIMESTAMP_PRE_INVALIDATE
73+
TIMESTAMP_PRE_INVALIDATE
7774
);
7875
eventListenerManager.cachePutEnd();
7976
}
@@ -88,31 +85,32 @@ public void preInvalidate(
8885
public void invalidate(
8986
String[] spaces,
9087
SharedSessionContractImplementor session) {
91-
final StatisticsImplementor statistics = session.getFactory().getStatistics();
88+
final SessionFactoryImplementor factory = session.getFactory();
89+
final StatisticsImplementor statistics = factory.getStatistics();
9290
final boolean stats = statistics.isStatisticsEnabled();
9391

94-
final Long ts = session.getFactory().getCache().getRegionFactory().nextTimestamp();
95-
final boolean traceEnabled = log.isTraceEnabled();
92+
final Long timestamp = factory.getCache().getRegionFactory().nextTimestamp();
9693

94+
final SessionEventListenerManager eventListenerManager = session.getEventListenerManager();
95+
final EventMonitor eventMonitor = session.getEventMonitor();
96+
final boolean traceEnabled = L2CACHE_LOGGER.isTraceEnabled();
9797
for ( String space : spaces ) {
9898
if ( traceEnabled ) {
99-
log.tracef( "Invalidating space [%s], timestamp: %s", space, ts );
99+
L2CACHE_LOGGER.invalidatingSpace( space, timestamp );
100100
}
101101

102-
final SessionEventListenerManager eventListenerManager = session.getEventListenerManager();
103-
final EventMonitor eventMonitor = session.getEventMonitor();
104102
final DiagnosticEvent cachePutEvent = eventMonitor.beginCachePutEvent();
105103
try {
106104
eventListenerManager.cachePutStart();
107-
timestampsRegion.putIntoCache( space, ts, session );
105+
timestampsRegion.putIntoCache( space, timestamp, session );
108106
}
109107
finally {
110108
eventMonitor.completeCachePutEvent(
111109
cachePutEvent,
112110
session,
113111
timestampsRegion,
114112
true,
115-
EventMonitor.CacheActionDescription.TIMESTAMP_INVALIDATE
113+
TIMESTAMP_INVALIDATE
116114
);
117115
eventListenerManager.cachePutEnd();
118116

@@ -152,13 +150,7 @@ private boolean isSpaceOutOfDate(
152150
return false;
153151
}
154152
else {
155-
if ( TRACE_ENABLED ) {
156-
log.tracef(
157-
"[%s] last update timestamp: %s",
158-
space,
159-
lastUpdate + ", result set timestamp: " + timestamp
160-
);
161-
}
153+
L2CACHE_LOGGER.lastUpdateTimestampForSpace( space, lastUpdate, timestamp );
162154
if ( statistics.isStatisticsEnabled() ) {
163155
statistics.updateTimestampsCacheHit();
164156
}

hibernate-core/src/main/java/org/hibernate/cache/spi/SecondLevelCacheLogger.java

Lines changed: 88 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,94 @@ public interface SecondLevelCacheLogger extends BasicLogger {
131131

132132
@LogMessage(level = TRACE)
133133
@Message(
134-
value = "Returning cached query results",
135-
id = NAMESPACE + 14
134+
value = "Returning cached query results",
135+
id = NAMESPACE + 14
136136
)
137137
void returningCachedQueryResults();
138+
139+
@LogMessage(level = TRACE)
140+
@Message(
141+
value = "Evicting entity second-level cache: %s",
142+
id = NAMESPACE + 15
143+
)
144+
void evictingEntityCache(String entityInfo);
145+
146+
@LogMessage(level = TRACE)
147+
@Message(
148+
value = "Evicting entity second-level cache: %s",
149+
id = NAMESPACE + 16
150+
)
151+
void evictingEntityCacheByRole(String role);
152+
153+
@LogMessage(level = TRACE)
154+
@Message(
155+
value = "Evicting natural id cache: %s",
156+
id = NAMESPACE + 17
157+
)
158+
void evictingNaturalIdCache(String role);
159+
160+
@LogMessage(level = TRACE)
161+
@Message(
162+
value = "Evicting collection second-level cache: %s",
163+
id = NAMESPACE + 18
164+
)
165+
void evictingCollectionCache(String collectionInfo);
166+
167+
@LogMessage(level = TRACE)
168+
@Message(
169+
value = "Evicting collection second-level cache: %s",
170+
id = NAMESPACE + 19
171+
)
172+
void evictingCollectionCacheByRole(String role);
173+
174+
@LogMessage(level = TRACE)
175+
@Message(
176+
value = "Auto-evicting collection from second-level cache: %s"
177+
+ " (since 'hibernate.cache.auto_evict_collection_cache' is enabled)",
178+
id = NAMESPACE + 25
179+
)
180+
void autoEvictingCollectionCache(String collectionInfo);
181+
182+
@LogMessage(level = TRACE)
183+
@Message(
184+
value = "Auto-evicting collection from second-level cache: %s"
185+
+ " (since 'hibernate.cache.auto_evict_collection_cache' is enabled)",
186+
id = NAMESPACE + 26
187+
)
188+
void autoEvictingCollectionCacheByRole(String collectionRole);
189+
190+
@LogMessage(level = TRACE)
191+
@Message(
192+
value = "Evicting query cache region: %s",
193+
id = NAMESPACE + 20
194+
)
195+
void evictingQueryCacheRegion(String regionName);
196+
197+
@LogMessage(level = TRACE)
198+
@Message(
199+
value = "Evicting cache of all query regions",
200+
id = NAMESPACE + 21
201+
)
202+
void evictingAllQueryRegions();
203+
204+
@LogMessage(level = TRACE)
205+
@Message(
206+
value = "Pre-invalidating space [%s], timestamp: %s",
207+
id = NAMESPACE + 22
208+
)
209+
void preInvalidatingSpace(String space, Long timestamp);
210+
211+
@LogMessage(level = TRACE)
212+
@Message(
213+
value = "Invalidating space [%s], timestamp: %s",
214+
id = NAMESPACE + 23
215+
)
216+
void invalidatingSpace(String space, Long timestamp);
217+
218+
@LogMessage(level = TRACE)
219+
@Message(
220+
value = "[%s] last update timestamp: %s, result set timestamp: %s",
221+
id = NAMESPACE + 24
222+
)
223+
void lastUpdateTimestampForSpace(String space, Long lastUpdate, Long timestamp);
138224
}

0 commit comments

Comments
 (0)