Skip to content

Commit e5a295b

Browse files
Guard potentially expensive log message computations.
Original Pull Request #3883
1 parent f7cbd42 commit e5a295b

File tree

9 files changed

+66
-27
lines changed

9 files changed

+66
-27
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/ServerAddressPropertyEditor.java

+12-4
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,18 @@ public void setAsText(@Nullable String replicaSetString) {
8888
private ServerAddress parseServerAddress(String source) {
8989

9090
if (!StringUtils.hasText(source)) {
91-
LOG.warn(String.format(COULD_NOT_PARSE_ADDRESS_MESSAGE, "source", source));
91+
if(LOG.isWarnEnabled()) {
92+
LOG.warn(String.format(COULD_NOT_PARSE_ADDRESS_MESSAGE, "source", source));
93+
}
9294
return null;
9395
}
9496

9597
String[] hostAndPort = extractHostAddressAndPort(source.trim());
9698

9799
if (hostAndPort.length > 2) {
98-
LOG.warn(String.format(COULD_NOT_PARSE_ADDRESS_MESSAGE, "source", source));
100+
if(LOG.isWarnEnabled()) {
101+
LOG.warn(String.format(COULD_NOT_PARSE_ADDRESS_MESSAGE, "source", source));
102+
}
99103
return null;
100104
}
101105

@@ -105,9 +109,13 @@ private ServerAddress parseServerAddress(String source) {
105109

106110
return port == null ? new ServerAddress(hostAddress) : new ServerAddress(hostAddress, port);
107111
} catch (UnknownHostException e) {
108-
LOG.warn(String.format(COULD_NOT_PARSE_ADDRESS_MESSAGE, "host", hostAndPort[0]));
112+
if(LOG.isWarnEnabled()) {
113+
LOG.warn(String.format(COULD_NOT_PARSE_ADDRESS_MESSAGE, "host", hostAndPort[0]));
114+
}
109115
} catch (NumberFormatException e) {
110-
LOG.warn(String.format(COULD_NOT_PARSE_ADDRESS_MESSAGE, "port", hostAndPort[1]));
116+
if(LOG.isWarnEnabled()) {
117+
LOG.warn(String.format(COULD_NOT_PARSE_ADDRESS_MESSAGE, "port", hostAndPort[1]));
118+
}
111119
}
112120

113121
return null;

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexCreator.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,10 @@ private IndexInfo fetchIndexInformation(@Nullable IndexDefinitionHolder indexDef
208208
orElse(null);
209209

210210
} catch (Exception e) {
211-
LOGGER.debug(
212-
String.format("Failed to load index information for collection '%s'.", indexDefinition.getCollection()), e);
211+
if(LOGGER.isDebugEnabled()) {
212+
LOGGER.debug(
213+
String.format("Failed to load index information for collection '%s'.", indexDefinition.getCollection()), e);
214+
}
213215
}
214216

215217
return null;

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolver.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,9 @@ private void potentiallyAddIndexForProperty(MongoPersistentEntity<?> root, Mongo
170170
indexes.addAll(indexDefinitions);
171171
}
172172
} catch (CyclicPropertyReferenceException e) {
173-
LOGGER.info(e.getMessage());
173+
if(LOGGER.isInfoEnabled()) {
174+
LOGGER.info(e.getMessage());
175+
}
174176
}
175177
}
176178

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/ReactiveMongoPersistentEntityIndexCreator.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,11 @@ private Mono<IndexInfo> fetchIndexInformation(IndexDefinitionHolder indexDefinit
175175
.filter(indexInfo -> ObjectUtils.nullSafeEquals(indexNameToLookUp, indexInfo.getName())) //
176176
.next() //
177177
.doOnError(e -> {
178-
LOGGER.debug(
179-
String.format("Failed to load index information for collection '%s'.", indexDefinition.getCollection()),
180-
e);
178+
if(LOGGER.isDebugEnabled()) {
179+
LOGGER.debug(
180+
String.format("Failed to load index information for collection '%s'.", indexDefinition.getCollection()),
181+
e);
182+
}
181183
});
182184
}
183185

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/BasicMongoPersistentProperty.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,11 @@ public BasicMongoPersistentProperty(Property property, MongoPersistentEntity<?>
9393

9494
String annotatedName = getAnnotatedFieldName();
9595
if (!ID_FIELD_NAME.equals(annotatedName)) {
96-
LOG.warn(String.format(
97-
"Customizing field name for id property '%s.%s' is not allowed! Custom name ('%s') will not be considered!",
98-
owner.getName(), getName(), annotatedName));
96+
if(LOG.isWarnEnabled()) {
97+
LOG.warn(String.format(
98+
"Customizing field name for id property '%s.%s' is not allowed! Custom name ('%s') will not be considered!",
99+
owner.getName(), getName(), annotatedName));
100+
}
99101
}
100102
}
101103
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/event/LoggingEventListener.java

+21-7
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ public class LoggingEventListener extends AbstractMongoEventListener<Object> {
4040
*/
4141
@Override
4242
public void onBeforeConvert(BeforeConvertEvent<Object> event) {
43-
LOGGER.info(String.format("onBeforeConvert: %s", event.getSource()));
43+
if(LOGGER.isInfoEnabled()) {
44+
LOGGER.info(String.format("onBeforeConvert: %s", event.getSource()));
45+
}
4446
}
4547

4648
/*
@@ -49,7 +51,9 @@ public void onBeforeConvert(BeforeConvertEvent<Object> event) {
4951
*/
5052
@Override
5153
public void onBeforeSave(BeforeSaveEvent<Object> event) {
52-
LOGGER.info(String.format("onBeforeSave: %s, %s", event.getSource(), serializeToJsonSafely(event.getDocument())));
54+
if(LOGGER.isInfoEnabled()) {
55+
LOGGER.info(String.format("onBeforeSave: %s, %s", event.getSource(), serializeToJsonSafely(event.getDocument())));
56+
}
5357
}
5458

5559
/*
@@ -58,7 +62,9 @@ public void onBeforeSave(BeforeSaveEvent<Object> event) {
5862
*/
5963
@Override
6064
public void onAfterSave(AfterSaveEvent<Object> event) {
61-
LOGGER.info(String.format("onAfterSave: %s, %s", event.getSource(), serializeToJsonSafely(event.getDocument())));
65+
if(LOGGER.isInfoEnabled()) {
66+
LOGGER.info(String.format("onAfterSave: %s, %s", event.getSource(), serializeToJsonSafely(event.getDocument())));
67+
}
6268
}
6369

6470
/*
@@ -67,7 +73,9 @@ public void onAfterSave(AfterSaveEvent<Object> event) {
6773
*/
6874
@Override
6975
public void onAfterLoad(AfterLoadEvent<Object> event) {
70-
LOGGER.info(String.format("onAfterLoad: %s", serializeToJsonSafely(event.getDocument())));
76+
if(LOGGER.isInfoEnabled()) {
77+
LOGGER.info(String.format("onAfterLoad: %s", serializeToJsonSafely(event.getDocument())));
78+
}
7179
}
7280

7381
/*
@@ -76,7 +84,9 @@ public void onAfterLoad(AfterLoadEvent<Object> event) {
7684
*/
7785
@Override
7886
public void onAfterConvert(AfterConvertEvent<Object> event) {
79-
LOGGER.info(String.format("onAfterConvert: %s, %s", serializeToJsonSafely(event.getDocument()), event.getSource()));
87+
if(LOGGER.isInfoEnabled()) {
88+
LOGGER.info(String.format("onAfterConvert: %s, %s", serializeToJsonSafely(event.getDocument()), event.getSource()));
89+
}
8090
}
8191

8292
/*
@@ -85,7 +95,9 @@ public void onAfterConvert(AfterConvertEvent<Object> event) {
8595
*/
8696
@Override
8797
public void onAfterDelete(AfterDeleteEvent<Object> event) {
88-
LOGGER.info(String.format("onAfterDelete: %s", serializeToJsonSafely(event.getDocument())));
98+
if(LOGGER.isInfoEnabled()) {
99+
LOGGER.info(String.format("onAfterDelete: %s", serializeToJsonSafely(event.getDocument())));
100+
}
89101
}
90102

91103
/*
@@ -94,6 +106,8 @@ public void onAfterDelete(AfterDeleteEvent<Object> event) {
94106
*/
95107
@Override
96108
public void onBeforeDelete(BeforeDeleteEvent<Object> event) {
97-
LOGGER.info(String.format("onBeforeDelete: %s", serializeToJsonSafely(event.getDocument())));
109+
if(LOGGER.isInfoEnabled()) {
110+
LOGGER.info(String.format("onBeforeDelete: %s", serializeToJsonSafely(event.getDocument())));
111+
}
98112
}
99113
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/event/ValidatingMongoEventListener.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,14 @@ public ValidatingMongoEventListener(Validator validator) {
5959
public void onBeforeSave(BeforeSaveEvent<Object> event) {
6060

6161
if (LOG.isDebugEnabled()) {
62-
LOG.debug(String.format("Validating object: {}", event.getSource()));
62+
LOG.debug(String.format("Validating object: %s", event.getSource()));
6363
}
6464
Set violations = validator.validate(event.getSource());
6565

6666
if (!violations.isEmpty()) {
6767

6868
if (LOG.isDebugEnabled()) {
69-
LOG.info(String.format("During object: {} validation violations found: {}", event.getSource(), violations));
69+
LOG.info(String.format("During object: %s validation violations found: %s", event.getSource(), violations));
7070
}
7171
throw new ConstraintViolationException(violations);
7272
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/IndexEnsuringQueryCreationListener.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,10 @@ public void onCreation(PartTreeMongoQuery query) {
136136
}
137137
}
138138
}
139-
LOG.debug(String.format("Created %s!", index));
139+
140+
if (LOG.isDebugEnabled()) {
141+
LOG.debug(String.format("Created %s!", index));
142+
}
140143
}
141144

142145
public boolean isIndexOnUnwrappedType(Part part) {

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/test/util/CleanMongoDB.java

+10-4
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,9 @@ private boolean dropDbIfRequired(String dbName) {
289289
}
290290

291291
client.getDatabase(dbName).drop();
292-
LOGGER.debug(String.format("Dropping DB '%s'. ", dbName));
292+
if(LOGGER.isDebugEnabled()) {
293+
LOGGER.debug(String.format("Dropping DB '%s'. ", dbName));
294+
}
293295
return true;
294296
}
295297

@@ -306,11 +308,15 @@ private void dropCollectionsOrIndexIfRequried(MongoDatabase db, Collection<Strin
306308

307309
if (types.contains(Struct.COLLECTION)) {
308310
collection.drop();
309-
LOGGER.debug(String.format("Dropping collection '%s' for DB '%s'. ", collectionName, db.getName()));
311+
if(LOGGER.isDebugEnabled()) {
312+
LOGGER.debug(String.format("Dropping collection '%s' for DB '%s'. ", collectionName, db.getName()));
313+
}
310314
} else if (types.contains(Struct.INDEX)) {
311315
collection.dropIndexes();
312-
LOGGER.debug(
313-
String.format("Dropping indexes in collection '%s' for DB '%s'. ", collectionName, db.getName()));
316+
if(LOGGER.isDebugEnabled()) {
317+
LOGGER.debug(
318+
String.format("Dropping indexes in collection '%s' for DB '%s'. ", collectionName, db.getName()));
319+
}
314320
}
315321
}
316322
}

0 commit comments

Comments
 (0)