diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoTemplate.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoTemplate.java index 0f45c8ada..dd627394a 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoTemplate.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoTemplate.java @@ -468,12 +468,12 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica // Find methods that take a Query to express the query and that return a single object. public T findOne(Query query, Class targetClass) { - return findOne(getDefaultCollectionName(), query, targetClass); + return findOne(getEntityCollection(targetClass), query, targetClass); } public T findOne(Query query, Class targetClass, MongoReader reader) { - return findOne(getDefaultCollectionName(), query, targetClass, reader); + return findOne(getEntityCollection(targetClass), query, targetClass, reader); } public T findOne(String collectionName, Query query, @@ -536,12 +536,12 @@ public class MongoTemplate implements InitializingBean, MongoOperations, Applica // also removed from the collection in the database. public T findAndRemove(Query query, Class targetClass) { - return findAndRemove(getDefaultCollectionName(), query, targetClass); + return findAndRemove(getEntityCollection(targetClass), query, targetClass); } public T findAndRemove(Query query, Class targetClass, MongoReader reader) { - return findAndRemove(getDefaultCollectionName(), query, targetClass, reader); + return findAndRemove(getEntityCollection(targetClass), query, targetClass, reader); } public T findAndRemove(String collectionName, Query query, diff --git a/src/docbkx/reference/logging.xml b/src/docbkx/reference/logging.xml index bed76327d..5c129f00d 100644 --- a/src/docbkx/reference/logging.xml +++ b/src/docbkx/reference/logging.xml @@ -1,17 +1,44 @@ +"http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd"> Logging support - Logging support introduction. - + An appender for Log4j is provided in the maven module + "spring-data-mongodb-log4j". Note, there is no dependency on other Spring + Mongo modules, only the MongoDB driver.
MongoDB Log4j Configuration - Log4j... - + Here is an example configuration + log4j.rootCategory=INFO, stdout + +log4j.appender.stdout=org.springframework.data.document.mongodb.log4j.MongoLog4jAppender +log4j.appender.stdout.layout=org.apache.log4j.PatternLayout +log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - <%m>%n +log4j.appender.stdout.host = localhost +log4j.appender.stdout.port = 27017 +log4j.appender.stdout.database = logs +log4j.appender.stdout.collectionPattern = %X{year}%X{month} +log4j.appender.stdout.applicationId = my.application +log4j.appender.stdout.warnOrHigherWriteConcern = FSYNC_SAFE + +log4j.category.org.apache.activemq=ERROR +log4j.category.org.springframework.batch=DEBUG +log4j.category.org.springframework.data.document.mongodb=DEBUG +log4j.category.org.springframework.transaction=INFO + + The important configuration to look at aside from host and port is + the database and collectionPattern. The variables year, month, day and + hour are available for you to use in forming a collection name. This is to + support the common convention of grouping log information in a collection + that corresponds to a specific time period, for example a collection per + day. + + There is also an applicationId which is put into the stored message. + The document stored from logging as the following keys: level, name, + applicationId, timestamp, properties, traceback, and message.
diff --git a/src/docbkx/reference/mongodb.xml b/src/docbkx/reference/mongodb.xml index db787d377..32e9a0fb0 100644 --- a/src/docbkx/reference/mongodb.xml +++ b/src/docbkx/reference/mongodb.xml @@ -105,11 +105,7 @@ <artifactId>cglib</artifactId> <version>2.2</version> </dependency> - <dependency> - <groupId>log4j</groupId> - <artifactId>log4j</artifactId> - <scope>test</scope> - </dependency> + The cglib dependency is there as we will use Spring's Java configuration style. Also change the version of Spring in the pom.xml to @@ -117,6 +113,11 @@ <spring.framework.version>3.0.5.RELEASE</spring.framework.version> + You may also want to set the logging level to DEBUG to see some + additional information, edit the log4j.properties file and add + + log4j.category.org.springframework.data.document.mongodb=DEBUG + Next, in the org.spring.mongodb package in the sr/ctest/java directory create a class as shown below. @@ -145,7 +146,7 @@ public class MongoConfig extends AbstractMongoConfiguration { Then create a simple Person class to persist - package org.spring.mongodb; + package org.spring.mongodb; public class Person { @@ -175,7 +176,42 @@ public class Person { And a main application to run - + package org.spring.mongodb; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.data.document.mongodb.MongoOperations; +import org.springframework.data.document.mongodb.query.Criteria; +import org.springframework.data.document.mongodb.query.Query; + +public class MongoApp { + + private static final Log log = LogFactory.getLog(MongoApp.class); + + public static void main(String[] args) { + ApplicationContext ctx = new AnnotationConfigApplicationContext(MongoConfig.class); + MongoOperations mongoOps = ctx.getBean(MongoOperations.class); + + mongoOps.insert(new Person("1234", "Joe")); + + log.info(mongoOps.findOne(new Query(Criteria.where("name").is("Joe")), Person.class)); + } + +} + + This will produce the following output + + MongoPersistentEntityIndexCreator] - <Analyzing class class org.spring.mongodb.Person for index information.> +LoggingEventListener] - <onBeforeConvert: Person [id=1234, name=Joe]> +LoggingEventListener] - <onBeforeSave: Person [id=1234, name=Joe], { "_id" : "1234" , "name" : "Joe"}> +MongoTemplate] - <insert DBObject: { "_id" : "1234" , "name" : "Joe"}> +LoggingEventListener] - <onAfterSave: Person [id=1234, name=Joe], { "_id" : "1234" , "name" : "Joe"}> +MongoTemplate] - <findOne using query: { "name" : "Joe"} in db.collection: database.person> +LoggingEventListener] - <onAfterLoad: { "_id" : "1234" , "name" : "Joe"}> +LoggingEventListener] - <onAfterConvert: { "_id" : "1234" , "name" : "Joe"}, Person [id=1234, name=Joe]> +MongoApp] - <Person [id=1234, name=Joe]>