Use Log4J pattern to generate collection name, fix for test data cleanup.
This commit is contained in:
@@ -28,6 +28,8 @@ import com.mongodb.Mongo;
|
|||||||
import com.mongodb.WriteConcern;
|
import com.mongodb.WriteConcern;
|
||||||
import org.apache.log4j.AppenderSkeleton;
|
import org.apache.log4j.AppenderSkeleton;
|
||||||
import org.apache.log4j.Level;
|
import org.apache.log4j.Level;
|
||||||
|
import org.apache.log4j.MDC;
|
||||||
|
import org.apache.log4j.PatternLayout;
|
||||||
import org.apache.log4j.spi.LoggingEvent;
|
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 PROPERTIES = "properties";
|
||||||
public static final String TRACEBACK = "traceback";
|
public static final String TRACEBACK = "traceback";
|
||||||
public static final String MESSAGE = "message";
|
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 String host = "localhost";
|
||||||
protected int port = 27017;
|
protected int port = 27017;
|
||||||
protected String database = "logs";
|
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 String applicationId = System.getProperty("APPLICATION_ID", null);
|
||||||
protected WriteConcern warnOrHigherWriteConcern = WriteConcern.SAFE;
|
protected WriteConcern warnOrHigherWriteConcern = WriteConcern.SAFE;
|
||||||
protected WriteConcern infoOrLowerWriteConcern = WriteConcern.NORMAL;
|
protected WriteConcern infoOrLowerWriteConcern = WriteConcern.NORMAL;
|
||||||
@@ -84,12 +91,13 @@ public class MongoLog4jAppender extends AppenderSkeleton {
|
|||||||
this.database = database;
|
this.database = database;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getCollection() {
|
public String getCollectionPattern() {
|
||||||
return collection;
|
return collectionPattern;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setCollection(String collection) {
|
public void setCollectionPattern(String collectionPattern) {
|
||||||
this.collection = collection;
|
this.collectionPattern = collectionPattern;
|
||||||
|
this.collectionLayout = new PatternLayout(collectionPattern);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getApplicationId() {
|
public String getApplicationId() {
|
||||||
@@ -133,7 +141,10 @@ public class MongoLog4jAppender extends AppenderSkeleton {
|
|||||||
}
|
}
|
||||||
|
|
||||||
BasicDBObject dbo = new BasicDBObject();
|
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(NAME, event.getLogger().getName());
|
||||||
dbo.put(LEVEL, event.getLevel().toString());
|
dbo.put(LEVEL, event.getLevel().toString());
|
||||||
Calendar tstamp = Calendar.getInstance();
|
Calendar tstamp = Calendar.getInstance();
|
||||||
@@ -162,19 +173,20 @@ public class MongoLog4jAppender extends AppenderSkeleton {
|
|||||||
dbo.put(MESSAGE, event.getRenderedMessage());
|
dbo.put(MESSAGE, event.getRenderedMessage());
|
||||||
|
|
||||||
// Insert the document
|
// Insert the document
|
||||||
String coll;
|
Calendar now = Calendar.getInstance();
|
||||||
if (null == collection) {
|
MDC.put(YEAR, now.get(Calendar.YEAR));
|
||||||
// Use the category name
|
MDC.put(MONTH, String.format("%1$02d", now.get(Calendar.MONTH) + 1));
|
||||||
coll = event.getLogger().getName();
|
MDC.put(DAY, String.format("%1$02d", now.get(Calendar.DAY_OF_MONTH)));
|
||||||
} else {
|
MDC.put(HOUR, String.format("%1$02d", now.get(Calendar.HOUR_OF_DAY)));
|
||||||
Calendar now = Calendar.getInstance();
|
|
||||||
coll = String.format(collection,
|
String coll = collectionLayout.format(event);
|
||||||
now.get(Calendar.YEAR),
|
|
||||||
now.get(Calendar.MONTH + 1),
|
MDC.remove(YEAR);
|
||||||
now.get(Calendar.DAY_OF_MONTH),
|
MDC.remove(MONTH);
|
||||||
now.get(Calendar.HOUR_OF_DAY),
|
MDC.remove(DAY);
|
||||||
event.getLevel().toString(),
|
MDC.remove(HOUR);
|
||||||
event.getLogger().getName());
|
if (null != applicationId) {
|
||||||
|
MDC.remove(APP_ID);
|
||||||
}
|
}
|
||||||
|
|
||||||
WriteConcern wc;
|
WriteConcern wc;
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ import static org.hamcrest.Matchers.*;
|
|||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
|
import java.util.Calendar;
|
||||||
|
|
||||||
import com.mongodb.DB;
|
import com.mongodb.DB;
|
||||||
import com.mongodb.DBCursor;
|
import com.mongodb.DBCursor;
|
||||||
@@ -38,13 +39,16 @@ public class AppenderTest {
|
|||||||
private Logger log = Logger.getLogger(NAME);
|
private Logger log = Logger.getLogger(NAME);
|
||||||
private Mongo mongo;
|
private Mongo mongo;
|
||||||
private DB db;
|
private DB db;
|
||||||
|
private String collection;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setup() {
|
public void setup() {
|
||||||
try {
|
try {
|
||||||
mongo = new Mongo("localhost", 27017);
|
mongo = new Mongo("localhost", 27017);
|
||||||
db = mongo.getDB("logs");
|
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) {
|
} catch (UnknownHostException e) {
|
||||||
throw new RuntimeException(e.getMessage(), e);
|
throw new RuntimeException(e.getMessage(), e);
|
||||||
}
|
}
|
||||||
@@ -57,7 +61,7 @@ public class AppenderTest {
|
|||||||
log.warn("WARN message");
|
log.warn("WARN message");
|
||||||
log.error("ERROR message");
|
log.error("ERROR message");
|
||||||
|
|
||||||
DBCursor msgs = db.getCollection(NAME).find();
|
DBCursor msgs = db.getCollection(collection).find();
|
||||||
assertThat(msgs.count(), is(4));
|
assertThat(msgs.count(), is(4));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - <%m>%n
|
|||||||
log4j.appender.stdout.host = localhost
|
log4j.appender.stdout.host = localhost
|
||||||
log4j.appender.stdout.port = 27017
|
log4j.appender.stdout.port = 27017
|
||||||
log4j.appender.stdout.database = logs
|
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.applicationId = my.application
|
||||||
log4j.appender.stdout.warnOrHigherWriteConcern = FSYNC_SAFE
|
log4j.appender.stdout.warnOrHigherWriteConcern = FSYNC_SAFE
|
||||||
|
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ import java.util.HashMap;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
@@ -39,13 +40,17 @@ public class MappingTests {
|
|||||||
|
|
||||||
ApplicationContext applicationContext;
|
ApplicationContext applicationContext;
|
||||||
MongoTemplate template;
|
MongoTemplate template;
|
||||||
MongoMappingContext mappingContext;
|
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() throws InterruptedException {
|
||||||
applicationContext = new ClassPathXmlApplicationContext("/mapping.xml");
|
applicationContext = new ClassPathXmlApplicationContext("/mapping.xml");
|
||||||
template = applicationContext.getBean(MongoTemplate.class);
|
template = applicationContext.getBean(MongoTemplate.class);
|
||||||
mappingContext = applicationContext.getBean(MongoMappingContext.class);
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void tearDown() {
|
||||||
|
template.dropCollection("person");
|
||||||
|
template.dropCollection("account");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -69,7 +74,7 @@ public class MappingTests {
|
|||||||
assertNotNull(result.get(0).getCustomId());
|
assertNotNull(result.get(0).getCustomId());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testPersonMapProperty() {
|
public void testPersonMapProperty() {
|
||||||
PersonMapProperty p = new PersonMapProperty(1234567, "Map", "Property");
|
PersonMapProperty p = new PersonMapProperty(1234567, "Map", "Property");
|
||||||
Map<String, AccountPojo> accounts = new HashMap<String, AccountPojo>();
|
Map<String, AccountPojo> accounts = new HashMap<String, AccountPojo>();
|
||||||
@@ -126,6 +131,7 @@ public class MappingTests {
|
|||||||
assertThat(result.get(0).getAccounts(), notNullValue());
|
assertThat(result.get(0).getAccounts(), notNullValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings({"unchecked"})
|
||||||
@Test
|
@Test
|
||||||
public void testUniqueIndex() {
|
public void testUniqueIndex() {
|
||||||
Address addr = new Address();
|
Address addr = new Address();
|
||||||
@@ -145,7 +151,7 @@ public class MappingTests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testEvents(){
|
public void testEvents() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user