Skip to content

Commit 9b97c27

Browse files
committed
Use Log4J pattern to generate collection name, fix for test data cleanup.
1 parent 8e9cf3a commit 9b97c27

File tree

4 files changed

+49
-27
lines changed

4 files changed

+49
-27
lines changed

spring-data-mongodb-log4j/src/main/java/org/springframework/data/document/mongodb/log4j/MongoLog4jAppender.java

+31-19
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import com.mongodb.WriteConcern;
2929
import org.apache.log4j.AppenderSkeleton;
3030
import org.apache.log4j.Level;
31+
import org.apache.log4j.MDC;
32+
import org.apache.log4j.PatternLayout;
3133
import org.apache.log4j.spi.LoggingEvent;
3234

3335
/**
@@ -42,11 +44,16 @@ public class MongoLog4jAppender extends AppenderSkeleton {
4244
public static final String PROPERTIES = "properties";
4345
public static final String TRACEBACK = "traceback";
4446
public static final String MESSAGE = "message";
47+
public static final String YEAR = "year";
48+
public static final String MONTH = "month";
49+
public static final String DAY = "day";
50+
public static final String HOUR = "hour";
4551

4652
protected String host = "localhost";
4753
protected int port = 27017;
4854
protected String database = "logs";
49-
protected String collection = null;
55+
protected String collectionPattern = "%c";
56+
protected PatternLayout collectionLayout = new PatternLayout(collectionPattern);
5057
protected String applicationId = System.getProperty("APPLICATION_ID", null);
5158
protected WriteConcern warnOrHigherWriteConcern = WriteConcern.SAFE;
5259
protected WriteConcern infoOrLowerWriteConcern = WriteConcern.NORMAL;
@@ -84,12 +91,13 @@ public void setDatabase(String database) {
8491
this.database = database;
8592
}
8693

87-
public String getCollection() {
88-
return collection;
94+
public String getCollectionPattern() {
95+
return collectionPattern;
8996
}
9097

91-
public void setCollection(String collection) {
92-
this.collection = collection;
98+
public void setCollectionPattern(String collectionPattern) {
99+
this.collectionPattern = collectionPattern;
100+
this.collectionLayout = new PatternLayout(collectionPattern);
93101
}
94102

95103
public String getApplicationId() {
@@ -133,7 +141,10 @@ protected void append(final LoggingEvent event) {
133141
}
134142

135143
BasicDBObject dbo = new BasicDBObject();
136-
dbo.put(APP_ID, applicationId);
144+
if (null != applicationId) {
145+
dbo.put(APP_ID, applicationId);
146+
MDC.put(APP_ID, applicationId);
147+
}
137148
dbo.put(NAME, event.getLogger().getName());
138149
dbo.put(LEVEL, event.getLevel().toString());
139150
Calendar tstamp = Calendar.getInstance();
@@ -162,19 +173,20 @@ protected void append(final LoggingEvent event) {
162173
dbo.put(MESSAGE, event.getRenderedMessage());
163174

164175
// Insert the document
165-
String coll;
166-
if (null == collection) {
167-
// Use the category name
168-
coll = event.getLogger().getName();
169-
} else {
170-
Calendar now = Calendar.getInstance();
171-
coll = String.format(collection,
172-
now.get(Calendar.YEAR),
173-
now.get(Calendar.MONTH + 1),
174-
now.get(Calendar.DAY_OF_MONTH),
175-
now.get(Calendar.HOUR_OF_DAY),
176-
event.getLevel().toString(),
177-
event.getLogger().getName());
176+
Calendar now = Calendar.getInstance();
177+
MDC.put(YEAR, now.get(Calendar.YEAR));
178+
MDC.put(MONTH, String.format("%1$02d", now.get(Calendar.MONTH) + 1));
179+
MDC.put(DAY, String.format("%1$02d", now.get(Calendar.DAY_OF_MONTH)));
180+
MDC.put(HOUR, String.format("%1$02d", now.get(Calendar.HOUR_OF_DAY)));
181+
182+
String coll = collectionLayout.format(event);
183+
184+
MDC.remove(YEAR);
185+
MDC.remove(MONTH);
186+
MDC.remove(DAY);
187+
MDC.remove(HOUR);
188+
if (null != applicationId) {
189+
MDC.remove(APP_ID);
178190
}
179191

180192
WriteConcern wc;

spring-data-mongodb-log4j/src/test/java/org/springframework/data/document/mongodb/log4j/AppenderTest.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static org.junit.Assert.*;
2121

2222
import java.net.UnknownHostException;
23+
import java.util.Calendar;
2324

2425
import com.mongodb.DB;
2526
import com.mongodb.DBCursor;
@@ -38,13 +39,16 @@ public class AppenderTest {
3839
private Logger log = Logger.getLogger(NAME);
3940
private Mongo mongo;
4041
private DB db;
42+
private String collection;
4143

4244
@Before
4345
public void setup() {
4446
try {
4547
mongo = new Mongo("localhost", 27017);
4648
db = mongo.getDB("logs");
47-
db.getCollection(NAME).drop();
49+
Calendar now = Calendar.getInstance();
50+
collection = String.valueOf(now.get(Calendar.YEAR)) + String.format("%1$02d", now.get(Calendar.MONTH) + 1);
51+
db.getCollection(collection).drop();
4852
} catch (UnknownHostException e) {
4953
throw new RuntimeException(e.getMessage(), e);
5054
}
@@ -57,7 +61,7 @@ public void testLogging() {
5761
log.warn("WARN message");
5862
log.error("ERROR message");
5963

60-
DBCursor msgs = db.getCollection(NAME).find();
64+
DBCursor msgs = db.getCollection(collection).find();
6165
assertThat(msgs.count(), is(4));
6266

6367
}

spring-data-mongodb-log4j/src/test/resources/log4j.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - <%m>%n
66
log4j.appender.stdout.host = localhost
77
log4j.appender.stdout.port = 27017
88
log4j.appender.stdout.database = logs
9-
log4j.appender.stdout.collection = %1$4d%2$02d%3$02d
9+
log4j.appender.stdout.collectionPattern = %X{year}%X{month}
1010
log4j.appender.stdout.applicationId = my.application
1111
log4j.appender.stdout.warnOrHigherWriteConcern = FSYNC_SAFE
1212

spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/mapping/MappingTests.java

+11-5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.List;
2525
import java.util.Map;
2626

27+
import org.junit.After;
2728
import org.junit.Before;
2829
import org.junit.Test;
2930
import org.springframework.context.ApplicationContext;
@@ -39,13 +40,17 @@ public class MappingTests {
3940

4041
ApplicationContext applicationContext;
4142
MongoTemplate template;
42-
MongoMappingContext mappingContext;
4343

4444
@Before
45-
public void setUp() {
45+
public void setUp() throws InterruptedException {
4646
applicationContext = new ClassPathXmlApplicationContext("/mapping.xml");
4747
template = applicationContext.getBean(MongoTemplate.class);
48-
mappingContext = applicationContext.getBean(MongoMappingContext.class);
48+
}
49+
50+
@After
51+
public void tearDown() {
52+
template.dropCollection("person");
53+
template.dropCollection("account");
4954
}
5055

5156
@Test
@@ -69,7 +74,7 @@ public void testPersonWithCustomIdName() {
6974
assertNotNull(result.get(0).getCustomId());
7075
}
7176

72-
@Test
77+
@Test
7378
public void testPersonMapProperty() {
7479
PersonMapProperty p = new PersonMapProperty(1234567, "Map", "Property");
7580
Map<String, AccountPojo> accounts = new HashMap<String, AccountPojo>();
@@ -126,6 +131,7 @@ public void testWriteEntity() {
126131
assertThat(result.get(0).getAccounts(), notNullValue());
127132
}
128133

134+
@SuppressWarnings({"unchecked"})
129135
@Test
130136
public void testUniqueIndex() {
131137
Address addr = new Address();
@@ -145,7 +151,7 @@ public void testUniqueIndex() {
145151
}
146152

147153
@Test
148-
public void testEvents(){
154+
public void testEvents() {
149155

150156
}
151157

0 commit comments

Comments
 (0)