Added applicationId and write concern settings. Documented them in the README.
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
This module sets up a Log4J appender that puts logging events in MongoDB. It is fully configurable
|
||||
and connects directly to the MongoDB server using the driver. It has no dependency on any Spring package.
|
||||
|
||||
To use it, configure a host, port, and database property in your Log4J configuration:
|
||||
To use it, configure a host, port, (optionally) applicationId, and database property in your Log4J configuration:
|
||||
|
||||
log4j.appender.stdout=org.springframework.data.document.mongodb.log4j.MongoLog4jAppender
|
||||
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
|
||||
@@ -11,6 +11,8 @@ To use it, configure a host, port, and database property in your Log4J configura
|
||||
log4j.appender.stdout.host = localhost
|
||||
log4j.appender.stdout.port = 27017
|
||||
log4j.appender.stdout.database = logs
|
||||
log4j.appender.stdout.applicationId = my.application
|
||||
log4j.appender.stdout.warnOrHigherWriteConcern = FSYNC_SAFE
|
||||
|
||||
It will even support properties in your MDC (so long as they're Strings or support .toString()).
|
||||
|
||||
@@ -18,23 +20,33 @@ The collection name is configurable as well. If you don't specify anything, it w
|
||||
If you want to specify a collection name, you can give it a String.format() string which will be passed the
|
||||
following parameters:
|
||||
|
||||
1. Calendar.YEAR
|
||||
2. Calendar.MONTH
|
||||
3. Calendar.DAY_OF_MONTH
|
||||
4. Calendar.HOUR_OF_DAY
|
||||
5. event.getLevel().toString()
|
||||
6. event.getLogger().getName()
|
||||
1. Calendar.YEAR
|
||||
2. Calendar.MONTH
|
||||
3. Calendar.DAY_OF_MONTH
|
||||
4. Calendar.HOUR_OF_DAY
|
||||
5. event.getLevel().toString()
|
||||
6. event.getLogger().getName()
|
||||
|
||||
An example log entry might look like:
|
||||
|
||||
{
|
||||
"_id" : ObjectId("4d88ff0975cce9d7655bda46"),
|
||||
"_id" : ObjectId("4d89341a8ef397e06940d5cd"),
|
||||
"applicationId" : "my.application",
|
||||
"name" : "org.springframework.data.document.mongodb.log4j.AppenderTest",
|
||||
"level" : "DEBUG",
|
||||
"timestamp" : "1300823817891",
|
||||
"timestamp" : "1300837402444",
|
||||
"properties" : {
|
||||
"property" : "one"
|
||||
},
|
||||
"message" : "DEBUG message"
|
||||
}
|
||||
}
|
||||
|
||||
To set WriteConcern levels for WARN or higher messages, set warnOrHigherWriteConcern to one of the following:
|
||||
|
||||
* FSYNC_SAFE
|
||||
* NONE
|
||||
* NORMAL
|
||||
* REPLICAS_SAFE
|
||||
* SAFE
|
||||
|
||||
(http://api.mongodb.org/java/2.5-pre-/com/mongodb/WriteConcern.html#field_detail)[http://api.mongodb.org/java/2.5-pre-/com/mongodb/WriteConcern.html#field_detail]
|
||||
@@ -37,6 +37,7 @@ public class MongoLog4jAppender extends AppenderSkeleton {
|
||||
|
||||
public static final String LEVEL = "level";
|
||||
public static final String NAME = "name";
|
||||
public static final String APP_ID = "applicationId";
|
||||
public static final String TIMESTAMP = "timestamp";
|
||||
public static final String PROPERTIES = "properties";
|
||||
public static final String TRACEBACK = "traceback";
|
||||
@@ -46,6 +47,9 @@ public class MongoLog4jAppender extends AppenderSkeleton {
|
||||
protected int port = 27017;
|
||||
protected String database = "logs";
|
||||
protected String collection = null;
|
||||
protected String applicationId = System.getProperty("APPLICATION_ID", null);
|
||||
protected WriteConcern warnOrHigherWriteConcern = WriteConcern.SAFE;
|
||||
protected WriteConcern infoOrLowerWriteConcern = WriteConcern.NORMAL;
|
||||
protected Mongo mongo;
|
||||
protected DB db;
|
||||
|
||||
@@ -88,6 +92,30 @@ public class MongoLog4jAppender extends AppenderSkeleton {
|
||||
this.collection = collection;
|
||||
}
|
||||
|
||||
public String getApplicationId() {
|
||||
return applicationId;
|
||||
}
|
||||
|
||||
public void setApplicationId(String applicationId) {
|
||||
this.applicationId = applicationId;
|
||||
}
|
||||
|
||||
public void setWarnOrHigherWriteConcern(String wc) {
|
||||
this.warnOrHigherWriteConcern = WriteConcern.valueOf(wc);
|
||||
}
|
||||
|
||||
public String getWarnOrHigherWriteConcern() {
|
||||
return warnOrHigherWriteConcern.toString();
|
||||
}
|
||||
|
||||
public String getInfoOrLowerWriteConcern() {
|
||||
return infoOrLowerWriteConcern.toString();
|
||||
}
|
||||
|
||||
public void setInfoOrLowerWriteConcern(String wc) {
|
||||
this.infoOrLowerWriteConcern = WriteConcern.valueOf(wc);
|
||||
}
|
||||
|
||||
protected void connectToMongo() throws UnknownHostException {
|
||||
this.mongo = new Mongo(host, port);
|
||||
this.db = mongo.getDB(database);
|
||||
@@ -103,8 +131,9 @@ public class MongoLog4jAppender extends AppenderSkeleton {
|
||||
throw new RuntimeException(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
BasicDBObject dbo = new BasicDBObject();
|
||||
dbo.put(APP_ID, applicationId);
|
||||
dbo.put(NAME, event.getLogger().getName());
|
||||
dbo.put(LEVEL, event.getLevel().toString());
|
||||
dbo.put(TIMESTAMP, String.format("%s", event.getTimeStamp()));
|
||||
@@ -145,9 +174,11 @@ public class MongoLog4jAppender extends AppenderSkeleton {
|
||||
event.getLogger().getName());
|
||||
}
|
||||
|
||||
WriteConcern wc = WriteConcern.NORMAL;
|
||||
WriteConcern wc;
|
||||
if (event.getLevel().isGreaterOrEqual(Level.WARN)) {
|
||||
wc = WriteConcern.SAFE;
|
||||
wc = warnOrHigherWriteConcern;
|
||||
} else {
|
||||
wc = infoOrLowerWriteConcern;
|
||||
}
|
||||
db.getCollection(collection).insert(dbo, wc);
|
||||
}
|
||||
|
||||
@@ -6,6 +6,8 @@ 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.applicationId = my.application
|
||||
log4j.appender.stdout.warnOrHigherWriteConcern = FSYNC_SAFE
|
||||
|
||||
log4j.category.org.apache.activemq=ERROR
|
||||
log4j.category.org.springframework.batch=DEBUG
|
||||
|
||||
Reference in New Issue
Block a user