Permalink
Browse files

Minor tweaks, mostly formatting, to MDC enhancement

  • Loading branch information...
1 parent 39ba8f9 commit c4868de9749c944a3af452385d82de2a158ac69a Robert Stewart committed Jan 24, 2012
View
2 README.md
@@ -9,6 +9,7 @@ This library provides Log4J Appenders [1] that write log events to the
MongoDB document oriented database [2].
* MongoDbAppender - Stores a BSONified version of the Log4J LoggingEvent
+* ExtendedMongoDbAppender - Extends MongoDbAppender by allowing you to add top level elements
* MongoDbPatternLayoutAppender - Uses standard Log4J pattern layout, parser
and converter classes to store a log message as a custom-formatted document
@@ -23,6 +24,7 @@ More details are at the [Project site](http://log4mongo.org/display/PUB/Log4mong
* Zach Bailey (znbailey@gmail.com)
* Gabriel Eisbruch (gabrieleisbruch@gmail.com)
* cskinfill
+* Mick Knutson
# Pre-requisites
* JDK 1.5+
View
2 log4j.properties.sample
@@ -29,5 +29,5 @@ log4j.appender.MongoDB.password=sesame
# sub-documents. Values can be strings or arrays.
#log4j.appender.MongoDB.layout.ConversionPattern={"timestamp":"%d{yyyy-MM-dd'T'HH:mm:ss'Z'}","level":"%p","class":"%c{1}","message":"%m"}
-# Add optional root level elements to each log
+# Add optional root level elements to each log event
#log4j.appender.MongoDB.rootLevelProperties=applicationName=MyProject&eventType=Development
View
37 src/main/java/org/log4mongo/ExtendedMongoDbAppender.java
@@ -7,16 +7,16 @@
import java.util.Map;
/**
- * This extended MongoDbAppender is designed so you can add top
- * level elements to each logging entry. I think that users can also extend MongoDbAppender themselves in
- * order to add the top level elements.
- *
- * Rational: My use case is that many different code bases will use a common
- * appender for unified logs, and the common elements in a unified logging system
- * should be consistent such as application, eventType etc...
- * So by adding a property called rootLevelProperties with a key=value
- * pair of elements to be added to the root level log.
- *
+ * This appender is designed so you can add top level elements to each logging
+ * entry. Users can also extend MongoDbAppender themselves in order to add the
+ * top level elements.
+ *
+ * Use case: A desire to use a common appender for unified logs across different
+ * code bases, such that commonly logged elements be consistent, such as
+ * application, eventType, etc. This is enabled by adding a property called
+ * rootLevelProperties with a key=value list of elements to be added to the root
+ * level log. See log4j.properties.sample for an example.
+ *
* @author Mick Knutson (http://www.baselogic.com)
*/
public class ExtendedMongoDbAppender extends MongoDbAppender {
@@ -25,7 +25,6 @@
private Map<String, String> rootProperties = new LinkedHashMap<String, String>();
-
/**
* @see org.apache.log4j.AppenderSkeleton#activateOptions()
*/
@@ -36,13 +35,14 @@ public void activateOptions() {
}
/**
- * Initialize Top level JSON properties
- * Allow users to create custom properties to be added
- * to the top level JSON log.
+ * Initialize custom top level elements to appear in a log event
+ * <p>
+ * Allows users to create custom properties to be added to the top level
+ * log event.
*/
- public void initTopLevelProperties(){
+ public void initTopLevelProperties() {
constants = new BasicDBObject();
- if(!rootProperties.isEmpty()){
+ if (!rootProperties.isEmpty()){
constants.putAll(rootProperties);
}
}
@@ -54,13 +54,12 @@ public void initTopLevelProperties(){
* @param rootLevelProperties
*/
public void setRootLevelProperties(String rootLevelProperties) {
- for(String keyValue : rootLevelProperties.split(" *& *")) {
+ for (String keyValue : rootLevelProperties.split(" *& *")) {
String[] pairs = keyValue.split(" *= *", 2);
rootProperties.put(pairs[0], pairs.length == 1 ? "" : pairs[1]);
}
}
-
/**
* @param bson The BSON object to insert into a MongoDB database collection.
*/
@@ -73,4 +72,4 @@ public void append(DBObject bson) {
super.append(bson);
}
}
-}
+}
View
2 src/main/java/org/log4mongo/LoggingEventBsonifierImpl.java
@@ -121,9 +121,9 @@ public DBObject bsonify(final LoggingEvent loggingEvent) {
protected void addMDCInformation(DBObject bson, final Map<Object, Object> props) {
if (props != null && props.size() > 0) {
- // Copy properties into document
BasicDBObject mdcProperties = new BasicDBObject();
+ // Copy MDC properties into document
for (Map.Entry<Object, Object> entry : props.entrySet()) {
nullSafePut(mdcProperties, entry.getKey().toString(), entry.getValue().toString());
}
View
4 src/test/java/org/log4mongo/TestExtendedMongoDbAppender.java
@@ -49,7 +49,7 @@
private final static String MONGODB_APPENDER_NAME = "MongoDB";
private final static String LOG4J_PROPS = "src/test/resources/log4j_extended.properties";
- //private final static String LOG4J_PROPS = "src/test/resources/log4j.xml";
+ //private final static String LOG4J_PROPS = "src/test/resources/log4j_extended.xml";
private final Mongo mongo;
private final ExtendedMongoDbAppender appender;
@@ -104,7 +104,6 @@ public void testRootLevelProperties() throws Exception {
assertEquals(0L, countLogEntries());
Map<String, String> obj = new HashMap<String, String>() {
- //Unnamed Block.
{
put("key1", "value1");
put("key2", "value2");
@@ -127,7 +126,6 @@ public void testRootLevelProperties() throws Exception {
public void testObjectAsMessage() throws Exception {
assertEquals(0L, countLogEntries());
-
log.warn("Testing Object in Message");
long appNameCount = countLogEntriesWhere(BasicDBObjectBuilder.start().add("applicationName", "MyProject").get());
View
0 src/test/resources/log4j.xml → src/test/resources/log4j_extended.xml
File renamed without changes.

0 comments on commit c4868de

Please sign in to comment.