forked from qos-ch/logback
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MongoDB classic appender, 100% integration test coverage (requires Mo…
…ngoDB running)
- Loading branch information
1 parent
0822661
commit 858aceb
Showing
11 changed files
with
712 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
logback-classic/src/main/java/ch/qos/logback/classic/db/mongo/MongoDBAppender.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package ch.qos.logback.classic.db.mongo; | ||
|
||
import ch.qos.logback.classic.spi.IThrowableProxy; | ||
import ch.qos.logback.classic.spi.LoggingEvent; | ||
import ch.qos.logback.classic.spi.StackTraceElementProxy; | ||
import ch.qos.logback.core.db.mongo.MongoDBAppenderBase; | ||
import com.mongodb.BasicDBList; | ||
import com.mongodb.BasicDBObject; | ||
|
||
import java.util.Date; | ||
|
||
/** | ||
* @author Tomasz Nurkiewicz | ||
* @since 02.04.11, 15:01 | ||
*/ | ||
public class MongoDBAppender extends MongoDBAppenderBase<LoggingEvent> { | ||
|
||
private boolean includeCallerData; | ||
|
||
@Override | ||
protected BasicDBObject toMongoDocument(LoggingEvent event) { | ||
final BasicDBObject doc = new BasicDBObject(); | ||
doc.append("timeStamp", new Date(event.getTimeStamp())); | ||
doc.append("level", event.getLevel().levelStr); | ||
doc.append("thread", event.getThreadName()); | ||
if (event.getMdc() != null && !event.getMdc().isEmpty()) | ||
doc.append("mdc", event.getMdc()); | ||
doc.append("logger", event.getLoggerName()); | ||
if (includeCallerData) | ||
doc.append("callerData", toDocument(event.getCallerData())); | ||
doc.append("message", event.getFormattedMessage()); | ||
if (event.getArgumentArray() != null && event.getArgumentArray().length > 0) | ||
doc.append("arguments", event.getArgumentArray()); | ||
appendThrowableIfAvailable(doc, event); | ||
return doc; | ||
} | ||
|
||
private BasicDBList toDocument(StackTraceElement[] callerData) { | ||
final BasicDBList dbList = new BasicDBList(); | ||
for (final StackTraceElement ste : callerData) { | ||
dbList.add( | ||
new BasicDBObject() | ||
.append("file", ste.getFileName()) | ||
.append("class", ste.getClassName()) | ||
.append("method", ste.getMethodName()) | ||
.append("line", ste.getLineNumber()) | ||
.append("native", ste.isNativeMethod())); | ||
} | ||
return dbList; | ||
} | ||
|
||
private void appendThrowableIfAvailable(BasicDBObject doc, LoggingEvent event) { | ||
if (event.getThrowableProxy() != null) { | ||
final BasicDBObject val = toMongoDocument(event.getThrowableProxy()); | ||
doc.append("throwable", val); | ||
} | ||
} | ||
|
||
private BasicDBObject toMongoDocument(IThrowableProxy throwable) { | ||
final BasicDBObject throwableDoc = new BasicDBObject(); | ||
throwableDoc.append("class", throwable.getClassName()); | ||
throwableDoc.append("message", throwable.getMessage()); | ||
throwableDoc.append("stackTrace", toSteArray(throwable)); | ||
if (throwable.getCause() != null) | ||
throwableDoc.append("cause", toMongoDocument(throwable.getCause())); | ||
return throwableDoc; | ||
} | ||
|
||
private String[] toSteArray(IThrowableProxy throwableProxy) { | ||
final StackTraceElementProxy[] elementProxies = throwableProxy.getStackTraceElementProxyArray(); | ||
final int totalFrames = elementProxies.length - throwableProxy.getCommonFrames(); | ||
final String[] stackTraceElements = new String[totalFrames]; | ||
for (int i = 0; i < totalFrames; ++i) | ||
stackTraceElements[i] = elementProxies[i].getStackTraceElement().toString(); | ||
return stackTraceElements; | ||
} | ||
|
||
public void setIncludeCallerData(boolean includeCallerData) { | ||
this.includeCallerData = includeCallerData; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
logback-classic/src/test/input/joran/mongodb/all_params.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<configuration debug="true"> | ||
|
||
<appender name="MONGODB" class="ch.qos.logback.classic.db.mongo.MongoDBAppender"> | ||
<host>localhost</host> | ||
<port>27017</port> | ||
<dbName>db</dbName> | ||
<collectionName>loggingEvents</collectionName> | ||
<username>admin</username> | ||
<password>s3cr3t</password> | ||
<connectionsPerHost>10</connectionsPerHost> | ||
<threadsAllowedToBlockForConnectionMultiplier>5</threadsAllowedToBlockForConnectionMultiplier> | ||
<maxWaitTime>120000</maxWaitTime> | ||
<connectTimeout>0</connectTimeout> | ||
<socketTimeout>0</socketTimeout> | ||
<autoConnectRetry>false</autoConnectRetry> | ||
<slaveOk>false</slaveOk> | ||
<safe>false</safe> | ||
<w>0</w> | ||
<wtimeout>0</wtimeout> | ||
<fsync>false</fsync> | ||
<includeCallerData>false</includeCallerData> | ||
</appender> | ||
|
||
<root level="DEBUG"> | ||
<appender-ref ref="MONGODB"/> | ||
</root> | ||
|
||
</configuration> |
12 changes: 12 additions & 0 deletions
12
logback-classic/src/test/input/joran/mongodb/caller_data.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<configuration debug="true"> | ||
|
||
<appender name="MONGODB" class="ch.qos.logback.classic.db.mongo.MongoDBAppender"> | ||
<includeCallerData>true</includeCallerData> | ||
</appender> | ||
|
||
<root level="DEBUG"> | ||
<appender-ref ref="MONGODB"/> | ||
</root> | ||
|
||
</configuration> |
12 changes: 12 additions & 0 deletions
12
logback-classic/src/test/input/joran/mongodb/conn_failure.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<configuration debug="true"> | ||
|
||
<appender name="MONGODB" class="ch.qos.logback.classic.db.mongo.MongoDBAppender"> | ||
<port>27018</port> | ||
</appender> | ||
|
||
<root level="DEBUG"> | ||
<appender-ref ref="MONGODB"/> | ||
</root> | ||
|
||
</configuration> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<configuration debug="true"> | ||
|
||
<appender name="MONGODB" class="ch.qos.logback.classic.db.mongo.MongoDBAppender"/> | ||
|
||
<root level="DEBUG"> | ||
<appender-ref ref="MONGODB"/> | ||
</root> | ||
|
||
</configuration> |
12 changes: 12 additions & 0 deletions
12
logback-classic/src/test/input/joran/mongodb/server_failure.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<configuration debug="true"> | ||
|
||
<appender name="MONGODB" class="ch.qos.logback.classic.db.mongo.MongoDBAppender"> | ||
<host>xyz.abc</host> | ||
</appender> | ||
|
||
<root level="DEBUG"> | ||
<appender-ref ref="MONGODB"/> | ||
</root> | ||
|
||
</configuration> |
Oops, something went wrong.