-
Notifications
You must be signed in to change notification settings - Fork 417
Add new JsonProviders for exception messages #743
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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); | ||
| } | ||
| } |
58 changes: 58 additions & 0 deletions
58
src/main/java/net/logstash/logback/composite/loggingevent/ThrowableSelectors.java
This file contains hidden or 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,58 @@ | ||
| /* | ||
| * 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}. | ||
| * | ||
| * @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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.