Skip to content

Commit ccf7d1c

Browse files
Added more logging
1 parent f28c4aa commit ccf7d1c

File tree

1 file changed

+76
-15
lines changed

1 file changed

+76
-15
lines changed

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

Lines changed: 76 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
import org.springframework.data.mongodb.core.query.Meta;
100100
import org.springframework.data.mongodb.core.query.NearQuery;
101101
import org.springframework.data.mongodb.core.query.Query;
102+
import org.springframework.data.mongodb.core.query.SerializationUtils;
102103
import org.springframework.data.mongodb.core.query.UpdateDefinition;
103104
import org.springframework.data.mongodb.core.query.UpdateDefinition.ArrayFilter;
104105
import org.springframework.data.mongodb.core.timeseries.Granularity;
@@ -179,6 +180,7 @@
179180
* @author Bartłomiej Mazur
180181
* @author Michael Krog
181182
* @author Jakub Zurawa
183+
* @author Marcin Grzejszczak
182184
*/
183185
public class MongoTemplate
184186
implements MongoOperations, ApplicationContextAware, IndexOperationsProvider, ReadPreferenceAware {
@@ -534,7 +536,7 @@ public Document executeCommand(Document command) {
534536

535537
if (LOGGER.isDebugEnabled()) {
536538
LOGGER.debug(String.format(
537-
"Executing command: [%s]", command.toJson()));
539+
"Executing command: [%s]", serializeToJsonSafely(command)));
538540
}
539541

540542
return execute(db -> db.runCommand(command, Document.class));
@@ -548,7 +550,7 @@ public Document executeCommand(Document command, @Nullable ReadPreference readPr
548550

549551
if (LOGGER.isDebugEnabled()) {
550552
LOGGER.debug(String.format(
551-
"Executing command: [%s]%s", command.toJson(), readPreference != null ? (" with read preference: [" + readPreference + "]") : ""));
553+
"Executing command: [%s]%s", serializeToJsonSafely(command), readPreference != null ? (" with read preference: [" + readPreference + "]") : ""));
552554
}
553555

554556
return execute(db -> readPreference != null //
@@ -3069,8 +3071,8 @@ public Document doInCollection(MongoCollection<Document> collection) throws Mong
30693071

30703072
if (LOGGER.isDebugEnabled()) {
30713073
LOGGER.debug(String.format(
3072-
"Applied callback [%s] query [%s], fields [%s]. Will return first result.", getClass().getSimpleName(), query.toJson(), fields.map(
3073-
Document::toJson).orElse("")));
3074+
"Applied callback [%s] query [%s], fields [%s]", getClass().getSimpleName(), serializeToJsonSafely(query), fields.map(
3075+
SerializationUtils::serializeToJsonSafely).orElse("")));
30743076
}
30753077

30763078
return iterable.first();
@@ -3117,7 +3119,7 @@ public FindIterable<Document> doInCollection(MongoCollection<Document> collectio
31173119

31183120
if (LOGGER.isDebugEnabled()) {
31193121
LOGGER.debug(String.format(
3120-
"Applied callback [%s] query [%s], collation [%s]. Will return first result.", getClass().getSimpleName(), query.toJson(), collation));
3122+
"Applied callback [%s] query [%s], collation [%s]", getClass().getSimpleName(), serializeToJsonSafely(query), serializeToJsonSafely(collation)));
31213123
}
31223124

31233125
return findIterable;
@@ -3147,9 +3149,15 @@ private class ExistsCallback implements CollectionCallback<Boolean> {
31473149

31483150
@Override
31493151
public Boolean doInCollection(MongoCollection<Document> collection) throws MongoException, DataAccessException {
3150-
3151-
return doCount(collectionPreparer, collection.getNamespace().getCollectionName(), mappedQuery,
3152+
boolean positiveCount = doCount(collectionPreparer, collection.getNamespace().getCollectionName(), mappedQuery,
31523153
new CountOptions().limit(1).collation(collation)) > 0;
3154+
3155+
if (LOGGER.isDebugEnabled()) {
3156+
LOGGER.debug(String.format(
3157+
"Applied callback [%s] query [%s], collation [%s]", getClass().getSimpleName(), serializeToJsonSafely(mappedQuery), serializeToJsonSafely(collation)));
3158+
}
3159+
3160+
return positiveCount;
31533161
}
31543162
}
31553163

@@ -3183,7 +3191,14 @@ public Document doInCollection(MongoCollection<Document> collection) throws Mong
31833191
FindOneAndDeleteOptions opts = new FindOneAndDeleteOptions().sort(sort).projection(fields);
31843192
collation.map(Collation::toMongoCollation).ifPresent(opts::collation);
31853193

3186-
return collectionPreparer.prepare(collection).findOneAndDelete(query, opts);
3194+
Document oneAndDelete = collectionPreparer.prepare(collection).findOneAndDelete(query, opts);
3195+
3196+
if (LOGGER.isDebugEnabled()) {
3197+
LOGGER.debug(String.format(
3198+
"Applied callback [%s] query [%s], fields [%s], sort [%s], collation [%s]", getClass().getSimpleName(), serializeToJsonSafely(query), serializeToJsonSafely(fields), serializeToJsonSafely(sort), serializeToJsonSafely(collation)));
3199+
}
3200+
3201+
return oneAndDelete;
31873202
}
31883203
}
31893204

@@ -3228,13 +3243,21 @@ public Document doInCollection(MongoCollection<Document> collection) throws Mong
32283243
opts.arrayFilters(arrayFilters);
32293244
}
32303245

3246+
Document result;
32313247
if (update instanceof Document document) {
3232-
return collectionPreparer.prepare(collection).findOneAndUpdate(query, document, opts);
3248+
result = collectionPreparer.prepare(collection).findOneAndUpdate(query, document, opts);
32333249
} else if (update instanceof List) {
3234-
return collectionPreparer.prepare(collection).findOneAndUpdate(query, (List<Document>) update, opts);
3250+
result = collectionPreparer.prepare(collection).findOneAndUpdate(query, (List<Document>) update, opts);
3251+
} else {
3252+
throw new IllegalArgumentException(String.format("Using %s is not supported in findOneAndUpdate", update));
32353253
}
32363254

3237-
throw new IllegalArgumentException(String.format("Using %s is not supported in findOneAndUpdate", update));
3255+
if (LOGGER.isDebugEnabled()) {
3256+
LOGGER.debug(String.format(
3257+
"Applied callback [%s] query [%s], fields [%s], sort [%s], update [%s], arrayFilters [%s], options [%s]", getClass().getSimpleName(), serializeToJsonSafely(query), serializeToJsonSafely(fields), serializeToJsonSafely(sort), serializeToJsonSafely(update), serializeToJsonSafely(arrayFilters), serializeToJsonSafely(options)));
3258+
}
3259+
3260+
return result;
32383261
}
32393262
}
32403263

@@ -3283,7 +3306,14 @@ public Document doInCollection(MongoCollection<Document> collection) throws Mong
32833306
opts.returnDocument(ReturnDocument.AFTER);
32843307
}
32853308

3286-
return collectionPreparer.prepare(collection).findOneAndReplace(query, update, opts);
3309+
Document document = collectionPreparer.prepare(collection).findOneAndReplace(query, update, opts);
3310+
3311+
if (LOGGER.isDebugEnabled()) {
3312+
LOGGER.debug(String.format(
3313+
"Applied callback [%s] query [%s], fields [%s], sort [%s], update [%s], collation [%s], options [%s]", getClass().getSimpleName(), serializeToJsonSafely(query), serializeToJsonSafely(fields), serializeToJsonSafely(sort), serializeToJsonSafely(update), serializeToJsonSafely(collation), serializeToJsonSafely(options)));
3314+
}
3315+
3316+
return document;
32873317
}
32883318
}
32893319

@@ -3333,6 +3363,11 @@ public T doWith(Document document) {
33333363
maybeEmitEvent(new AfterConvertEvent<>(document, entity, collectionName));
33343364
entity = maybeCallAfterConvert(entity, document, collectionName);
33353365

3366+
if (LOGGER.isDebugEnabled()) {
3367+
LOGGER.debug(String.format(
3368+
"Applied callback [%s] reader [%s], type [%s], collectionName [%s]", getClass().getSimpleName(), serializeToJsonSafely(reader), serializeToJsonSafely(type), serializeToJsonSafely(collectionName)));
3369+
}
3370+
33363371
return entity;
33373372
}
33383373
}
@@ -3375,7 +3410,14 @@ public T doWith(Document document) {
33753410
}
33763411

33773412
maybeEmitEvent(new AfterConvertEvent<>(document, entity, collectionName));
3378-
return (T) maybeCallAfterConvert(entity, document, collectionName);
3413+
T maybeConverted = (T) maybeCallAfterConvert(entity, document, collectionName);
3414+
3415+
if (LOGGER.isDebugEnabled()) {
3416+
LOGGER.debug(String.format(
3417+
"Applied callback [%s] converter [%s], projection [%s], collectionName [%s]", getClass().getSimpleName(), serializeToJsonSafely(mongoConverter), serializeToJsonSafely(projection), serializeToJsonSafely(collectionName)));
3418+
}
3419+
3420+
return maybeConverted;
33793421
}
33803422
}
33813423

@@ -3474,6 +3516,11 @@ public FindIterable<Document> prepare(FindIterable<Document> iterable) {
34743516
throw potentiallyConvertRuntimeException(e, exceptionTranslator);
34753517
}
34763518

3519+
if (LOGGER.isDebugEnabled()) {
3520+
LOGGER.debug(String.format(
3521+
"Prepared cursor with [%s] query [%s], sortObject [%s], limit [%s], skip [%s], type [%s]", getClass().getSimpleName(), serializeToJsonSafely(query), serializeToJsonSafely(sortObject), serializeToJsonSafely(limit), serializeToJsonSafely(skip), serializeToJsonSafely(type)));
3522+
}
3523+
34773524
return cursorToUse;
34783525
}
34793526

@@ -3519,7 +3566,14 @@ public GeoResult<T> doWith(Document object) {
35193566

35203567
T doWith = delegate.doWith(object);
35213568

3522-
return new GeoResult<>(doWith, new Distance(distance, metric));
3569+
GeoResult<T> tGeoResult = new GeoResult<>(doWith, new Distance(distance, metric));
3570+
3571+
if (LOGGER.isDebugEnabled()) {
3572+
LOGGER.debug(String.format(
3573+
"Applied callback [%s] distanceField [%s], delegate [%s], metric [%s]", getClass().getSimpleName(), serializeToJsonSafely(distanceField), serializeToJsonSafely(delegate), serializeToJsonSafely(metric)));
3574+
}
3575+
3576+
return tGeoResult;
35233577
}
35243578
}
35253579

@@ -3684,7 +3738,14 @@ private static class ReplaceCallback implements CollectionCallback<UpdateResult>
36843738
@Override
36853739
public UpdateResult doInCollection(MongoCollection<Document> collection)
36863740
throws MongoException, DataAccessException {
3687-
return collectionPreparer.prepare(collection).replaceOne(query, update, options);
3741+
UpdateResult updateResult = collectionPreparer.prepare(collection).replaceOne(query, update, options);
3742+
3743+
if (LOGGER.isDebugEnabled()) {
3744+
LOGGER.debug(String.format(
3745+
"Applied callback [%s] collectionPreparer [%s], query [%s], update [%s], options [%s]", getClass().getSimpleName(), collectionPreparer.getClass().getSimpleName(), serializeToJsonSafely(query), serializeToJsonSafely(update), serializeToJsonSafely(options)));
3746+
}
3747+
3748+
return updateResult;
36883749
}
36893750
}
36903751
}

0 commit comments

Comments
 (0)