Skip to content

Commit 15f967d

Browse files
Capture exceptions that are logged using the fatal log level. (elastic#2377)
1 parent b16511a commit 15f967d

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

CHANGELOG.asciidoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ endif::[]
3434
[[release-notes-1.28.4]]
3535
==== 1.28.4 - 2021/12/30
3636
37+
[float]
38+
===== Features
39+
* Exceptions that are logged using the fatal log level are now captured (log4j2 only) - {pull}2377[#2377]
40+
3741
[float]
3842
===== Bug fixes
3943
* Fix `@Traced` annotation to return proper outcome instead of `failed` - {pull}2370[#2370]

apm-agent-plugins/apm-error-logging-plugin/src/main/java/co/elastic/apm/agent/errorlogging/Log4j2LoggerErrorCapturingInstrumentation.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919
package co.elastic.apm.agent.errorlogging;
2020

21+
import net.bytebuddy.description.method.MethodDescription;
2122
import net.bytebuddy.description.type.TypeDescription;
2223
import net.bytebuddy.matcher.ElementMatcher;
2324

@@ -27,6 +28,7 @@
2728
import static net.bytebuddy.matcher.ElementMatchers.hasSuperType;
2829
import static net.bytebuddy.matcher.ElementMatchers.named;
2930
import static net.bytebuddy.matcher.ElementMatchers.not;
31+
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
3032

3133
public class Log4j2LoggerErrorCapturingInstrumentation extends AbstractLoggerErrorCapturingInstrumentation {
3234

@@ -37,6 +39,11 @@ public ElementMatcher<? super TypeDescription> getTypeMatcher() {
3739
return hasSuperType(named(LOG4J2_LOGGER)).and(not(hasSuperType(named(SLF4J_LOGGER))));
3840
}
3941

42+
@Override
43+
public ElementMatcher<? super MethodDescription> getMethodMatcher() {
44+
return named("fatal").and(takesArgument(1, named("java.lang.Throwable"))).or(super.getMethodMatcher());
45+
}
46+
4047
@Override
4148
public Collection<String> getInstrumentationGroupNames() {
4249
Collection<String> ret = super.getInstrumentationGroupNames();

apm-agent-plugins/apm-error-logging-plugin/src/test/java/co/elastic/apm/agent/errorlogging/Log4j2LoggerErrorCapturingInstrumentationTest.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import org.apache.logging.log4j.LogManager;
2222
import org.apache.logging.log4j.Logger;
23+
import org.apache.logging.log4j.MarkerManager;
2324
import org.apache.logging.log4j.message.ParameterizedMessageFactory;
2425
import org.junit.jupiter.api.Test;
2526

@@ -28,15 +29,20 @@ class Log4j2LoggerErrorCapturingInstrumentationTest extends AbstractErrorLogging
2829
private static final Logger logger = LogManager.getLogger(Log4j2LoggerErrorCapturingInstrumentationTest.class);
2930

3031
@Test
31-
void captureExceptionWithStringMessage() {
32+
void captureErrorExceptionWithStringMessage() {
3233
logger.error("exception captured", new RuntimeException("some business exception"));
3334
verifyThatExceptionCaptured(1, "some business exception", RuntimeException.class);
3435
}
3536

36-
3737
@Test
38-
void captureExceptionWithMessageMessage() {
38+
void captureErrorExceptionWithMessageMessage() {
3939
logger.error(ParameterizedMessageFactory.INSTANCE.newMessage("exception captured with parameter {}", "foo"), new RuntimeException("some business exception"));
4040
verifyThatExceptionCaptured(1, "some business exception", RuntimeException.class);
4141
}
42+
43+
@Test
44+
void captureFatalException() {
45+
logger.fatal("exception captured", new RuntimeException("some business exception"));
46+
verifyThatExceptionCaptured(1, "some business exception", RuntimeException.class);
47+
}
4248
}

0 commit comments

Comments
 (0)