|
|
|
@ -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; |
|
|
|
|