-
Notifications
You must be signed in to change notification settings - Fork 80
Configurable exception pattern for log4j2 #177
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
fa69d2a
Configurable exception pattern for log4j2
HaloFour 11cad95
Add unit test to confirm exception pattern with specific depth
HaloFour d24dc09
Test stack frame package filters in log4j2 throwable converter
HaloFour 1d9c586
Add unit test for rendering stack trace as array
HaloFour c0a4a71
Split exception pattern test out
HaloFour 69956b6
Fix tests based on PR comments
HaloFour c4d1714
PR comments
HaloFour 2b7ebca
Removing extra semicolon
eyalkoren 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
45 changes: 45 additions & 0 deletions
45
...2-ecs-layout/src/test/java/co/elastic/logging/log4j2/CustomExceptionPatternConverter.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,45 @@ | ||
package co.elastic.logging.log4j2; | ||
|
||
import org.apache.logging.log4j.core.LogEvent; | ||
import org.apache.logging.log4j.core.config.plugins.Plugin; | ||
import org.apache.logging.log4j.core.pattern.ConverterKeys; | ||
import org.apache.logging.log4j.core.pattern.LogEventPatternConverter; | ||
import org.apache.logging.log4j.core.pattern.PatternConverter; | ||
|
||
@Plugin(category = PatternConverter.CATEGORY, name = "CustomExceptionPatternConverter") | ||
@ConverterKeys({"cEx"}) | ||
public class CustomExceptionPatternConverter extends LogEventPatternConverter { | ||
|
||
public CustomExceptionPatternConverter(final String[] options) { | ||
super("Custom", "custom"); | ||
} | ||
|
||
public static CustomExceptionPatternConverter newInstance(final String[] options) { | ||
return new CustomExceptionPatternConverter(options); | ||
} | ||
|
||
|
||
@Override | ||
public void format(LogEvent event, StringBuilder toAppendTo) { | ||
Throwable thrown = event.getThrown(); | ||
if (thrown != null) { | ||
String message = thrown.getMessage(); | ||
if (message == null || message.isEmpty()) { | ||
toAppendTo.append(thrown.getClass().getName()) | ||
.append('\n'); | ||
} else { | ||
toAppendTo.append(thrown.getClass().getName()) | ||
.append(": ") | ||
.append(message) | ||
.append('\n'); | ||
} | ||
|
||
toAppendTo.append("STACK_TRACE!"); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean handlesThrowable() { | ||
return true; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...ecs-layout/src/test/java/co/elastic/logging/log4j2/EcsLayoutWithExceptionPatternTest.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,34 @@ | ||||||||
package co.elastic.logging.log4j2; | ||||||||
|
||||||||
import com.fasterxml.jackson.databind.JsonNode; | ||||||||
import org.apache.logging.log4j.core.LoggerContext; | ||||||||
import org.junit.jupiter.api.Test; | ||||||||
|
||||||||
import static org.assertj.core.api.Assertions.assertThat; | ||||||||
|
||||||||
public class EcsLayoutWithExceptionPatternTest extends Log4j2EcsLayoutTest { | ||||||||
@Override | ||||||||
protected EcsLayout.Builder configureLayout(LoggerContext context) { | ||||||||
return super.configureLayout(context) | ||||||||
.setExceptionPattern("%cEx"); | ||||||||
} | ||||||||
|
||||||||
@Test | ||||||||
void testLogException() throws Exception { | ||||||||
error("test", new RuntimeException("test")); | ||||||||
JsonNode log = getLastLogLine(); | ||||||||
assertThat(log.get("log.level").textValue()).isIn("ERROR", "SEVERE"); | ||||||||
assertThat(log.get("error.message").textValue()).isEqualTo("test"); | ||||||||
assertThat(log.get("error.type").textValue()).isEqualTo(RuntimeException.class.getName()); | ||||||||
assertThat(log.get("error.stack_trace").textValue()).isEqualTo("java.lang.RuntimeException: test\nSTACK_TRACE!"); | ||||||||
} | ||||||||
|
||||||||
@Test | ||||||||
void testLogExceptionNullMessage() throws Exception { | ||||||||
error("test", new RuntimeException()); | ||||||||
JsonNode log = getLastLogLine();; | ||||||||
assertThat(log.get("error.type").textValue()).isEqualTo(RuntimeException.class.getName()); | ||||||||
assertThat(log.get("error.message")).isNull(); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
(didn't actually test that, but based on code - we need to test this |
||||||||
assertThat(log.get("error.stack_trace").textValue()).isEqualTo("java.lang.RuntimeException\nSTACK_TRACE!"); | ||||||||
} | ||||||||
} |
57 changes: 57 additions & 0 deletions
57
...cs-layout/src/test/java/co/elastic/logging/log4j2/EcsLayoutWithStackTraceAsArrayTest.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,57 @@ | ||
package co.elastic.logging.log4j2; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.node.ArrayNode; | ||
import org.apache.logging.log4j.core.LoggerContext; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class EcsLayoutWithStackTraceAsArrayTest extends Log4j2EcsLayoutTest { | ||
|
||
@Override | ||
protected EcsLayout.Builder configureLayout(LoggerContext context) { | ||
return super.configureLayout(context) | ||
.setExceptionPattern("%rEx{4,filters(java.base,java.lang)}") | ||
.setStackTraceAsArray(true); | ||
} | ||
|
||
@Test | ||
void testLogException() throws Exception { | ||
error("test", numberFormatException()); | ||
JsonNode log = getLastLogLine(); | ||
assertThat(log.get("log.level").textValue()).isIn("ERROR", "SEVERE"); | ||
assertThat(log.get("error.message").textValue()).isEqualTo("For input string: \"NOT_AN_INT\""); | ||
assertThat(log.get("error.type").textValue()).isEqualTo(NumberFormatException.class.getName()); | ||
assertThat(log.get("error.stack_trace").isArray()).isTrue(); | ||
ArrayNode arrayNode = (ArrayNode) log.get("error.stack_trace"); | ||
assertThat(arrayNode.size()).isEqualTo(4); | ||
assertThat(arrayNode.get(0).textValue()).isEqualTo("java.lang.NumberFormatException: For input string: \"NOT_AN_INT\""); | ||
assertThat(arrayNode.get(1).textValue()).isEqualTo("\t... suppressed 3 lines"); | ||
eyalkoren marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assertThat(arrayNode.get(2).textValue()).startsWith("\tat co.elastic.logging.log4j2.EcsLayoutWithStackTraceAsArrayTest.numberFormatException"); | ||
assertThat(arrayNode.get(3).textValue()).startsWith("\tat co.elastic.logging.log4j2.EcsLayoutWithStackTraceAsArrayTest.testLogException"); | ||
} | ||
|
||
private static Throwable numberFormatException() { | ||
try { | ||
Integer.parseInt("NOT_AN_INT"); | ||
return null; | ||
} catch (Exception ex) { | ||
return ex; | ||
} | ||
} | ||
|
||
@Test | ||
void testLogExceptionNullMessage() throws Exception { | ||
error("test", new RuntimeException()); | ||
// skip validation that error.stack_trace is a string | ||
JsonNode log = getLastLogLine(); | ||
assertThat(log.get("error.type").textValue()).isEqualTo(RuntimeException.class.getName()); | ||
assertThat(log.get("error.message")).isNull(); | ||
assertThat(log.get("error.stack_trace").isArray()).isTrue(); | ||
ArrayNode arrayNode = (ArrayNode) log.get("error.stack_trace"); | ||
assertThat(arrayNode.size()).isEqualTo(4); | ||
assertThat(arrayNode.get(0).textValue()).isEqualTo("java.lang.RuntimeException"); | ||
assertThat(arrayNode.get(1).textValue()).startsWith("\tat co.elastic.logging.log4j2.EcsLayoutWithStackTraceAsArrayTest.testLogExceptionNullMessage"); | ||
} | ||
} |
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
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
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
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.