Skip to content

Commit 4e37cfe

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

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
@@ -397,13 +397,11 @@ public CommandResult doInDB(DB db) throws MongoException, DataAccessException {
397397
}
398398

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

@@ -431,8 +429,8 @@ protected void executeQuery(Query query, String collectionName, DocumentCallback
431429
DBObject fieldsObject = query.getFieldsObject();
432430

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

438436
this.executeQueryInternal(new FindCallback(queryObject, fieldsObject), preparer, dch, collectionName);
@@ -530,7 +528,7 @@ public void dropCollection(String collectionName) {
530528
public Void doInCollection(DBCollection collection) throws MongoException, DataAccessException {
531529
collection.drop();
532530
if (LOGGER.isDebugEnabled()) {
533-
LOGGER.debug("Dropped collection [" + collection.getFullName() + "]");
531+
LOGGER.debug("Dropped collection [{}]", collection.getFullName());
534532
}
535533
return null;
536534
}
@@ -642,8 +640,8 @@ public <T> GeoResults<T> geoNear(NearQuery near, Class<T> entityClass, String co
642640
}
643641

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

649647
CommandResult commandResult = executeCommand(command, this.readPreference);
@@ -997,9 +995,11 @@ protected <T> void doSave(String collectionName, T objectToSave, MongoWriter<T>
997995
}
998996

999997
protected Object insertDBObject(final String collectionName, final DBObject dbDoc, final Class<?> entityClass) {
998+
1000999
if (LOGGER.isDebugEnabled()) {
1001-
LOGGER.debug("Inserting DBObject containing fields: " + dbDoc.keySet() + " in collection: " + collectionName);
1000+
LOGGER.debug("Inserting DBObject containing fields: {} in collection: {}", dbDoc.keySet(), collectionName);
10021001
}
1002+
10031003
return execute(collectionName, new CollectionCallback<Object>() {
10041004
public Object doInCollection(DBCollection collection) throws MongoException, DataAccessException {
10051005
MongoAction mongoAction = new MongoAction(writeConcern, MongoActionOperation.INSERT, collectionName,
@@ -1019,8 +1019,9 @@ protected List<ObjectId> insertDBObjectList(final String collectionName, final L
10191019
}
10201020

10211021
if (LOGGER.isDebugEnabled()) {
1022-
LOGGER.debug("Inserting list of DBObjects containing " + dbDocList.size() + " items");
1022+
LOGGER.debug("Inserting list of DBObjects containing {} items", dbDocList.size());
10231023
}
1024+
10241025
execute(collectionName, new CollectionCallback<Void>() {
10251026
public Void doInCollection(DBCollection collection) throws MongoException, DataAccessException {
10261027
MongoAction mongoAction = new MongoAction(writeConcern, MongoActionOperation.INSERT_LIST, collectionName, null,
@@ -1047,9 +1048,11 @@ public Void doInCollection(DBCollection collection) throws MongoException, DataA
10471048
}
10481049

10491050
protected Object saveDBObject(final String collectionName, final DBObject dbDoc, final Class<?> entityClass) {
1051+
10501052
if (LOGGER.isDebugEnabled()) {
1051-
LOGGER.debug("Saving DBObject containing fields: " + dbDoc.keySet());
1053+
LOGGER.debug("Saving DBObject containing fields: {}", dbDoc.keySet());
10521054
}
1055+
10531056
return execute(collectionName, new CollectionCallback<Object>() {
10541057
public Object doInCollection(DBCollection collection) throws MongoException, DataAccessException {
10551058
MongoAction mongoAction = new MongoAction(writeConcern, MongoActionOperation.SAVE, collectionName, entityClass,
@@ -1115,8 +1118,8 @@ public WriteResult doInCollection(DBCollection collection) throws MongoException
11151118
update.getUpdateObject(), entity);
11161119

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

11221125
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);
@@ -1677,8 +1680,8 @@ protected <T> T doFindOne(String collectionName, DBObject query, DBObject fields
16771680
DBObject mappedFields = fields == null ? null : queryMapper.getMappedObject(fields, entity);
16781681

16791682
if (LOGGER.isDebugEnabled()) {
1680-
LOGGER.debug(String.format("findOne using query: %s fields: %s for class: %s in collection: %s",
1681-
serializeToJsonSafely(query), mappedFields, entityClass, collectionName));
1683+
LOGGER.debug("findOne using query: {} fields: {} for class: {} in collection: {}", serializeToJsonSafely(query),
1684+
mappedFields, entityClass, collectionName);
16821685
}
16831686

16841687
return executeFindOneInternal(new FindOneCallback(mappedQuery, mappedFields), new ReadDbObjectCallback<T>(
@@ -1728,8 +1731,8 @@ protected <S, T> List<T> doFind(String collectionName, DBObject query, DBObject
17281731
DBObject mappedQuery = queryMapper.getMappedObject(query, entity);
17291732

17301733
if (LOGGER.isDebugEnabled()) {
1731-
LOGGER.debug(String.format("find using query: %s fields: %s for class: %s in collection: %s",
1732-
serializeToJsonSafely(mappedQuery), mappedFields, entityClass, collectionName));
1734+
LOGGER.debug("find using query: {} fields: {} for class: {} in collection: {}",
1735+
serializeToJsonSafely(mappedQuery), mappedFields, entityClass, collectionName);
17331736
}
17341737

17351738
return executeFindMultiInternal(new FindCallback(mappedQuery, mappedFields), preparer, objectCallback,
@@ -1765,12 +1768,16 @@ protected DBObject convertToDbObject(CollectionOptions collectionOptions) {
17651768
*/
17661769
protected <T> T doFindAndRemove(String collectionName, DBObject query, DBObject fields, DBObject sort,
17671770
Class<T> entityClass) {
1771+
17681772
EntityReader<? super T, DBObject> readerToUse = this.mongoConverter;
1773+
17691774
if (LOGGER.isDebugEnabled()) {
1770-
LOGGER.debug(String.format("findAndRemove using query: %s fields: %s sort: %s for class: %s in collection: %s",
1771-
serializeToJsonSafely(query), fields, sort, entityClass, collectionName));
1775+
LOGGER.debug("findAndRemove using query: {} fields: {} sort: {} for class: {} in collection: {}",
1776+
serializeToJsonSafely(query), fields, sort, entityClass, collectionName);
17721777
}
1778+
17731779
MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(entityClass);
1780+
17741781
return executeFindOneInternal(new FindAndRemoveCallback(queryMapper.getMappedObject(query, entity), fields, sort),
17751782
new ReadDbObjectCallback<T>(readerToUse, entityClass, collectionName), collectionName);
17761783
}
@@ -1792,9 +1799,10 @@ protected <T> T doFindAndModify(String collectionName, DBObject query, DBObject
17921799
DBObject mappedUpdate = updateMapper.getMappedObject(update.getUpdateObject(), entity);
17931800

17941801
if (LOGGER.isDebugEnabled()) {
1795-
LOGGER.debug(String.format("findAndModify using query: %s fields: %s sort: %s for class: %s and update: %s "
1796-
+ "in collection: %s", serializeToJsonSafely(mappedQuery), fields, sort, entityClass,
1797-
serializeToJsonSafely(mappedUpdate), collectionName));
1802+
LOGGER.debug(
1803+
"findAndModify using query: {} fields: {} sort: {} for class: {} and update: {} " + "in collection: {}",
1804+
serializeToJsonSafely(mappedQuery), fields, sort, entityClass, serializeToJsonSafely(mappedUpdate),
1805+
collectionName);
17981806
}
17991807

18001808
return executeFindOneInternal(new FindAndModifyCallback(mappedQuery, fields, sort, mappedUpdate, options),
@@ -2104,14 +2112,14 @@ public FindOneCallback(DBObject query, DBObject fields) {
21042112
public DBObject doInCollection(DBCollection collection) throws MongoException, DataAccessException {
21052113
if (fields == null) {
21062114
if (LOGGER.isDebugEnabled()) {
2107-
LOGGER.debug(String.format("findOne using query: %s in db.collection: %s", serializeToJsonSafely(query),
2108-
collection.getFullName()));
2115+
LOGGER.debug("findOne using query: {} in db.collection: {}", serializeToJsonSafely(query),
2116+
collection.getFullName());
21092117
}
21102118
return collection.findOne(query);
21112119
} else {
21122120
if (LOGGER.isDebugEnabled()) {
2113-
LOGGER.debug(String.format("findOne using query: %s fields: %s in db.collection: %s",
2114-
serializeToJsonSafely(query), fields, collection.getFullName()));
2121+
LOGGER.debug("findOne using query: {} fields: {} in db.collection: {}", serializeToJsonSafely(query), fields,
2122+
collection.getFullName());
21152123
}
21162124
return collection.findOne(query, fields);
21172125
}

0 commit comments

Comments
 (0)