Browse Source

DATAMONGO-1397 - Polishing.

Switched to Slf4J-native placeholder replacement in debug logging for MongoTemplate.

Original pull request: #348.
1.7.x
Oliver Gierke 10 years ago
parent
commit
2a061b9725
  1. 68
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java

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

@ -396,13 +396,11 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
} }
protected void logCommandExecutionError(final DBObject command, CommandResult result) { protected void logCommandExecutionError(final DBObject command, CommandResult result) {
String error = result.getErrorMessage(); String error = result.getErrorMessage();
if (error != null) { if (error != null) {
// TODO: DATADOC-204 allow configuration of logging level / throw LOGGER.warn("Command execution of {} failed: {}", command.toString(), error);
// throw new
// InvalidDataAccessApiUsageException("Command execution of " +
// command.toString() + " failed: " + error);
LOGGER.warn("Command execution of " + command.toString() + " failed: " + error);
} }
} }
@ -430,8 +428,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
DBObject fieldsObject = query.getFieldsObject(); DBObject fieldsObject = query.getFieldsObject();
if (LOGGER.isDebugEnabled()) { if (LOGGER.isDebugEnabled()) {
LOGGER.debug(String.format("Executing query: %s sort: %s fields: %s in collection: %s", LOGGER.debug("Executing query: {} sort: {} fields: {} in collection: {}", serializeToJsonSafely(queryObject),
serializeToJsonSafely(queryObject), sortObject, fieldsObject, collectionName)); sortObject, fieldsObject, collectionName);
} }
this.executeQueryInternal(new FindCallback(queryObject, fieldsObject), preparer, dch, collectionName); this.executeQueryInternal(new FindCallback(queryObject, fieldsObject), preparer, dch, collectionName);
@ -529,7 +527,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
public Void doInCollection(DBCollection collection) throws MongoException, DataAccessException { public Void doInCollection(DBCollection collection) throws MongoException, DataAccessException {
collection.drop(); collection.drop();
if (LOGGER.isDebugEnabled()) { if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Dropped collection [" + collection.getFullName() + "]"); LOGGER.debug("Dropped collection [{}]", collection.getFullName());
} }
return null; return null;
} }
@ -641,8 +639,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
} }
if (LOGGER.isDebugEnabled()) { if (LOGGER.isDebugEnabled()) {
LOGGER.debug(String.format("Executing geoNear using: %s for class: %s in collection: %s", LOGGER.debug("Executing geoNear using: {} for class: {} in collection: {}", serializeToJsonSafely(command),
serializeToJsonSafely(command), entityClass, collectionName)); entityClass, collectionName);
} }
CommandResult commandResult = executeCommand(command, this.readPreference); CommandResult commandResult = executeCommand(command, this.readPreference);
@ -996,9 +994,11 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
} }
protected Object insertDBObject(final String collectionName, final DBObject dbDoc, final Class<?> entityClass) { protected Object insertDBObject(final String collectionName, final DBObject dbDoc, final Class<?> entityClass) {
if (LOGGER.isDebugEnabled()) { if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Inserting DBObject containing fields: " + dbDoc.keySet() + " in collection: " + collectionName); LOGGER.debug("Inserting DBObject containing fields: {} in collection: {}", dbDoc.keySet(), collectionName);
} }
return execute(collectionName, new CollectionCallback<Object>() { return execute(collectionName, new CollectionCallback<Object>() {
public Object doInCollection(DBCollection collection) throws MongoException, DataAccessException { public Object doInCollection(DBCollection collection) throws MongoException, DataAccessException {
MongoAction mongoAction = new MongoAction(writeConcern, MongoActionOperation.INSERT, collectionName, MongoAction mongoAction = new MongoAction(writeConcern, MongoActionOperation.INSERT, collectionName,
@ -1018,8 +1018,9 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
} }
if (LOGGER.isDebugEnabled()) { if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Inserting list of DBObjects containing " + dbDocList.size() + " items"); LOGGER.debug("Inserting list of DBObjects containing {} items", dbDocList.size());
} }
execute(collectionName, new CollectionCallback<Void>() { execute(collectionName, new CollectionCallback<Void>() {
public Void doInCollection(DBCollection collection) throws MongoException, DataAccessException { public Void doInCollection(DBCollection collection) throws MongoException, DataAccessException {
MongoAction mongoAction = new MongoAction(writeConcern, MongoActionOperation.INSERT_LIST, collectionName, null, MongoAction mongoAction = new MongoAction(writeConcern, MongoActionOperation.INSERT_LIST, collectionName, null,
@ -1046,9 +1047,11 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
} }
protected Object saveDBObject(final String collectionName, final DBObject dbDoc, final Class<?> entityClass) { protected Object saveDBObject(final String collectionName, final DBObject dbDoc, final Class<?> entityClass) {
if (LOGGER.isDebugEnabled()) { if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Saving DBObject containing fields: " + dbDoc.keySet()); LOGGER.debug("Saving DBObject containing fields: {}", dbDoc.keySet());
} }
return execute(collectionName, new CollectionCallback<Object>() { return execute(collectionName, new CollectionCallback<Object>() {
public Object doInCollection(DBCollection collection) throws MongoException, DataAccessException { public Object doInCollection(DBCollection collection) throws MongoException, DataAccessException {
MongoAction mongoAction = new MongoAction(writeConcern, MongoActionOperation.SAVE, collectionName, entityClass, MongoAction mongoAction = new MongoAction(writeConcern, MongoActionOperation.SAVE, collectionName, entityClass,
@ -1114,8 +1117,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
update.getUpdateObject(), entity); update.getUpdateObject(), entity);
if (LOGGER.isDebugEnabled()) { if (LOGGER.isDebugEnabled()) {
LOGGER.debug(String.format("Calling update using query: %s and update: %s in collection: %s", LOGGER.debug("Calling update using query: {} and update: {} in collection: {}",
serializeToJsonSafely(queryObj), serializeToJsonSafely(updateObj), collectionName)); serializeToJsonSafely(queryObj), serializeToJsonSafely(updateObj), collectionName);
} }
MongoAction mongoAction = new MongoAction(writeConcern, MongoActionOperation.UPDATE, collectionName, MongoAction mongoAction = new MongoAction(writeConcern, MongoActionOperation.UPDATE, collectionName,
@ -1350,8 +1353,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
copyMapReduceOptionsToCommand(query, mapReduceOptions, command); copyMapReduceOptionsToCommand(query, mapReduceOptions, command);
if (LOGGER.isDebugEnabled()) { if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Executing MapReduce on collection [" + command.getInput() + "], mapFunction [" + mapFunc LOGGER.debug("Executing MapReduce on collection [{}], mapFunction [{}], reduceFunction [{}]", command.getInput(),
+ "], reduceFunction [" + reduceFunc + "]"); mapFunc, reduceFunc);
} }
MapReduceOutput mapReduceOutput = inputCollection.mapReduce(command); MapReduceOutput mapReduceOutput = inputCollection.mapReduce(command);
@ -1675,8 +1678,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
DBObject mappedFields = fields == null ? null : queryMapper.getMappedObject(fields, entity); DBObject mappedFields = fields == null ? null : queryMapper.getMappedObject(fields, entity);
if (LOGGER.isDebugEnabled()) { if (LOGGER.isDebugEnabled()) {
LOGGER.debug(String.format("findOne using query: %s fields: %s for class: %s in collection: %s", LOGGER.debug("findOne using query: {} fields: {} for class: {} in collection: {}", serializeToJsonSafely(query),
serializeToJsonSafely(query), mappedFields, entityClass, collectionName)); mappedFields, entityClass, collectionName);
} }
return executeFindOneInternal(new FindOneCallback(mappedQuery, mappedFields), new ReadDbObjectCallback<T>( return executeFindOneInternal(new FindOneCallback(mappedQuery, mappedFields), new ReadDbObjectCallback<T>(
@ -1726,8 +1729,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
DBObject mappedQuery = queryMapper.getMappedObject(query, entity); DBObject mappedQuery = queryMapper.getMappedObject(query, entity);
if (LOGGER.isDebugEnabled()) { if (LOGGER.isDebugEnabled()) {
LOGGER.debug(String.format("find using query: %s fields: %s for class: %s in collection: %s", LOGGER.debug("find using query: {} fields: {} for class: {} in collection: {}",
serializeToJsonSafely(mappedQuery), mappedFields, entityClass, collectionName)); serializeToJsonSafely(mappedQuery), mappedFields, entityClass, collectionName);
} }
return executeFindMultiInternal(new FindCallback(mappedQuery, mappedFields), preparer, objectCallback, return executeFindMultiInternal(new FindCallback(mappedQuery, mappedFields), preparer, objectCallback,
@ -1763,12 +1766,16 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
*/ */
protected <T> T doFindAndRemove(String collectionName, DBObject query, DBObject fields, DBObject sort, protected <T> T doFindAndRemove(String collectionName, DBObject query, DBObject fields, DBObject sort,
Class<T> entityClass) { Class<T> entityClass) {
EntityReader<? super T, DBObject> readerToUse = this.mongoConverter; EntityReader<? super T, DBObject> readerToUse = this.mongoConverter;
if (LOGGER.isDebugEnabled()) { if (LOGGER.isDebugEnabled()) {
LOGGER.debug(String.format("findAndRemove using query: %s fields: %s sort: %s for class: %s in collection: %s", LOGGER.debug("findAndRemove using query: {} fields: {} sort: {} for class: {} in collection: {}",
serializeToJsonSafely(query), fields, sort, entityClass, collectionName)); serializeToJsonSafely(query), fields, sort, entityClass, collectionName);
} }
MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(entityClass); MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(entityClass);
return executeFindOneInternal(new FindAndRemoveCallback(queryMapper.getMappedObject(query, entity), fields, sort), return executeFindOneInternal(new FindAndRemoveCallback(queryMapper.getMappedObject(query, entity), fields, sort),
new ReadDbObjectCallback<T>(readerToUse, entityClass), collectionName); new ReadDbObjectCallback<T>(readerToUse, entityClass), collectionName);
} }
@ -1790,9 +1797,10 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
DBObject mappedUpdate = updateMapper.getMappedObject(update.getUpdateObject(), entity); DBObject mappedUpdate = updateMapper.getMappedObject(update.getUpdateObject(), entity);
if (LOGGER.isDebugEnabled()) { if (LOGGER.isDebugEnabled()) {
LOGGER.debug(String.format("findAndModify using query: %s fields: %s sort: %s for class: %s and update: %s " LOGGER.debug(
+ "in collection: %s", serializeToJsonSafely(mappedQuery), fields, sort, entityClass, "findAndModify using query: {} fields: {} sort: {} for class: {} and update: {} " + "in collection: {}",
serializeToJsonSafely(mappedUpdate), collectionName)); serializeToJsonSafely(mappedQuery), fields, sort, entityClass, serializeToJsonSafely(mappedUpdate),
collectionName);
} }
return executeFindOneInternal(new FindAndModifyCallback(mappedQuery, fields, sort, mappedUpdate, options), return executeFindOneInternal(new FindAndModifyCallback(mappedQuery, fields, sort, mappedUpdate, options),
@ -2102,14 +2110,14 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
public DBObject doInCollection(DBCollection collection) throws MongoException, DataAccessException { public DBObject doInCollection(DBCollection collection) throws MongoException, DataAccessException {
if (fields == null) { if (fields == null) {
if (LOGGER.isDebugEnabled()) { if (LOGGER.isDebugEnabled()) {
LOGGER.debug(String.format("findOne using query: %s in db.collection: %s", serializeToJsonSafely(query), LOGGER.debug("findOne using query: {} in db.collection: {}", serializeToJsonSafely(query),
collection.getFullName())); collection.getFullName());
} }
return collection.findOne(query); return collection.findOne(query);
} else { } else {
if (LOGGER.isDebugEnabled()) { if (LOGGER.isDebugEnabled()) {
LOGGER.debug(String.format("findOne using query: %s fields: %s in db.collection: %s", LOGGER.debug("findOne using query: {} fields: {} in db.collection: {}", serializeToJsonSafely(query), fields,
serializeToJsonSafely(query), fields, collection.getFullName())); collection.getFullName());
} }
return collection.findOne(query, fields); return collection.findOne(query, fields);
} }

Loading…
Cancel
Save