Skip to content

Commit 6068f32

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

File tree

1 file changed

+38
-34
lines changed

1 file changed

+38
-34
lines changed

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

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -398,13 +398,11 @@ public CommandResult doInDB(DB db) throws MongoException, DataAccessException {
398398
}
399399

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

@@ -433,8 +431,8 @@ protected void executeQuery(Query query, String collectionName, DocumentCallback
433431
DBObject fieldsObject = query.getFieldsObject();
434432

435433
if (LOGGER.isDebugEnabled()) {
436-
LOGGER.debug(String.format("Executing query: %s sort: %s fields: %s in collection: %s",
437-
serializeToJsonSafely(queryObject), sortObject, fieldsObject, collectionName));
434+
LOGGER.debug("Executing query: {} sort: {} fields: {} in collection: {}", serializeToJsonSafely(queryObject),
435+
sortObject, fieldsObject, collectionName);
438436
}
439437

440438
this.executeQueryInternal(new FindCallback(queryObject, fieldsObject), preparer, dch, collectionName);
@@ -532,7 +530,7 @@ public void dropCollection(String collectionName) {
532530
public Void doInCollection(DBCollection collection) throws MongoException, DataAccessException {
533531
collection.drop();
534532
if (LOGGER.isDebugEnabled()) {
535-
LOGGER.debug("Dropped collection [" + collection.getFullName() + "]");
533+
LOGGER.debug("Dropped collection [{}]", collection.getFullName());
536534
}
537535
return null;
538536
}
@@ -666,8 +664,8 @@ public <T> GeoResults<T> geoNear(NearQuery near, Class<T> entityClass, String co
666664
}
667665

668666
if (LOGGER.isDebugEnabled()) {
669-
LOGGER.debug(String.format("Executing geoNear using: %s for class: %s in collection: %s",
670-
serializeToJsonSafely(command), entityClass, collectionName));
667+
LOGGER.debug("Executing geoNear using: {} for class: {} in collection: {}", serializeToJsonSafely(command),
668+
entityClass, collectionName);
671669
}
672670

673671
CommandResult commandResult = executeCommand(command, this.readPreference);
@@ -1022,9 +1020,11 @@ protected <T> void doSave(String collectionName, T objectToSave, MongoWriter<T>
10221020
}
10231021

10241022
protected Object insertDBObject(final String collectionName, final DBObject dbDoc, final Class<?> entityClass) {
1023+
10251024
if (LOGGER.isDebugEnabled()) {
1026-
LOGGER.debug("Inserting DBObject containing fields: " + dbDoc.keySet() + " in collection: " + collectionName);
1025+
LOGGER.debug("Inserting DBObject containing fields: {} in collection: {}", dbDoc.keySet(), collectionName);
10271026
}
1027+
10281028
return execute(collectionName, new CollectionCallback<Object>() {
10291029
public Object doInCollection(DBCollection collection) throws MongoException, DataAccessException {
10301030
MongoAction mongoAction = new MongoAction(writeConcern, MongoActionOperation.INSERT, collectionName,
@@ -1044,8 +1044,9 @@ protected List<ObjectId> insertDBObjectList(final String collectionName, final L
10441044
}
10451045

10461046
if (LOGGER.isDebugEnabled()) {
1047-
LOGGER.debug("Inserting list of DBObjects containing " + dbDocList.size() + " items");
1047+
LOGGER.debug("Inserting list of DBObjects containing {} items", dbDocList.size());
10481048
}
1049+
10491050
execute(collectionName, new CollectionCallback<Void>() {
10501051
public Void doInCollection(DBCollection collection) throws MongoException, DataAccessException {
10511052
MongoAction mongoAction = new MongoAction(writeConcern, MongoActionOperation.INSERT_LIST, collectionName, null,
@@ -1072,9 +1073,11 @@ public Void doInCollection(DBCollection collection) throws MongoException, DataA
10721073
}
10731074

10741075
protected Object saveDBObject(final String collectionName, final DBObject dbDoc, final Class<?> entityClass) {
1076+
10751077
if (LOGGER.isDebugEnabled()) {
1076-
LOGGER.debug("Saving DBObject containing fields: " + dbDoc.keySet());
1078+
LOGGER.debug("Saving DBObject containing fields: {}", dbDoc.keySet());
10771079
}
1080+
10781081
return execute(collectionName, new CollectionCallback<Object>() {
10791082
public Object doInCollection(DBCollection collection) throws MongoException, DataAccessException {
10801083
MongoAction mongoAction = new MongoAction(writeConcern, MongoActionOperation.SAVE, collectionName, entityClass,
@@ -1140,8 +1143,8 @@ public WriteResult doInCollection(DBCollection collection) throws MongoException
11401143
: updateMapper.getMappedObject(update.getUpdateObject(), entity);
11411144

11421145
if (LOGGER.isDebugEnabled()) {
1143-
LOGGER.debug(String.format("Calling update using query: %s and update: %s in collection: %s",
1144-
serializeToJsonSafely(queryObj), serializeToJsonSafely(updateObj), collectionName));
1146+
LOGGER.debug("Calling update using query: {} and update: {} in collection: {}",
1147+
serializeToJsonSafely(queryObj), serializeToJsonSafely(updateObj), collectionName);
11451148
}
11461149

11471150
MongoAction mongoAction = new MongoAction(writeConcern, MongoActionOperation.UPDATE, collectionName,
@@ -1376,8 +1379,8 @@ public <T> MapReduceResults<T> mapReduce(Query query, String inputCollectionName
13761379
copyMapReduceOptionsToCommand(query, mapReduceOptions, command);
13771380

13781381
if (LOGGER.isDebugEnabled()) {
1379-
LOGGER.debug("Executing MapReduce on collection [" + command.getInput() + "], mapFunction [" + mapFunc
1380-
+ "], reduceFunction [" + reduceFunc + "]");
1382+
LOGGER.debug("Executing MapReduce on collection [{}], mapFunction [{}], reduceFunction [{}]", command.getInput(),
1383+
mapFunc, reduceFunc);
13811384
}
13821385

13831386
MapReduceOutput mapReduceOutput = inputCollection.mapReduce(command);
@@ -1703,8 +1706,8 @@ protected <T> T doFindOne(String collectionName, DBObject query, DBObject fields
17031706
DBObject mappedFields = fields == null ? null : queryMapper.getMappedObject(fields, entity);
17041707

17051708
if (LOGGER.isDebugEnabled()) {
1706-
LOGGER.debug(String.format("findOne using query: %s fields: %s for class: %s in collection: %s",
1707-
serializeToJsonSafely(query), mappedFields, entityClass, collectionName));
1709+
LOGGER.debug("findOne using query: {} fields: {} for class: {} in collection: {}", serializeToJsonSafely(query),
1710+
mappedFields, entityClass, collectionName);
17081711
}
17091712

17101713
return executeFindOneInternal(new FindOneCallback(mappedQuery, mappedFields),
@@ -1754,8 +1757,8 @@ protected <S, T> List<T> doFind(String collectionName, DBObject query, DBObject
17541757
DBObject mappedQuery = queryMapper.getMappedObject(query, entity);
17551758

17561759
if (LOGGER.isDebugEnabled()) {
1757-
LOGGER.debug(String.format("find using query: %s fields: %s for class: %s in collection: %s",
1758-
serializeToJsonSafely(mappedQuery), mappedFields, entityClass, collectionName));
1760+
LOGGER.debug("find using query: {} fields: {} for class: {} in collection: {}",
1761+
serializeToJsonSafely(mappedQuery), mappedFields, entityClass, collectionName);
17591762
}
17601763

17611764
return executeFindMultiInternal(new FindCallback(mappedQuery, mappedFields), preparer, objectCallback,
@@ -1791,12 +1794,16 @@ protected DBObject convertToDbObject(CollectionOptions collectionOptions) {
17911794
*/
17921795
protected <T> T doFindAndRemove(String collectionName, DBObject query, DBObject fields, DBObject sort,
17931796
Class<T> entityClass) {
1797+
17941798
EntityReader<? super T, DBObject> readerToUse = this.mongoConverter;
1799+
17951800
if (LOGGER.isDebugEnabled()) {
1796-
LOGGER.debug(String.format("findAndRemove using query: %s fields: %s sort: %s for class: %s in collection: %s",
1797-
serializeToJsonSafely(query), fields, sort, entityClass, collectionName));
1801+
LOGGER.debug("findAndRemove using query: {} fields: {} sort: {} for class: {} in collection: {}",
1802+
serializeToJsonSafely(query), fields, sort, entityClass, collectionName);
17981803
}
1804+
17991805
MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(entityClass);
1806+
18001807
return executeFindOneInternal(new FindAndRemoveCallback(queryMapper.getMappedObject(query, entity), fields, sort),
18011808
new ReadDbObjectCallback<T>(readerToUse, entityClass, collectionName), collectionName);
18021809
}
@@ -1818,13 +1825,10 @@ protected <T> T doFindAndModify(String collectionName, DBObject query, DBObject
18181825
DBObject mappedUpdate = updateMapper.getMappedObject(update.getUpdateObject(), entity);
18191826

18201827
if (LOGGER.isDebugEnabled()) {
1821-
LOGGER
1822-
.debug(
1823-
String.format(
1824-
"findAndModify using query: %s fields: %s sort: %s for class: %s and update: %s "
1825-
+ "in collection: %s",
1826-
serializeToJsonSafely(mappedQuery), fields, sort, entityClass, serializeToJsonSafely(mappedUpdate),
1827-
collectionName));
1828+
LOGGER.debug(
1829+
"findAndModify using query: {} fields: {} sort: {} for class: {} and update: {} " + "in collection: {}",
1830+
serializeToJsonSafely(mappedQuery), fields, sort, entityClass, serializeToJsonSafely(mappedUpdate),
1831+
collectionName);
18281832
}
18291833

18301834
return executeFindOneInternal(new FindAndModifyCallback(mappedQuery, fields, sort, mappedUpdate, options),
@@ -2134,14 +2138,14 @@ public FindOneCallback(DBObject query, DBObject fields) {
21342138
public DBObject doInCollection(DBCollection collection) throws MongoException, DataAccessException {
21352139
if (fields == null) {
21362140
if (LOGGER.isDebugEnabled()) {
2137-
LOGGER.debug(String.format("findOne using query: %s in db.collection: %s", serializeToJsonSafely(query),
2138-
collection.getFullName()));
2141+
LOGGER.debug("findOne using query: {} in db.collection: {}", serializeToJsonSafely(query),
2142+
collection.getFullName());
21392143
}
21402144
return collection.findOne(query);
21412145
} else {
21422146
if (LOGGER.isDebugEnabled()) {
2143-
LOGGER.debug(String.format("findOne using query: %s fields: %s in db.collection: %s",
2144-
serializeToJsonSafely(query), fields, collection.getFullName()));
2147+
LOGGER.debug("findOne using query: {} fields: {} in db.collection: {}", serializeToJsonSafely(query), fields,
2148+
collection.getFullName());
21452149
}
21462150
return collection.findOne(query, fields);
21472151
}

0 commit comments

Comments
 (0)