Permalink
Browse files

Merge pull request #5 from blaulabs/host-config-for-appender

Host config for appender some little cosmetics changes todo
  • Loading branch information...
2 parents 52789ad + 6d3daa5 commit 5c191a5d3bc1dc1d040503b1be5ba840d5fbb336 @lucamarrocco lucamarrocco committed Sep 27, 2012
View
@@ -5,3 +5,5 @@ target
BACKLOG
FEFLOG
TODO
+bin
+.checkstyle
View
@@ -38,13 +38,26 @@ Log4j
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%d,%p] [%c{1}.%M:%L] %m%n
- log4j.appender.airbrake=code.lucamarrocco.airbrake.AirbrakeAppender
+ log4j.appender.airbrake=airbrake.AirbrakeAppender
log4j.appender.airbrake.api_key=YOUR_AIRBRAKE_API_KEY
#log4j.appender.airbrake.env=development
#log4j.appender.airbrake.env=production
log4j.appender.airbrake.env=test
log4j.appender.airbrake.enabled=true
+ #log4j.appender.airbrake.host=my.other.airbrake.host.io
+or in XML format:
+
+ <appender name="AIRBRAKE" class="airbrake.AirbrakeAppender">
+ <param name="api_key" value="YOUR_AIRBRAKE_API_KEY"/>
+ <param name="env" value="test"/>
+ <param name="enabled" value="true"/>
+ <!-- <param name="host" value="my.other.airbrake.host.io" /> -->
+ </appender>
+
+ <root>
+ <appender-ref ref="AIRBRAKE"/>
+ </root>
Directly
------------------------------
@@ -19,6 +19,8 @@
private Backtrace backtrace = new QuietRubyBacktrace();
+ private String host;
+
public AirbrakeAppender() {
setThreshold(Level.ERROR);
}
@@ -45,7 +47,7 @@ protected void append(final LoggingEvent loggingEvent) {
public void close() {}
public AirbrakeNotice newNoticeFor(final Throwable throwable) {
- return new AirbrakeNoticeBuilderUsingFilteredSystemProperties(apiKey, backtrace, throwable, env).newNotice();
+ return new AirbrakeNoticeBuilderUsingFilteredSystemProperties(apiKey, backtrace, throwable, env, host).newNotice();
}
private int notifyThrowableIn(final LoggingEvent loggingEvent) {
@@ -73,6 +75,10 @@ public void setEnv(final String env) {
this.env = env;
}
+ public void setHost(String host) {
+ this.host = host;
+ }
+
private boolean thereIsThrowableIn(final LoggingEvent loggingEvent) {
return loggingEvent.getThrowableInformation() != null;
}
@@ -34,9 +34,11 @@
private final String component;
+ private String host;
+
public AirbrakeNotice(final String apiKey, String projectRoot, String environmentName, final String errorMessage, String errorClass,
final Backtrace backtrace, final Map<String, Object> request, final Map<String, Object> session, final Map<String, Object> environment,
- final List<String> environmentFilters, boolean hasRequest, String url, String component) {
+ final List<String> environmentFilters, boolean hasRequest, String url, String component, String host) {
this.apiKey = apiKey;
this.projectRoot = projectRoot;
this.environmentName = environmentName;
@@ -48,6 +50,7 @@ public AirbrakeNotice(final String apiKey, String projectRoot, String environmen
this.hasRequest = hasRequest;
this.url = url;
this.component = component;
+ this.host = host;
filter(environment, environmentFilters);
}
@@ -113,4 +116,8 @@ public String component() {
public String projectRoot() {
return projectRoot;
}
+
+ public String host() {
+ return host;
+ }
}
@@ -39,33 +39,46 @@
private String component;
- public AirbrakeNoticeBuilder(final String apiKey, final Backtrace backtraceBuilder, final Throwable throwable, final String env) {
- this(apiKey, throwable.getMessage(), env);
+ private String host;
+
+ final static String DEFAULT_HOST = "api.airbrake.io";
+
+ public AirbrakeNoticeBuilder(final String apiKey, final Backtrace backtraceBuilder, final Throwable throwable, final String env, final String host) {
+ this(apiKey, throwable.getMessage(), env, host);
this.backtraceBuilder = backtraceBuilder;
errorClass(throwable);
backtrace(throwable);
}
+ public AirbrakeNoticeBuilder(final String apiKey, final Backtrace backtraceBuilder, final Throwable throwable, final String env) {
+ this(apiKey, backtraceBuilder, throwable, env, null);
+ }
+
public AirbrakeNoticeBuilder(final String apiKey, final String errorMessage) {
- this(apiKey, errorMessage, "test");
+ this(apiKey, errorMessage, "test", null);
}
public AirbrakeNoticeBuilder(final String apiKey, final String errorMessage, final String env) {
+ this(apiKey, errorMessage, env, null);
+ }
+
+ public AirbrakeNoticeBuilder(final String apiKey, final String errorMessage, final String env, String host) {
apiKey(apiKey);
errorMessage(errorMessage);
env(env);
+ host(host);
}
public AirbrakeNoticeBuilder(final String apiKey, final Throwable throwable) {
- this(apiKey, new Backtrace(), throwable, "test");
+ this(apiKey, new Backtrace(), throwable, "test", null);
}
public AirbrakeNoticeBuilder(final String apiKey, final Throwable throwable, final String env) {
- this(apiKey, new Backtrace(), throwable, env);
+ this(apiKey, new Backtrace(), throwable, env, null);
}
public AirbrakeNoticeBuilder(final String apiKey, final Throwable throwable, final String projectRoot, final String env) {
- this(apiKey, new Backtrace(), throwable, env);
+ this(apiKey, new Backtrace(), throwable, env, null);
projectRoot(projectRoot);
}
@@ -110,6 +123,14 @@ private void env(final String env) {
environmentName = env;
}
+ private void host(String host) {
+ if (notDefined(host)) {
+ this.host = DEFAULT_HOST;
+ } else {
+ this.host = host;
+ }
+ }
+
/** A hash of the environment data that existed when the error occurred (required, but can be empty). */
protected void environment(final Map<String, Object> environment) {
this.environment.putAll(environment);
@@ -156,7 +177,7 @@ protected void filteredSystemProperties() {
public AirbrakeNotice newNotice() {
return new AirbrakeNotice(apiKey, projectRoot, environmentName, errorMessage, errorClass, backtrace, request, session, environment, environmentFilters,
- hasRequest, url, component);
+ hasRequest, url, component, host);
}
private boolean notDefined(final Object object) {
@@ -10,8 +10,8 @@
public class AirbrakeNoticeBuilderUsingFilteredSystemProperties extends AirbrakeNoticeBuilder {
- public AirbrakeNoticeBuilderUsingFilteredSystemProperties(final String apiKey, final Backtrace backtraceBuilder, final Throwable throwable, final String env) {
- super(apiKey, backtraceBuilder, throwable, env);
+ public AirbrakeNoticeBuilderUsingFilteredSystemProperties(final String apiKey, final Backtrace backtraceBuilder, final Throwable throwable, final String env, final String host) {
+ super(apiKey, backtraceBuilder, throwable, env, host);
environment(System.getProperties());
addMDCToSession();
standardEnvironmentFilters();
@@ -16,8 +16,8 @@ private void addingProperties(final HttpURLConnection connection) throws Protoco
connection.setRequestMethod("POST");
}
- private HttpURLConnection createConnection() throws IOException {
- return (HttpURLConnection) new URL("http://api.airbrake.io/notifier_api/v2/notices").openConnection();
+ private HttpURLConnection createConnection(String host) throws IOException {
+ return (HttpURLConnection) new URL("http://" + host + "/notifier_api/v2/notices").openConnection();
}
private void err(final AirbrakeNotice notice, final Exception e) {
@@ -26,7 +26,7 @@ private void err(final AirbrakeNotice notice, final Exception e) {
public int notify(final AirbrakeNotice notice) {
try {
- final HttpURLConnection toairbrake = createConnection();
+ final HttpURLConnection toairbrake = createConnection(notice.host());
addingProperties(toairbrake);
String toPost = new NoticeXml(notice).toString();
return send(toPost, toairbrake);
@@ -80,6 +80,19 @@ public void testNewairbrakeUsingBuilderNoticeWithBacktrace() {
assertThat(notice.apiKey(), is(API_KEY));
assertThat(notice.errorMessage(), is(ERROR_MESSAGE));
assertThat(notice.backtrace(), is(BACKTRACE));
+ assertThat(notice.host(), is(AirbrakeNoticeBuilder.DEFAULT_HOST));
+ }
+
+ @Test
+ public void testNewairbrakeUsingBuilderNoticeWithBacktraceWithHost_shouldUseTheHost() {
+ final AirbrakeNotice notice = new AirbrakeNoticeBuilder(API_KEY, ERROR_MESSAGE, "test", "your.proxy.to.airbrake.com") {
+ {
+ backtrace(BACKTRACE);
+ }
+ }.newNotice();
+
+ assertThat(notice, is(notNullValue()));
+ assertThat(notice.host(), is("your.proxy.to.airbrake.com"));
}
@Test
@@ -146,7 +146,7 @@ public void testSendExceptionToairbrakeUsingRubyBacktrace() {
@Test
public void testSendExceptionToairbrakeUsingRubyBacktraceAndFilteredSystemProperties() {
final Exception EXCEPTION = newException(ERROR_MESSAGE);
- final AirbrakeNotice notice = new AirbrakeNoticeBuilderUsingFilteredSystemProperties(API_KEY, new RubyBacktrace(), EXCEPTION, "test").newNotice();
+ final AirbrakeNotice notice = new AirbrakeNoticeBuilderUsingFilteredSystemProperties(API_KEY, new RubyBacktrace(), EXCEPTION, "test", null).newNotice();
assertThat(notifier.notify(notice), is(200));
}
@@ -41,7 +41,7 @@ public void testErrorBacktrace() {
@Test
public void testErrorBacktraceLine() {
- assertThat(xml(new NoticeXml(notice)), containsString("<line method=org.junit.runners.BlockJUnit4ClassRunner.runChild file=BlockJUnit4ClassRunner.java number=76/>"));
+ assertThat(xml(new NoticeXml(notice)), containsString("<line method=org.junit.runners.BlockJUnit4ClassRunner.runChild file=BlockJUnit4ClassRunner.java number="));
}
@Test

0 comments on commit 5c191a5

Please sign in to comment.