Skip to content

Commit c04343a

Browse files
s303Thomas Darimont
authored andcommitted
DATAMONGO-1096 - Use null-safe toString representation of query for debug logging.
We now use the null-safe serailizeToJsonSafely to avoid potential RuntimeExceptions during debug query printing in MongoTemplate. Based on original PR: #247. Original pull request: #251.
1 parent 05311ea commit c04343a

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,8 +1007,8 @@ public WriteResult doInCollection(DBCollection collection) throws MongoException
10071007
update.getUpdateObject(), entity);
10081008

10091009
if (LOGGER.isDebugEnabled()) {
1010-
LOGGER.debug("Calling update using query: " + queryObj + " and update: " + updateObj + " in collection: "
1011-
+ collectionName);
1010+
LOGGER.debug(String.format("Calling update using query: %s and update: %s in collection: %s",
1011+
serializeToJsonSafely(queryObj), serializeToJsonSafely(updateObj), collectionName));
10121012
}
10131013

10141014
MongoAction mongoAction = new MongoAction(writeConcern, MongoActionOperation.UPDATE, collectionName,
@@ -1187,7 +1187,8 @@ public WriteResult doInCollection(DBCollection collection) throws MongoException
11871187
WriteConcern writeConcernToUse = prepareWriteConcern(mongoAction);
11881188

11891189
if (LOGGER.isDebugEnabled()) {
1190-
LOGGER.debug("Remove using query: {} in collection: {}.", new Object[] { dboq, collection.getName() });
1190+
LOGGER.debug("Remove using query: {} in collection: {}.",
1191+
new Object[] { serializeToJsonSafely(dboq), collection.getName() });
11911192
}
11921193

11931194
WriteResult wr = writeConcernToUse == null ? collection.remove(dboq) : collection.remove(dboq,
@@ -1644,8 +1645,8 @@ protected <T> T doFindAndRemove(String collectionName, DBObject query, DBObject
16441645
Class<T> entityClass) {
16451646
EntityReader<? super T, DBObject> readerToUse = this.mongoConverter;
16461647
if (LOGGER.isDebugEnabled()) {
1647-
LOGGER.debug("findAndRemove using query: " + query + " fields: " + fields + " sort: " + sort + " for class: "
1648-
+ entityClass + " in collection: " + collectionName);
1648+
LOGGER.debug(String.format("findAndRemove using query: %s fields: %s sort: %s for class: %s in collection: %s",
1649+
serializeToJsonSafely(query), fields, sort, entityClass, collectionName));
16491650
}
16501651
MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(entityClass);
16511652
return executeFindOneInternal(new FindAndRemoveCallback(queryMapper.getMappedObject(query, entity), fields, sort),
@@ -1669,8 +1670,9 @@ protected <T> T doFindAndModify(String collectionName, DBObject query, DBObject
16691670
DBObject mappedUpdate = updateMapper.getMappedObject(update.getUpdateObject(), entity);
16701671

16711672
if (LOGGER.isDebugEnabled()) {
1672-
LOGGER.debug("findAndModify using query: " + mappedQuery + " fields: " + fields + " sort: " + sort
1673-
+ " for class: " + entityClass + " and update: " + mappedUpdate + " in collection: " + collectionName);
1673+
LOGGER.debug(String.format("findAndModify using query: %s fields: %s sort: %s for class: %s and update: %s " +
1674+
"in collection: %s", serializeToJsonSafely(mappedQuery), fields, sort, entityClass,
1675+
serializeToJsonSafely(mappedUpdate), collectionName));
16741676
}
16751677

16761678
return executeFindOneInternal(new FindAndModifyCallback(mappedQuery, fields, sort, mappedUpdate, options),
@@ -1980,13 +1982,14 @@ public FindOneCallback(DBObject query, DBObject fields) {
19801982
public DBObject doInCollection(DBCollection collection) throws MongoException, DataAccessException {
19811983
if (fields == null) {
19821984
if (LOGGER.isDebugEnabled()) {
1983-
LOGGER.debug("findOne using query: " + query + " in db.collection: " + collection.getFullName());
1985+
LOGGER.debug(String.format("findOne using query: %s in db.collection: %s",
1986+
serializeToJsonSafely(query), collection.getFullName()));
19841987
}
19851988
return collection.findOne(query);
19861989
} else {
19871990
if (LOGGER.isDebugEnabled()) {
1988-
LOGGER.debug("findOne using query: " + query + " fields: " + fields + " in db.collection: "
1989-
+ collection.getFullName());
1991+
LOGGER.debug(String.format("findOne using query: %s fields: %s in db.collection: %s",
1992+
serializeToJsonSafely(query), fields, collection.getFullName()));
19901993
}
19911994
return collection.findOne(query, fields);
19921995
}

0 commit comments

Comments
 (0)