Skip to content

Commit a4819cd

Browse files
Merge pull request #147 from safarmer/appender-from-client
Allow a BugsnagAppender to be created from an existing client.
2 parents febc47b + 69e4372 commit a4819cd

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

bugsnag/src/main/java/com/bugsnag/BugsnagAppender.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,26 @@ public class BugsnagAppender extends UnsynchronizedAppenderBase<ILoggingEvent> {
8080
/** Bugsnag client. */
8181
private Bugsnag bugsnag = null;
8282

83+
/** Creates an appender from an existing Bugsnag client. */
84+
public BugsnagAppender(Bugsnag bugsnag) {
85+
super();
86+
this.bugsnag = bugsnag;
87+
}
88+
89+
/**
90+
* Default constructor used by Logback to create a Bugsnag appender that will create it's own
91+
* client when required.
92+
*/
93+
@SuppressWarnings("unused")
94+
public BugsnagAppender() {
95+
this(null);
96+
}
97+
8398
@Override
8499
public void start() {
85-
this.bugsnag = createBugsnag();
100+
if (bugsnag == null) {
101+
this.bugsnag = createBugsnag();
102+
}
86103
super.start();
87104
}
88105

bugsnag/src/test/java/com/bugsnag/AppenderTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,19 @@ public void testSplit() {
346346
assertArrayEquals(expected, appender.split("one,two,three").toArray());
347347
}
348348

349+
@Test
350+
public void testCreateFromExistingClient() {
351+
Bugsnag client = new Bugsnag("testApiKey");
352+
BugsnagAppender appender = new BugsnagAppender(client);
353+
// Make sure configuration changes are not passed through to the provided client.
354+
appender.setApiKey("newApiKey");
355+
// Make sure a new client is not created when starting the appender.
356+
appender.start();
357+
358+
assertEquals(client, appender.getClient());
359+
assertEquals("testApiKey", appender.getClient().getConfig().apiKey);
360+
}
361+
349362
private StackTraceElement changeClassName(StackTraceElement element, String className) {
350363
return new StackTraceElement(className,
351364
element.getFileName(),

0 commit comments

Comments
 (0)