Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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

…ile.
  • Loading branch information...
commit a124add4d960769af712d3c2cf598aa1df8d58e3 1 parent e9a51a5
@jaypatel512 jaypatel512 authored
View
19 src/main/java/org/log4mongo/MongoDbAppender.java
@@ -50,6 +50,7 @@
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;
@@ -57,7 +58,7 @@
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,7 +257,7 @@ public void setPassword(final String password) {
* @return
* Gets the writeConcern setting for Mongo.
*/
- public WriteConcern getWriteConcern() {
+ public String getWriteConcern() {
return writeConcern;
}
@@ -264,8 +265,16 @@ public WriteConcern getWriteConcern() {
* @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
Please sign in to comment.
Something went wrong with that request. Please try again.