DATAMONGO-641 - Reformatting to prepare fix.
This commit is contained in:
@@ -21,188 +21,189 @@ import java.util.Arrays;
|
|||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import com.mongodb.BasicDBList;
|
|
||||||
import com.mongodb.BasicDBObject;
|
|
||||||
import com.mongodb.DB;
|
|
||||||
import com.mongodb.Mongo;
|
|
||||||
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.MDC;
|
||||||
import org.apache.log4j.PatternLayout;
|
import org.apache.log4j.PatternLayout;
|
||||||
import org.apache.log4j.spi.LoggingEvent;
|
import org.apache.log4j.spi.LoggingEvent;
|
||||||
|
|
||||||
|
import com.mongodb.BasicDBList;
|
||||||
|
import com.mongodb.BasicDBObject;
|
||||||
|
import com.mongodb.DB;
|
||||||
|
import com.mongodb.Mongo;
|
||||||
|
import com.mongodb.WriteConcern;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Jon Brisbin <jbrisbin@vmware.com>
|
* @author Jon Brisbin <jbrisbin@vmware.com>
|
||||||
*/
|
*/
|
||||||
public class MongoLog4jAppender extends AppenderSkeleton {
|
public class MongoLog4jAppender extends AppenderSkeleton {
|
||||||
|
|
||||||
public static final String LEVEL = "level";
|
public static final String LEVEL = "level";
|
||||||
public static final String NAME = "name";
|
public static final String NAME = "name";
|
||||||
public static final String APP_ID = "applicationId";
|
public static final String APP_ID = "applicationId";
|
||||||
public static final String TIMESTAMP = "timestamp";
|
public static final String TIMESTAMP = "timestamp";
|
||||||
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 YEAR = "year";
|
||||||
public static final String MONTH = "month";
|
public static final String MONTH = "month";
|
||||||
public static final String DAY = "day";
|
public static final String DAY = "day";
|
||||||
public static final String HOUR = "hour";
|
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 collectionPattern = "%c";
|
protected String collectionPattern = "%c";
|
||||||
protected PatternLayout collectionLayout = new PatternLayout(collectionPattern);
|
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;
|
||||||
protected Mongo mongo;
|
protected Mongo mongo;
|
||||||
protected DB db;
|
protected DB db;
|
||||||
|
|
||||||
public MongoLog4jAppender() {
|
public MongoLog4jAppender() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public MongoLog4jAppender(boolean isActive) {
|
public MongoLog4jAppender(boolean isActive) {
|
||||||
super(isActive);
|
super(isActive);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getHost() {
|
public String getHost() {
|
||||||
return host;
|
return host;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setHost(String host) {
|
public void setHost(String host) {
|
||||||
this.host = host;
|
this.host = host;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getPort() {
|
public int getPort() {
|
||||||
return port;
|
return port;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setPort(int port) {
|
public void setPort(int port) {
|
||||||
this.port = port;
|
this.port = port;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getDatabase() {
|
public String getDatabase() {
|
||||||
return database;
|
return database;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setDatabase(String database) {
|
public void setDatabase(String database) {
|
||||||
this.database = database;
|
this.database = database;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getCollectionPattern() {
|
public String getCollectionPattern() {
|
||||||
return collectionPattern;
|
return collectionPattern;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setCollectionPattern(String collectionPattern) {
|
public void setCollectionPattern(String collectionPattern) {
|
||||||
this.collectionPattern = collectionPattern;
|
this.collectionPattern = collectionPattern;
|
||||||
this.collectionLayout = new PatternLayout(collectionPattern);
|
this.collectionLayout = new PatternLayout(collectionPattern);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getApplicationId() {
|
public String getApplicationId() {
|
||||||
return applicationId;
|
return applicationId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setApplicationId(String applicationId) {
|
public void setApplicationId(String applicationId) {
|
||||||
this.applicationId = applicationId;
|
this.applicationId = applicationId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setWarnOrHigherWriteConcern(String wc) {
|
public void setWarnOrHigherWriteConcern(String wc) {
|
||||||
this.warnOrHigherWriteConcern = WriteConcern.valueOf(wc);
|
this.warnOrHigherWriteConcern = WriteConcern.valueOf(wc);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getWarnOrHigherWriteConcern() {
|
public String getWarnOrHigherWriteConcern() {
|
||||||
return warnOrHigherWriteConcern.toString();
|
return warnOrHigherWriteConcern.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getInfoOrLowerWriteConcern() {
|
public String getInfoOrLowerWriteConcern() {
|
||||||
return infoOrLowerWriteConcern.toString();
|
return infoOrLowerWriteConcern.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setInfoOrLowerWriteConcern(String wc) {
|
public void setInfoOrLowerWriteConcern(String wc) {
|
||||||
this.infoOrLowerWriteConcern = WriteConcern.valueOf(wc);
|
this.infoOrLowerWriteConcern = WriteConcern.valueOf(wc);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void connectToMongo() throws UnknownHostException {
|
protected void connectToMongo() throws UnknownHostException {
|
||||||
this.mongo = new Mongo(host, port);
|
this.mongo = new Mongo(host, port);
|
||||||
this.db = mongo.getDB(database);
|
this.db = mongo.getDB(database);
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings({"unchecked"})
|
@SuppressWarnings({ "unchecked" })
|
||||||
@Override
|
@Override
|
||||||
protected void append(final LoggingEvent event) {
|
protected void append(final LoggingEvent event) {
|
||||||
if (null == db) {
|
if (null == db) {
|
||||||
try {
|
try {
|
||||||
connectToMongo();
|
connectToMongo();
|
||||||
} catch (UnknownHostException e) {
|
} catch (UnknownHostException e) {
|
||||||
throw new RuntimeException(e.getMessage(), e);
|
throw new RuntimeException(e.getMessage(), e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
BasicDBObject dbo = new BasicDBObject();
|
BasicDBObject dbo = new BasicDBObject();
|
||||||
if (null != applicationId) {
|
if (null != applicationId) {
|
||||||
dbo.put(APP_ID, applicationId);
|
dbo.put(APP_ID, applicationId);
|
||||||
MDC.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();
|
||||||
tstamp.setTimeInMillis(event.getTimeStamp());
|
tstamp.setTimeInMillis(event.getTimeStamp());
|
||||||
dbo.put(TIMESTAMP, tstamp.getTime());
|
dbo.put(TIMESTAMP, tstamp.getTime());
|
||||||
|
|
||||||
// Copy properties into document
|
// Copy properties into document
|
||||||
Map<Object, Object> props = event.getProperties();
|
Map<Object, Object> props = event.getProperties();
|
||||||
if (null != props && props.size() > 0) {
|
if (null != props && props.size() > 0) {
|
||||||
BasicDBObject propsDbo = new BasicDBObject();
|
BasicDBObject propsDbo = new BasicDBObject();
|
||||||
for (Map.Entry<Object, Object> entry : props.entrySet()) {
|
for (Map.Entry<Object, Object> entry : props.entrySet()) {
|
||||||
propsDbo.put(entry.getKey().toString(), entry.getValue().toString());
|
propsDbo.put(entry.getKey().toString(), entry.getValue().toString());
|
||||||
}
|
}
|
||||||
dbo.put(PROPERTIES, propsDbo);
|
dbo.put(PROPERTIES, propsDbo);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Copy traceback info (if there is any) into the document
|
// Copy traceback info (if there is any) into the document
|
||||||
String[] traceback = event.getThrowableStrRep();
|
String[] traceback = event.getThrowableStrRep();
|
||||||
if (null != traceback && traceback.length > 0) {
|
if (null != traceback && traceback.length > 0) {
|
||||||
BasicDBList tbDbo = new BasicDBList();
|
BasicDBList tbDbo = new BasicDBList();
|
||||||
tbDbo.addAll(Arrays.asList(traceback));
|
tbDbo.addAll(Arrays.asList(traceback));
|
||||||
dbo.put(TRACEBACK, tbDbo);
|
dbo.put(TRACEBACK, tbDbo);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Put the rendered message into the document
|
// Put the rendered message into the document
|
||||||
dbo.put(MESSAGE, event.getRenderedMessage());
|
dbo.put(MESSAGE, event.getRenderedMessage());
|
||||||
|
|
||||||
// Insert the document
|
// Insert the document
|
||||||
Calendar now = Calendar.getInstance();
|
Calendar now = Calendar.getInstance();
|
||||||
MDC.put(YEAR, now.get(Calendar.YEAR));
|
MDC.put(YEAR, now.get(Calendar.YEAR));
|
||||||
MDC.put(MONTH, String.format("%1$02d", now.get(Calendar.MONTH) + 1));
|
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(DAY, String.format("%1$02d", now.get(Calendar.DAY_OF_MONTH)));
|
||||||
MDC.put(HOUR, String.format("%1$02d", now.get(Calendar.HOUR_OF_DAY)));
|
MDC.put(HOUR, String.format("%1$02d", now.get(Calendar.HOUR_OF_DAY)));
|
||||||
|
|
||||||
String coll = collectionLayout.format(event);
|
String coll = collectionLayout.format(event);
|
||||||
|
|
||||||
MDC.remove(YEAR);
|
MDC.remove(YEAR);
|
||||||
MDC.remove(MONTH);
|
MDC.remove(MONTH);
|
||||||
MDC.remove(DAY);
|
MDC.remove(DAY);
|
||||||
MDC.remove(HOUR);
|
MDC.remove(HOUR);
|
||||||
if (null != applicationId) {
|
if (null != applicationId) {
|
||||||
MDC.remove(APP_ID);
|
MDC.remove(APP_ID);
|
||||||
}
|
}
|
||||||
|
|
||||||
WriteConcern wc;
|
WriteConcern wc;
|
||||||
if (event.getLevel().isGreaterOrEqual(Level.WARN)) {
|
if (event.getLevel().isGreaterOrEqual(Level.WARN)) {
|
||||||
wc = warnOrHigherWriteConcern;
|
wc = warnOrHigherWriteConcern;
|
||||||
} else {
|
} else {
|
||||||
wc = infoOrLowerWriteConcern;
|
wc = infoOrLowerWriteConcern;
|
||||||
}
|
}
|
||||||
db.getCollection(coll).insert(dbo, wc);
|
db.getCollection(coll).insert(dbo, wc);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void close() {
|
public void close() {
|
||||||
mongo.close();
|
mongo.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean requiresLayout() {
|
public boolean requiresLayout() {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,41 +35,41 @@ import org.junit.Test;
|
|||||||
*/
|
*/
|
||||||
public class AppenderTest {
|
public class AppenderTest {
|
||||||
|
|
||||||
private static final String NAME = AppenderTest.class.getName();
|
private static final String NAME = AppenderTest.class.getName();
|
||||||
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;
|
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");
|
||||||
Calendar now = Calendar.getInstance();
|
Calendar now = Calendar.getInstance();
|
||||||
collection = String.valueOf(now.get(Calendar.YEAR)) + String.format("%1$02d", now.get(Calendar.MONTH) + 1);
|
collection = String.valueOf(now.get(Calendar.YEAR)) + String.format("%1$02d", now.get(Calendar.MONTH) + 1);
|
||||||
db.getCollection(collection).drop();
|
db.getCollection(collection).drop();
|
||||||
} catch (UnknownHostException e) {
|
} catch (UnknownHostException e) {
|
||||||
throw new RuntimeException(e.getMessage(), e);
|
throw new RuntimeException(e.getMessage(), e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testLogging() {
|
public void testLogging() {
|
||||||
log.debug("DEBUG message");
|
log.debug("DEBUG message");
|
||||||
log.info("INFO message");
|
log.info("INFO message");
|
||||||
log.warn("WARN message");
|
log.warn("WARN message");
|
||||||
log.error("ERROR message");
|
log.error("ERROR message");
|
||||||
|
|
||||||
DBCursor msgs = db.getCollection(collection).find();
|
DBCursor msgs = db.getCollection(collection).find();
|
||||||
assertThat(msgs.count(), is(4));
|
assertThat(msgs.count(), is(4));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testProperties() {
|
public void testProperties() {
|
||||||
MDC.put("property", "one");
|
MDC.put("property", "one");
|
||||||
log.debug("DEBUG message");
|
log.debug("DEBUG message");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user