28
28
import com .mongodb .WriteConcern ;
29
29
import org .apache .log4j .AppenderSkeleton ;
30
30
import org .apache .log4j .Level ;
31
+ import org .apache .log4j .MDC ;
32
+ import org .apache .log4j .PatternLayout ;
31
33
import org .apache .log4j .spi .LoggingEvent ;
32
34
33
35
/**
@@ -42,11 +44,16 @@ public class MongoLog4jAppender extends AppenderSkeleton {
42
44
public static final String PROPERTIES = "properties" ;
43
45
public static final String TRACEBACK = "traceback" ;
44
46
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" ;
45
51
46
52
protected String host = "localhost" ;
47
53
protected int port = 27017 ;
48
54
protected String database = "logs" ;
49
- protected String collection = null ;
55
+ protected String collectionPattern = "%c" ;
56
+ protected PatternLayout collectionLayout = new PatternLayout (collectionPattern );
50
57
protected String applicationId = System .getProperty ("APPLICATION_ID" , null );
51
58
protected WriteConcern warnOrHigherWriteConcern = WriteConcern .SAFE ;
52
59
protected WriteConcern infoOrLowerWriteConcern = WriteConcern .NORMAL ;
@@ -84,12 +91,13 @@ public void setDatabase(String database) {
84
91
this .database = database ;
85
92
}
86
93
87
- public String getCollection () {
88
- return collection ;
94
+ public String getCollectionPattern () {
95
+ return collectionPattern ;
89
96
}
90
97
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 );
93
101
}
94
102
95
103
public String getApplicationId () {
@@ -133,7 +141,10 @@ protected void append(final LoggingEvent event) {
133
141
}
134
142
135
143
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
+ }
137
148
dbo .put (NAME , event .getLogger ().getName ());
138
149
dbo .put (LEVEL , event .getLevel ().toString ());
139
150
Calendar tstamp = Calendar .getInstance ();
@@ -162,19 +173,20 @@ protected void append(final LoggingEvent event) {
162
173
dbo .put (MESSAGE , event .getRenderedMessage ());
163
174
164
175
// 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 );
178
190
}
179
191
180
192
WriteConcern wc ;
0 commit comments