Skip to content

Commit 2a061b9

Browse files
committed
DATAMONGO-1397 - Polishing.
Switched to Slf4J-native placeholder replacement in debug logging for MongoTemplate. Original pull request: #348.
1 parent 4f10d0c commit 2a061b9

File tree

1 file changed

+38
-30
lines changed

1 file changed

+38
-30
lines changed

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

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -396,13 +396,11 @@ public CommandResult doInDB(DB db) throws MongoException, DataAccessException {
396396
}
397397

398398
protected void logCommandExecutionError(final DBObject command, CommandResult result) {
399+
399400
String error = result.getErrorMessage();
401+
400402
if (error != null) {
401-
// TODO: DATADOC-204 allow configuration of logging level / throw
402-
// throw new
403-
// InvalidDataAccessApiUsageException("Command execution of " +
404-
// command.toString() + " failed: " + error);
405-
LOGGER.warn("Command execution of " + command.toString() + " failed: " + error);
403+
LOGGER.warn("Command execution of {} failed: {}", command.toString(), error);
406404
}
407405
}
408406

@@ -430,8 +428,8 @@ protected void executeQuery(Query query, String collectionName, DocumentCallback
430428
DBObject fieldsObject = query.getFieldsObject();
431429

432430
if (LOGGER.isDebugEnabled()) {
433-
LOGGER.debug(String.format("Executing query: %s sort: %s fields: %s in collection: %s",
434-
serializeToJsonSafely(queryObject), sortObject, fieldsObject, collectionName));
431+
LOGGER.debug("Executing query: {} sort: {} fields: {} in collection: {}", serializeToJsonSafely(queryObject),
432+
sortObject, fieldsObject, collectionName);
435433
}
436434

437435
this.executeQueryInternal(new FindCallback(queryObject, fieldsObject), preparer, dch, collectionName);
@@ -529,7 +527,7 @@ public void dropCollection(String collectionName) {
529527
public Void doInCollection(DBCollection collection) throws MongoException, DataAccessException {
530528
collection.drop();
531529
if (LOGGER.isDebugEnabled()) {
532-
LOGGER.debug("Dropped collection [" + collection.getFullName() + "]");
530+
LOGGER.debug("Dropped collection [{}]", collection.getFullName());
533531
}
534532
return null;
535533
}
@@ -641,8 +639,8 @@ public <T> GeoResults<T> geoNear(NearQuery near, Class<T> entityClass, String co
641639
}
642640

643641
if (LOGGER.isDebugEnabled()) {
644-
LOGGER.debug(String.format("Executing geoNear using: %s for class: %s in collection: %s",
645-
serializeToJsonSafely(command), entityClass, collectionName));
642+
LOGGER.debug("Executing geoNear using: {} for class: {} in collection: {}", serializeToJsonSafely(command),
643+
entityClass, collectionName);
646644
}
647645

648646
CommandResult commandResult = executeCommand(command, this.readPreference);
@@ -996,9 +994,11 @@ protected <T> void doSave(String collectionName, T objectToSave, MongoWriter<T>
996994
}
997995

998996
protected Object insertDBObject(final String collectionName, final DBObject dbDoc, final Class<?> entityClass) {
997+
999998
if (LOGGER.isDebugEnabled()) {
1000-
LOGGER.debug("Inserting DBObject containing fields: " + dbDoc.keySet() + " in collection: " + collectionName);
999+
LOGGER.debug("Inserting DBObject containing fields: {} in collection: {}", dbDoc.keySet(), collectionName);
10011000
}
1001+
10021002
return execute(collectionName, new CollectionCallback<Object>() {
10031003
public Object doInCollection(DBCollection collection) throws MongoException, DataAccessException {
10041004
MongoAction mongoAction = new MongoAction(writeConcern, MongoActionOperation.INSERT, collectionName,
@@ -1018,8 +1018,9 @@ protected List<ObjectId> insertDBObjectList(final String collectionName, final L
10181018
}
10191019

10201020
if (LOGGER.isDebugEnabled()) {
1021-
LOGGER.debug("Inserting list of DBObjects containing " + dbDocList.size() + " items");
1021+
LOGGER.debug("Inserting list of DBObjects containing {} items", dbDocList.size());
10221022
}
1023+
10231024
execute(collectionName, new CollectionCallback<Void>() {
10241025
public Void doInCollection(DBCollection collection) throws MongoException, DataAccessException {
10251026
MongoAction mongoAction = new MongoAction(writeConcern, MongoActionOperation.INSERT_LIST, collectionName, null,
@@ -1046,9 +1047,11 @@ public Void doInCollection(DBCollection collection) throws MongoException, DataA
10461047
}
10471048

10481049
protected Object saveDBObject(final String collectionName, final DBObject dbDoc, final Class<?> entityClass) {
1050+
10491051
if (LOGGER.isDebugEnabled()) {
1050-
LOGGER.debug("Saving DBObject containing fields: " + dbDoc.keySet());
1052+
LOGGER.debug("Saving DBObject containing fields: {}", dbDoc.keySet());
10511053
}
1054+
10521055
return execute(collectionName, new CollectionCallback<Object>() {
10531056
public Object doInCollection(DBCollection collection) throws MongoException, DataAccessException {
10541057
MongoAction mongoAction = new MongoAction(writeConcern, MongoActionOperation.SAVE, collectionName, entityClass,
@@ -1114,8 +1117,8 @@ public WriteResult doInCollection(DBCollection collection) throws MongoException
11141117
update.getUpdateObject(), entity);
11151118

11161119
if (LOGGER.isDebugEnabled()) {
1117-
LOGGER.debug(String.format("Calling update using query: %s and update: %s in collection: %s",
1118-
serializeToJsonSafely(queryObj), serializeToJsonSafely(updateObj), collectionName));
1120+
LOGGER.debug("Calling update using query: {} and update: {} in collection: {}",
1121+
serializeToJsonSafely(queryObj), serializeToJsonSafely(updateObj), collectionName);
11191122
}
11201123

11211124
MongoAction mongoAction = new MongoAction(writeConcern, MongoActionOperation.UPDATE, collectionName,
@@ -1350,8 +1353,8 @@ public <T> MapReduceResults<T> mapReduce(Query query, String inputCollectionName
13501353
copyMapReduceOptionsToCommand(query, mapReduceOptions, command);
13511354

13521355
if (LOGGER.isDebugEnabled()) {
1353-
LOGGER.debug("Executing MapReduce on collection [" + command.getInput() + "], mapFunction [" + mapFunc
1354-
+ "], reduceFunction [" + reduceFunc + "]");
1356+
LOGGER.debug("Executing MapReduce on collection [{}], mapFunction [{}], reduceFunction [{}]", command.getInput(),
1357+
mapFunc, reduceFunc);
13551358
}
13561359

13571360
MapReduceOutput mapReduceOutput = inputCollection.mapReduce(command);
@@ -1675,8 +1678,8 @@ protected <T> T doFindOne(String collectionName, DBObject query, DBObject fields
16751678
DBObject mappedFields = fields == null ? null : queryMapper.getMappedObject(fields, entity);
16761679

16771680
if (LOGGER.isDebugEnabled()) {
1678-
LOGGER.debug(String.format("findOne using query: %s fields: %s for class: %s in collection: %s",
1679-
serializeToJsonSafely(query), mappedFields, entityClass, collectionName));
1681+
LOGGER.debug("findOne using query: {} fields: {} for class: {} in collection: {}", serializeToJsonSafely(query),
1682+
mappedFields, entityClass, collectionName);
16801683
}
16811684

16821685
return executeFindOneInternal(new FindOneCallback(mappedQuery, mappedFields), new ReadDbObjectCallback<T>(
@@ -1726,8 +1729,8 @@ protected <S, T> List<T> doFind(String collectionName, DBObject query, DBObject
17261729
DBObject mappedQuery = queryMapper.getMappedObject(query, entity);
17271730

17281731
if (LOGGER.isDebugEnabled()) {
1729-
LOGGER.debug(String.format("find using query: %s fields: %s for class: %s in collection: %s",
1730-
serializeToJsonSafely(mappedQuery), mappedFields, entityClass, collectionName));
1732+
LOGGER.debug("find using query: {} fields: {} for class: {} in collection: {}",
1733+
serializeToJsonSafely(mappedQuery), mappedFields, entityClass, collectionName);
17311734
}
17321735

17331736
return executeFindMultiInternal(new FindCallback(mappedQuery, mappedFields), preparer, objectCallback,
@@ -1763,12 +1766,16 @@ protected DBObject convertToDbObject(CollectionOptions collectionOptions) {
17631766
*/
17641767
protected <T> T doFindAndRemove(String collectionName, DBObject query, DBObject fields, DBObject sort,
17651768
Class<T> entityClass) {
1769+
17661770
EntityReader<? super T, DBObject> readerToUse = this.mongoConverter;
1771+
17671772
if (LOGGER.isDebugEnabled()) {
1768-
LOGGER.debug(String.format("findAndRemove using query: %s fields: %s sort: %s for class: %s in collection: %s",
1769-
serializeToJsonSafely(query), fields, sort, entityClass, collectionName));
1773+
LOGGER.debug("findAndRemove using query: {} fields: {} sort: {} for class: {} in collection: {}",
1774+
serializeToJsonSafely(query), fields, sort, entityClass, collectionName);
17701775
}
1776+
17711777
MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(entityClass);
1778+
17721779
return executeFindOneInternal(new FindAndRemoveCallback(queryMapper.getMappedObject(query, entity), fields, sort),
17731780
new ReadDbObjectCallback<T>(readerToUse, entityClass), collectionName);
17741781
}
@@ -1790,9 +1797,10 @@ protected <T> T doFindAndModify(String collectionName, DBObject query, DBObject
17901797
DBObject mappedUpdate = updateMapper.getMappedObject(update.getUpdateObject(), entity);
17911798

17921799
if (LOGGER.isDebugEnabled()) {
1793-
LOGGER.debug(String.format("findAndModify using query: %s fields: %s sort: %s for class: %s and update: %s "
1794-
+ "in collection: %s", serializeToJsonSafely(mappedQuery), fields, sort, entityClass,
1795-
serializeToJsonSafely(mappedUpdate), collectionName));
1800+
LOGGER.debug(
1801+
"findAndModify using query: {} fields: {} sort: {} for class: {} and update: {} " + "in collection: {}",
1802+
serializeToJsonSafely(mappedQuery), fields, sort, entityClass, serializeToJsonSafely(mappedUpdate),
1803+
collectionName);
17961804
}
17971805

17981806
return executeFindOneInternal(new FindAndModifyCallback(mappedQuery, fields, sort, mappedUpdate, options),
@@ -2102,14 +2110,14 @@ public FindOneCallback(DBObject query, DBObject fields) {
21022110
public DBObject doInCollection(DBCollection collection) throws MongoException, DataAccessException {
21032111
if (fields == null) {
21042112
if (LOGGER.isDebugEnabled()) {
2105-
LOGGER.debug(String.format("findOne using query: %s in db.collection: %s", serializeToJsonSafely(query),
2106-
collection.getFullName()));
2113+
LOGGER.debug("findOne using query: {} in db.collection: {}", serializeToJsonSafely(query),
2114+
collection.getFullName());
21072115
}
21082116
return collection.findOne(query);
21092117
} else {
21102118
if (LOGGER.isDebugEnabled()) {
2111-
LOGGER.debug(String.format("findOne using query: %s fields: %s in db.collection: %s",
2112-
serializeToJsonSafely(query), fields, collection.getFullName()));
2119+
LOGGER.debug("findOne using query: {} fields: {} in db.collection: {}", serializeToJsonSafely(query), fields,
2120+
collection.getFullName());
21132121
}
21142122
return collection.findOne(query, fields);
21152123
}

0 commit comments

Comments
 (0)