Browse files

Updated code to take writeconcern as a property in log4j.properties f…

…ile.
  • Loading branch information...
1 parent e9a51a5 commit a124add4d960769af712d3c2cf598aa1df8d58e3 @jaypatel512 jaypatel512 committed Apr 10, 2012
View
19 src/main/java/org/log4mongo/MongoDbAppender.java
@@ -50,14 +50,15 @@
private final static String DEFAULT_MONGO_DB_PORT = "27017";
private final static String DEFAULT_MONGO_DB_DATABASE_NAME = "log4mongo";
private final static String DEFAULT_MONGO_DB_COLLECTION_NAME = "logevents";
+ private WriteConcern concern;
private String hostname = DEFAULT_MONGO_DB_HOSTNAME;
private String port = DEFAULT_MONGO_DB_PORT;
private String databaseName = DEFAULT_MONGO_DB_DATABASE_NAME;
private String collectionName = DEFAULT_MONGO_DB_COLLECTION_NAME;
private String userName = null;
private String password = null;
- private WriteConcern writeConcern = WriteConcern.NORMAL;
+ private String writeConcern = null;
private Mongo mongo = null;
private DBCollection collection = null;
@@ -256,16 +257,24 @@ public void setPassword(final String password) {
* @return
* Gets the writeConcern setting for Mongo.
*/
- public WriteConcern getWriteConcern() {
+ public String getWriteConcern() {
return writeConcern;
}
/**
* @param writeConcern
* The WriteConcern setting for Mongo Inserts.<i>(may be null). If null, set to default of dbCollection's writeConcern.</i>
*/
- public void setWriteConcern(WriteConcern writeConcern) {
- this.writeConcern = writeConcern;
+ public void setWriteConcern(final String writeConcern) {
+ this.writeConcern = writeConcern;
+ this.concern = new WriteConcern(writeConcern);
+ }
+
+ public WriteConcern getConcern() {
+ if(this.concern == null){
+ this.concern = getCollection().getWriteConcern();
+ }
+ return concern;
}
/**
@@ -276,7 +285,7 @@ public void setWriteConcern(WriteConcern writeConcern) {
public void append(DBObject bson) {
if (initialized && bson != null) {
try {
- getCollection().insert(bson, getWriteConcern());
+ getCollection().insert(bson, getConcern());
} catch (MongoException e) {
errorHandler.error("Failed to insert document to MongoDB", e,
ErrorCode.WRITE_FAILURE);
View
8 src/test/java/org/log4mongo/TestMongoDbAppender.java
@@ -24,6 +24,8 @@
import java.lang.management.ManagementFactory;
import java.net.InetAddress;
+import junit.framework.Assert;
+
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.junit.After;
@@ -101,6 +103,12 @@ public void testInitialized() throws Exception {
if (!appender.isInitialized())
fail();
}
+
+ @Test
+ public void testWriteConcern() throws Exception{
+ testInitialized();
+ Assert.assertEquals(appender.getWriteConcern(),"FSYNC_SAFE");
+ }
@Test
public void testSingleLogEntry() throws Exception {
View
3 src/test/resources/log4j.properties
@@ -3,4 +3,5 @@ log4j.rootLogger=all, MongoDB
# MongoDB appender classname
log4j.appender.MongoDB=org.log4mongo.MongoDbAppender
-log4j.appender.MongoDB.databaseName=log4mongotest
+log4j.appender.MongoDB.databaseName=log4mongotest
+log4j.appender.MongoDB.writeConcern=FSYNC_SAFE

0 comments on commit a124add

Please sign in to comment.