-
Notifications
You must be signed in to change notification settings - Fork 408
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge PR #743 Add new JsonProviders for exception messages
* New providers `throwableMessage` and `throwableRootCauseMessage` * New cyclic reference safe algorithm to determine a throwable's root cause * Share that algorithm between `throwableRootCauseClassName` and `throwableRootCauseMessage`
- Loading branch information
Showing
10 changed files
with
404 additions
and
10 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
54 changes: 54 additions & 0 deletions
54
...ava/net/logstash/logback/composite/loggingevent/AbstractThrowableMessageJsonProvider.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,54 @@ | ||
/* | ||
* Copyright 2013-2021 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package net.logstash.logback.composite.loggingevent; | ||
|
||
import java.io.IOException; | ||
|
||
import net.logstash.logback.composite.AbstractFieldJsonProvider; | ||
import net.logstash.logback.composite.JsonWritingUtils; | ||
|
||
import ch.qos.logback.classic.spi.ILoggingEvent; | ||
import ch.qos.logback.classic.spi.IThrowableProxy; | ||
import com.fasterxml.jackson.core.JsonGenerator; | ||
|
||
/** | ||
* Logs an exception message for a given logging event. Which exception to be | ||
* logged depends on the subclass's implementation of | ||
* {@link #getThrowable(ILoggingEvent)}. | ||
*/ | ||
public abstract class AbstractThrowableMessageJsonProvider extends AbstractFieldJsonProvider<ILoggingEvent> { | ||
|
||
protected AbstractThrowableMessageJsonProvider(String fieldName) { | ||
setFieldName(fieldName); | ||
} | ||
|
||
@Override | ||
public void writeTo(JsonGenerator generator, ILoggingEvent event) throws IOException { | ||
IThrowableProxy throwable = getThrowable(event); | ||
if (throwable != null) { | ||
String throwableMessage = throwable.getMessage(); | ||
JsonWritingUtils.writeStringField(generator, getFieldName(), throwableMessage); | ||
} | ||
} | ||
|
||
/** | ||
* @param event the event being logged, never {@code null} | ||
* @return the throwable to use, or {@code null} if no appropriate throwable is | ||
* available | ||
* @throws NullPointerException if {@code event} is {@code null} | ||
*/ | ||
protected abstract IThrowableProxy getThrowable(ILoggingEvent event); | ||
} |
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
35 changes: 35 additions & 0 deletions
35
src/main/java/net/logstash/logback/composite/loggingevent/ThrowableMessageJsonProvider.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,35 @@ | ||
/* | ||
* Copyright 2013-2021 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package net.logstash.logback.composite.loggingevent; | ||
|
||
import ch.qos.logback.classic.spi.ILoggingEvent; | ||
import ch.qos.logback.classic.spi.IThrowableProxy; | ||
|
||
/** | ||
* Logs the message of the throwable associated with a given logging event, if | ||
* any. | ||
*/ | ||
public class ThrowableMessageJsonProvider extends AbstractThrowableMessageJsonProvider { | ||
|
||
public ThrowableMessageJsonProvider() { | ||
super("throwable_message"); | ||
} | ||
|
||
@Override | ||
protected IThrowableProxy getThrowable(ILoggingEvent event) { | ||
return event.getThrowableProxy(); | ||
} | ||
} |
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
37 changes: 37 additions & 0 deletions
37
...va/net/logstash/logback/composite/loggingevent/ThrowableRootCauseMessageJsonProvider.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,37 @@ | ||
/* | ||
* Copyright 2013-2021 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package net.logstash.logback.composite.loggingevent; | ||
|
||
import ch.qos.logback.classic.spi.ILoggingEvent; | ||
import ch.qos.logback.classic.spi.IThrowableProxy; | ||
|
||
/** | ||
* Logs the message of the innermost cause of the throwable associated with a | ||
* given logging event, if any. The root cause may be the throwable itself, if | ||
* it has no cause. | ||
*/ | ||
public class ThrowableRootCauseMessageJsonProvider extends AbstractThrowableMessageJsonProvider { | ||
|
||
public ThrowableRootCauseMessageJsonProvider() { | ||
super("throwable_root_cause_message"); | ||
} | ||
|
||
@Override | ||
protected IThrowableProxy getThrowable(ILoggingEvent event) { | ||
IThrowableProxy t = event.getThrowableProxy(); | ||
return t == null ? null : ThrowableSelectors.rootCause(t); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
src/main/java/net/logstash/logback/composite/loggingevent/ThrowableSelectors.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,59 @@ | ||
/* | ||
* Copyright 2013-2021 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package net.logstash.logback.composite.loggingevent; | ||
|
||
import ch.qos.logback.classic.spi.IThrowableProxy; | ||
|
||
/** | ||
* Utilities to obtain {@code Throwables} from {@code IThrowableProxies}. | ||
*/ | ||
public class ThrowableSelectors { | ||
|
||
/** | ||
* Returns the innermost cause of {@code throwable}. | ||
* | ||
* @param throwable the throwable for which to find the root cause | ||
* @return the innermost cause, which may be {@code throwable} itself if there | ||
* is no cause, or {@code null} if there is a loop in the causal chain. | ||
* | ||
* @throws NullPointerException if {@code throwable} is {@code null} | ||
*/ | ||
public static IThrowableProxy rootCause(IThrowableProxy throwable) { | ||
// Keep a second pointer that slowly walks the causal chain. | ||
// If the fast pointer ever catches the slower pointer, then there's a loop. | ||
IThrowableProxy slowPointer = throwable; | ||
boolean advanceSlowPointer = false; | ||
|
||
IThrowableProxy cause; | ||
while ((cause = throwable.getCause()) != null) { | ||
throwable = cause; | ||
|
||
if (throwable == slowPointer) { | ||
// There's a cyclic reference, so no real root cause. | ||
return null; | ||
} | ||
|
||
if (advanceSlowPointer) { | ||
slowPointer = slowPointer.getCause(); | ||
} | ||
|
||
advanceSlowPointer = !advanceSlowPointer; // only advance every other iteration | ||
} | ||
|
||
return throwable; | ||
} | ||
|
||
} |
65 changes: 65 additions & 0 deletions
65
...st/java/net/logstash/logback/composite/loggingevent/ThrowableMessageJsonProviderTest.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,65 @@ | ||
/* | ||
* Copyright 2013-2021 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package net.logstash.logback.composite.loggingevent; | ||
|
||
import static org.mockito.Mockito.atLeastOnce; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.verifyNoInteractions; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.io.IOException; | ||
|
||
import ch.qos.logback.classic.spi.ILoggingEvent; | ||
import ch.qos.logback.classic.spi.ThrowableProxy; | ||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
public class ThrowableMessageJsonProviderTest { | ||
|
||
private ThrowableMessageJsonProvider provider = new ThrowableMessageJsonProvider(); | ||
|
||
@Mock | ||
private JsonGenerator generator; | ||
|
||
@Mock | ||
private ILoggingEvent event; | ||
|
||
@Test | ||
public void testFieldName() throws IOException { | ||
provider.setFieldName("newFieldName"); | ||
|
||
IOException throwable = new IOException("kaput"); | ||
when(event.getThrowableProxy()).thenReturn(new ThrowableProxy(throwable)); | ||
|
||
provider.writeTo(generator, event); | ||
|
||
verify(generator).writeStringField("newFieldName", "kaput"); | ||
} | ||
|
||
@Test | ||
public void testNoThrowable() throws IOException { | ||
when(event.getThrowableProxy()).thenReturn(null); | ||
|
||
provider.writeTo(generator, event); | ||
|
||
verify(event, atLeastOnce()).getThrowableProxy(); | ||
verifyNoInteractions(generator); | ||
} | ||
} |
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
Oops, something went wrong.