Use Log4J pattern to generate collection name, fix for test data cleanup.

This commit is contained in:
Jon Brisbin
2011-03-30 16:21:23 -05:00
committed by J. Brisbin
parent 8e9cf3a9b1
commit 9b97c27599
4 changed files with 49 additions and 27 deletions

View File

@@ -28,6 +28,8 @@ import com.mongodb.Mongo;
import com.mongodb.WriteConcern;
import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.Level;
import org.apache.log4j.MDC;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.spi.LoggingEvent;
/**
@@ -42,11 +44,16 @@ public class MongoLog4jAppender extends AppenderSkeleton {
public static final String PROPERTIES = "properties";
public static final String TRACEBACK = "traceback";
public static final String MESSAGE = "message";
public static final String YEAR = "year";
public static final String MONTH = "month";
public static final String DAY = "day";
public static final String HOUR = "hour";
protected String host = "localhost";
protected int port = 27017;
protected String database = "logs";
protected String collection = null;
protected String collectionPattern = "%c";
protected PatternLayout collectionLayout = new PatternLayout(collectionPattern);
protected String applicationId = System.getProperty("APPLICATION_ID", null);
protected WriteConcern warnOrHigherWriteConcern = WriteConcern.SAFE;
protected WriteConcern infoOrLowerWriteConcern = WriteConcern.NORMAL;
@@ -84,12 +91,13 @@ public class MongoLog4jAppender extends AppenderSkeleton {
this.database = database;
}
public String getCollection() {
return collection;
public String getCollectionPattern() {
return collectionPattern;
}
public void setCollection(String collection) {
this.collection = collection;
public void setCollectionPattern(String collectionPattern) {
this.collectionPattern = collectionPattern;
this.collectionLayout = new PatternLayout(collectionPattern);
}
public String getApplicationId() {
@@ -133,7 +141,10 @@ public class MongoLog4jAppender extends AppenderSkeleton {
}
BasicDBObject dbo = new BasicDBObject();
dbo.put(APP_ID, applicationId);
if (null != applicationId) {
dbo.put(APP_ID, applicationId);
MDC.put(APP_ID, applicationId);
}
dbo.put(NAME, event.getLogger().getName());
dbo.put(LEVEL, event.getLevel().toString());
Calendar tstamp = Calendar.getInstance();
@@ -162,19 +173,20 @@ public class MongoLog4jAppender extends AppenderSkeleton {
dbo.put(MESSAGE, event.getRenderedMessage());
// Insert the document
String coll;
if (null == collection) {
// Use the category name
coll = event.getLogger().getName();
} else {
Calendar now = Calendar.getInstance();
coll = String.format(collection,
now.get(Calendar.YEAR),
now.get(Calendar.MONTH + 1),
now.get(Calendar.DAY_OF_MONTH),
now.get(Calendar.HOUR_OF_DAY),
event.getLevel().toString(),
event.getLogger().getName());
Calendar now = Calendar.getInstance();
MDC.put(YEAR, now.get(Calendar.YEAR));
MDC.put(MONTH, String.format("%1$02d", now.get(Calendar.MONTH) + 1));
MDC.put(DAY, String.format("%1$02d", now.get(Calendar.DAY_OF_MONTH)));
MDC.put(HOUR, String.format("%1$02d", now.get(Calendar.HOUR_OF_DAY)));
String coll = collectionLayout.format(event);
MDC.remove(YEAR);
MDC.remove(MONTH);
MDC.remove(DAY);
MDC.remove(HOUR);
if (null != applicationId) {
MDC.remove(APP_ID);
}
WriteConcern wc;

View File

@@ -20,6 +20,7 @@ import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import java.net.UnknownHostException;
import java.util.Calendar;
import com.mongodb.DB;
import com.mongodb.DBCursor;
@@ -38,13 +39,16 @@ public class AppenderTest {
private Logger log = Logger.getLogger(NAME);
private Mongo mongo;
private DB db;
private String collection;
@Before
public void setup() {
try {
mongo = new Mongo("localhost", 27017);
db = mongo.getDB("logs");
db.getCollection(NAME).drop();
Calendar now = Calendar.getInstance();
collection = String.valueOf(now.get(Calendar.YEAR)) + String.format("%1$02d", now.get(Calendar.MONTH) + 1);
db.getCollection(collection).drop();
} catch (UnknownHostException e) {
throw new RuntimeException(e.getMessage(), e);
}
@@ -57,7 +61,7 @@ public class AppenderTest {
log.warn("WARN message");
log.error("ERROR message");
DBCursor msgs = db.getCollection(NAME).find();
DBCursor msgs = db.getCollection(collection).find();
assertThat(msgs.count(), is(4));
}

View File

@@ -6,7 +6,7 @@ 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.collection = %1$4d%2$02d%3$02d
log4j.appender.stdout.collectionPattern = %X{year}%X{month}
log4j.appender.stdout.applicationId = my.application
log4j.appender.stdout.warnOrHigherWriteConcern = FSYNC_SAFE

View File

@@ -24,6 +24,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
@@ -39,13 +40,17 @@ public class MappingTests {
ApplicationContext applicationContext;
MongoTemplate template;
MongoMappingContext mappingContext;
@Before
public void setUp() {
public void setUp() throws InterruptedException {
applicationContext = new ClassPathXmlApplicationContext("/mapping.xml");
template = applicationContext.getBean(MongoTemplate.class);
mappingContext = applicationContext.getBean(MongoMappingContext.class);
}
@After
public void tearDown() {
template.dropCollection("person");
template.dropCollection("account");
}
@Test
@@ -69,7 +74,7 @@ public class MappingTests {
assertNotNull(result.get(0).getCustomId());
}
@Test
@Test
public void testPersonMapProperty() {
PersonMapProperty p = new PersonMapProperty(1234567, "Map", "Property");
Map<String, AccountPojo> accounts = new HashMap<String, AccountPojo>();
@@ -126,6 +131,7 @@ public class MappingTests {
assertThat(result.get(0).getAccounts(), notNullValue());
}
@SuppressWarnings({"unchecked"})
@Test
public void testUniqueIndex() {
Address addr = new Address();
@@ -145,7 +151,7 @@ public class MappingTests {
}
@Test
public void testEvents(){
public void testEvents() {
}