Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
*.pyc
*.DS_Store
out/*
out/*
target/*
.idea/*
13 changes: 0 additions & 13 deletions .idea/libraries/common.xml

This file was deleted.

36 changes: 31 additions & 5 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,29 @@ This is a very raw project at the moment it still needs some more TLC and testin

Installation
------------
Copy the raven-java-0.2.jar file to your java classpath and then configure log4j to use the SentryAppender.
You'll need Maven 2 to build the project::

The raven-java-0.2.jar is a self contained jar file, all dependencies are included, so this jar should be all you need.
$ cd raven-java
$ mvn package -Dmaven.test.skip

There is an example project checked into github.com where you can see an example log4j config file.
The last step will build the standalone raven-java jar file but also a jar file containing raven-java and all dependencies, which
you'll find in the target directory of the project.

**Option 1**: add raven-java as a dependency when you're using Maven::

<dependency>
<groupId>net.kencochrane</groupId>
<artifactId>raven-java</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>

**Option 2**: add the plain jar and the jar files of all dependencies to your classpath

**Option 3**: add the self contained jar file to your classpath

Log4J configuration
-------------------
Check out src/test/java/resources/log4j_configuration.txt where you can see an example log4j config file.

You will need to add the SentryAppender and the sentry_dsn properties.

Expand All @@ -24,15 +42,23 @@ Log4j Config example::
log4j.appender.sentry=net.kencochrane.sentry.SentryAppender
log4j.appender.sentry.sentry_dsn=http://b4935bdd78624092ac2bc70fdcdb6f5a:7a37d9ad4765428180316bfec91a27ef@localhost:8000/1

Proxy
~~~~~
If you need to use a proxy for HTTP transport, you can configure it as well::

log4j.appender.sentry.proxy=HTTP:proxyhost:proxyport

Sentry Versions Supported
-------------------------
This client has been tested with Sentry 2.7 and 2.8, and only very briefly.

Other
-----
If you want to generate the javadocs for this project, simply run ``mvn javadoc:javadoc`` and you'll be able to browse the
docs from the target directory of the project.

TODO
----
- Add a ant task to build the jar files (I made this first one from intellij (10.5 community edition) File-> Project structure -> artifacts.
- Make this maven friendly. Not familiar with Maven, so maybe someone else can help with this.
- Create better documentation
- Add unit tests
- Add more examples
Expand Down
3 changes: 0 additions & 3 deletions example/run.sh

This file was deleted.

3 changes: 0 additions & 3 deletions example/run2.sh

This file was deleted.

34 changes: 0 additions & 34 deletions example/src/net/kencochrane/sentry/SentryExample.java

This file was deleted.

Binary file removed lib/commons-codec-1.4.jar
Binary file not shown.
Binary file removed lib/commons-logging-1.1.1.jar
Binary file not shown.
Binary file removed lib/httpclient-4.1.2.jar
Binary file not shown.
Binary file removed lib/httpcore-4.1.2.jar
Binary file not shown.
Binary file removed lib/httpmime-4.1.2.jar
Binary file not shown.
Binary file removed lib/json_simple-1.1.jar
Binary file not shown.
Binary file removed lib/log4j-1.2.16.jar
Binary file not shown.
24 changes: 21 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
<version>1.6</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20090211</version>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
Expand Down Expand Up @@ -58,6 +58,24 @@
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.3</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand Down
3 changes: 0 additions & 3 deletions src/META-INF/MANIFEST.MF

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.kencochrane.sentry;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import java.io.IOException;
Expand Down Expand Up @@ -58,11 +59,17 @@ public void setSentryDSN(String sentryDSN) {
* @param culprit Who we think caused the problem.
* @return JSON String of message body
*/
private String buildJSON(String message, String timestamp, String loggerClass, int logLevel, String culprit) {
private String buildJSON(String message, String timestamp, String loggerClass, int logLevel, String culprit, Throwable exception) {
JSONObject obj = new JSONObject();
obj.put("event_id", RavenUtils.getRandomUUID()); //Hexadecimal string representing a uuid4 value.
obj.put("checksum", RavenUtils.calculateChecksum(message));
obj.put("culprit", culprit);
if (exception == null) {
obj.put("culprit", culprit);
} else {
obj.put("culprit", determineCulprit(exception));
obj.put("sentry.interfaces.Exception", buildException(exception));
obj.put("sentry.interfaces.Stacktrace", buildStacktrace(exception));
}
obj.put("timestamp", timestamp);
obj.put("message", message);
obj.put("project", getConfig().getProjectId());
Expand All @@ -72,6 +79,63 @@ private String buildJSON(String message, String timestamp, String loggerClass, i
return obj.toJSONString();
}

/**
* Determines the class and method name where the root cause exception occurred.
*
* @param exception exception
* @return the culprit
*/
private String determineCulprit(Throwable exception) {
Throwable cause = exception;
String culprit = null;
while (cause != null) {
StackTraceElement[] elements = cause.getStackTrace();
if (elements.length > 0) {
StackTraceElement trace = elements[0];
culprit = trace.getClassName() + "." + trace.getMethodName();
}
cause = cause.getCause();
}
return culprit;
}

private JSONObject buildException(Throwable exception) {
JSONObject json = new JSONObject();
json.put("type", exception.getClass().getSimpleName());
json.put("value", exception.getMessage());
json.put("module", exception.getClass().getPackage().getName());
return json;
}

private JSONObject buildStacktrace(Throwable exception) {
JSONArray array = new JSONArray();
Throwable cause = exception;
while (cause != null) {
StackTraceElement[] elements = cause.getStackTrace();
for (int index = 0; index < elements.length; ++index) {
if (index == 0) {
JSONObject causedByFrame = new JSONObject();
String msg = "Caused by: " + cause.getClass().getName();
if (cause.getMessage() != null) {
msg += " (\"" + cause.getMessage() + "\")";
}
causedByFrame.put("filename", msg);
causedByFrame.put("lineno", -1);
array.add(causedByFrame);
}
StackTraceElement element = elements[index];
JSONObject frame = new JSONObject();
frame.put("filename", element.getClassName());
frame.put("function", element.getMethodName());
frame.put("lineno", element.getLineNumber());
array.add(frame);
}
cause = cause.getCause();
}
JSONObject stacktrace = new JSONObject();
stacktrace.put("frames", array);
return stacktrace;
}

/**
* Take the raw message body and get it ready for sending. Encode and compress it.
Expand All @@ -98,11 +162,12 @@ private String buildMessageBody(String jsonMessage) {
* @param loggerClass The class associated with the log message
* @param logLevel int value for Log level for message (DEBUG, ERROR, INFO, etc.)
* @param culprit Who we think caused the problem.
* @param exception exception causing the problem
* @return Encode and compressed version of the JSON Message body
*/
private String buildMessage(String message, String timestamp, String loggerClass, int logLevel, String culprit) {
private String buildMessage(String message, String timestamp, String loggerClass, int logLevel, String culprit, Throwable exception) {
// get the json version of the body
String jsonMessage = buildJSON(message, timestamp, loggerClass, logLevel, culprit);
String jsonMessage = buildJSON(message, timestamp, loggerClass, logLevel, culprit, exception);

// compress and encode the json message.
return buildMessageBody(jsonMessage);
Expand Down Expand Up @@ -183,11 +248,13 @@ private void sendMessage(String messageBody, long timestamp) {
* @param loggerClass The class associated with the log message
* @param logLevel int value for Log level for message (DEBUG, ERROR, INFO, etc.)
* @param culprit Who we think caused the problem.
* @param exception exception that occurred
*/
public void logMessage(String theLogMessage, long timestamp, String loggerClass, int logLevel, String culprit) {
public void logMessage(String theLogMessage, long timestamp, String loggerClass, int logLevel, String culprit, Throwable exception) {
String timestampDate = RavenUtils.getTimestampString(timestamp);

String message = buildMessage(theLogMessage, timestampDate, loggerClass, logLevel, culprit);
String message = buildMessage(theLogMessage, timestampDate, loggerClass, logLevel, culprit, exception);
sendMessage(message, timestamp);
}

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package net.kencochrane.sentry;

import org.apache.commons.lang.time.DateFormatUtils;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.security.SignatureException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
import java.util.zip.CRC32;
Expand All @@ -25,7 +26,6 @@
public class RavenUtils {

private static final String HMAC_SHA1_ALGORITHM = "HmacSHA1";
private static final SimpleDateFormat ISO8601FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");

/**
* Computes RFC 2104-compliant HMAC signature.
Expand Down Expand Up @@ -83,10 +83,7 @@ public static String getHostname() {
* @return ISO8601 formatted date as a String
*/
public static String getDateAsISO8601String(Date date) {
String result = ISO8601FORMAT.format(date);
result = result.substring(0, result.length() - 2)
+ ":" + result.substring(result.length() - 2);
return result;
return DateFormatUtils.ISO_DATETIME_FORMAT.format(date);
}

/**
Expand All @@ -95,8 +92,7 @@ public static String getDateAsISO8601String(Date date) {
* @return timestamp for now as long
*/
public static long getTimestampLong() {
java.util.Date date = new java.util.Date();
return date.getTime();
return System.currentTimeMillis();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.spi.LoggingEvent;
import org.apache.log4j.spi.ThrowableInformation;

/**
* User: ken cochrane
Expand Down Expand Up @@ -52,7 +53,9 @@ protected void append(LoggingEvent loggingEvent) {
RavenClient client = new RavenClient(getSentry_dsn(), getProxy());

// send the message to the sentry server
client.logMessage(logMessage, timestamp, loggingClass, logLevel, culprit);
ThrowableInformation throwableInformation = loggingEvent.getThrowableInformation();
Throwable throwable = (throwableInformation == null ? null : throwableInformation.getThrowable());
client.logMessage(logMessage, timestamp, loggingClass, logLevel, culprit, throwable);

} catch (Exception e) {
System.err.println(e);
Expand Down
Loading