Skip to content

Commit

Permalink
Allowing writeconcern to be set using properties file.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaypatel512 authored and RobertStewart committed Jun 17, 2012
1 parent 4884130 commit 8bc7cab
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/main/java/org/log4mongo/MongoDbAppender.java
Expand Up @@ -29,6 +29,7 @@
import com.mongodb.Mongo;
import com.mongodb.MongoException;
import com.mongodb.ServerAddress;
import com.mongodb.WriteConcern;

/**
* Log4J Appender that writes log events into a MongoDB document oriented database. Log events are
Expand All @@ -42,7 +43,7 @@
* @author Peter Monks (pmonks@gmail.com)
* @see <a href="http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/Appender.html">Log4J
* Appender Interface</a>
* @see <a href="http://www.mongodb.org/">MongoDB</a>
* @see <a href="http://www.mongodb.org/">MongoDB</a>
*/
public class MongoDbAppender extends BsonAppender {
private final static String DEFAULT_MONGO_DB_HOSTNAME = "localhost";
Expand All @@ -56,7 +57,7 @@ public class MongoDbAppender extends BsonAppender {
private String collectionName = DEFAULT_MONGO_DB_COLLECTION_NAME;
private String userName = null;
private String password = null;

private WriteConcern writeConcern = WriteConcern.NORMAL;
private Mongo mongo = null;
private DBCollection collection = null;

Expand Down Expand Up @@ -250,6 +251,22 @@ public void setUserName(final String userName) {
public void setPassword(final String password) {
this.password = password;
}

/**
* @return
* Gets the writeConcern setting for Mongo.
*/
public WriteConcern 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;
}

/**
* @param bson
Expand All @@ -259,7 +276,7 @@ public void setPassword(final String password) {
public void append(DBObject bson) {
if (initialized && bson != null) {
try {
getCollection().insert(bson);
getCollection().insert(bson, getWriteConcern());
} catch (MongoException e) {
errorHandler.error("Failed to insert document to MongoDB", e,
ErrorCode.WRITE_FAILURE);
Expand Down

0 comments on commit 8bc7cab

Please sign in to comment.