-
Notifications
You must be signed in to change notification settings - Fork 76
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
Configurable exception pattern for log4j2 #177
Conversation
Decided to try to pick this back up. We're already running into issues with our Elastic storage given the size of our stack traces (Spring WebFlux application, a stack can easily exceed 50k). Instead of trying to build something completely new, as I did before, I decided to piggyback off of what is offered by log4j2. |
Thanks for checking out this PR! Unfortunately I don't believe #167 meets my needs. I'm looking for more control over how the exception stack trace is rendered beyond filtering out unwanted frames by package. For example, my preference is to render exceptions root-first, and to limit the number of total frames rendered, which is possible with the |
OK, makes sense, thanks! |
Yep, I just closed it, sorry for letting it hang out there for so long. 😅 |
To answer your question about stack-as-array, it should be supported by this PR. I use the Maybe it's worth looking to change those methods in |
Took a stab at it: #178 Can be considered entirely independently from this PR. Also rewrote |
@HaloFour looks good, let's go for it! |
Sorry, got distracted. I started down this path but I recall having issues where the schema to which the JSON is compared expects the |
Once we have a test we can see the error and either fix the validation if necessary, or realize what we are doing wrong. Without seeing the error, I can't say anything about that, so start by adding the test that does something and expects a specific behavior. If it fails and this is indeed the expected behavior, we will either fix the validation or the implementation later.
Changing the base test is a good idea if all frameworks can benefit from it. For example, we now have
which each concrete implementation will apply to its own formatter. Note that all concrete tests that subclass this abstract test use If this is specific to a single framework, you can either extend the abstract test (as I just did for JUL for example), or add a separate test. I hope this helps. |
Instead of See also Line 70 in 23d5bd6
|
Thanks for the pointers, let me know if this helps. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very nice, thanks!
Some minor comments.
The custom converter test is awesome! However, the other tests should preferably rely on the builtin converter.
I think we could use the stack as array test in the entire suite, but I can do it later (through this PR or other). If you tried and found reasons not to, please let me know.
...j2-ecs-layout/src/test/java/co/elastic/logging/log4j2/EcsLayoutWithExceptionPatternTest.java
Outdated
Show resolved
Hide resolved
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 comment
The reason will be displayed to describe this comment to others. Learn more.
assertThat(log.get("error.message")).isNull(); | |
assertThat(log.get("error.message")).isNull(); | |
assertThat(log.get("error.stack_trace").textValue()).isEqualTo("java.lang.RuntimeException\nSTACK_TRACE!"); |
(didn't actually test that, but based on code - we need to test this else
branch as well)
@Override | ||
protected EcsLayout.Builder configureLayout(LoggerContext context) { | ||
return super.configureLayout(context) | ||
.setExceptionPattern("%cEx") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why use the custom one and not rely these tests on the default pattern converter that is already configured through the superclass? Unless there's a good reason, I prefer to here what most users will use by default.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can do that, I was using the custom converter to keep the stack traces predictable as I don't really want to be testing whatever log4j2 does under the covers (beyond confirming that it works anyway). I'll split the custom converter to its own test and use the built-in ones for these tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's already tested through EcsLayoutWithExceptionPatternTest
, so what do you mean split further?
I understand the need for consistency without relying on log4j internals, but maybe you can use the builtin converter to achieve exactly that - filter out stack trace elements that are not yours and limit to only 3. Can you do that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's already tested through
EcsLayoutWithExceptionPatternTest
, so what do you mean split further?
You're right, I did, too much multitasking this week. 😅
I understand the need for consistency without relying on log4j internals, but maybe you can use the builtin converter to achieve exactly that - filter out stack trace elements that are not yours and limit to only 3. Can you do that?
Definitely, I'll try to get a commit up shortly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, thanks!
Please also apply my suggestions, unless there is a reason not to.
Allows configuring a throwable pattern to be used when serializing exceptions associated with the current log event. For example, given the pattern
%ex{4}
the stack trace of the exception will be limited to the first 4 lines. Should support any pattern supported by log4j2 including filters to suppress stack frames from specific packages.