Skip to content
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

Capture logback formatted message #5497

Merged
merged 1 commit into from
Mar 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,23 @@ class JavaUtilLoggingTest extends AgentInstrumentationSpecification {
private static final Logger logger = Logger.getLogger("abc")

@Unroll
def "test method=#testMethod with exception=#exception and parent=#parent"() {
def "test method=#testMethod with testArgs=#testArgs and parent=#parent"() {
when:
if (parent) {
runWithSpan("parent") {
if (exception) {
if (testArgs == "exception") {
logger.log(Level."${testMethod.toUpperCase()}", "xyz", new IllegalStateException("hello"))
} else if (testArgs == "params") {
logger.log(Level."${testMethod.toUpperCase()}", "xyz: {0}", 123)
} else {
logger."$testMethod"("xyz")
}
}
} else {
if (exception) {
if (testArgs == "exception") {
logger.log(Level."${testMethod.toUpperCase()}", "xyz", new IllegalStateException("hello"))
} else if (testArgs == "params") {
logger.log(Level."${testMethod.toUpperCase()}", "xyz: {0}", 123)
} else {
logger."$testMethod"("xyz")
}
Expand All @@ -49,11 +53,15 @@ class JavaUtilLoggingTest extends AgentInstrumentationSpecification {
assertThat(logs).hasSize(1)
})
def log = logs.get(0)
assertThat(log.getBody().asString()).isEqualTo("xyz")
if (testArgs == "params") {
assertThat(log.getBody().asString()).isEqualTo("xyz: 123")
} else {
assertThat(log.getBody().asString()).isEqualTo("xyz")
}
assertThat(log.getInstrumentationLibraryInfo().getName()).isEqualTo("abc")
assertThat(log.getSeverity()).isEqualTo(severity)
assertThat(log.getSeverityText()).isEqualTo(severityText)
if (exception) {
if (testArgs == "exception") {
assertThat(log.getAttributes().size()).isEqualTo(5)
assertThat(log.getAttributes().get(SemanticAttributes.EXCEPTION_TYPE)).isEqualTo(IllegalStateException.getName())
assertThat(log.getAttributes().get(SemanticAttributes.EXCEPTION_MESSAGE)).isEqualTo("hello")
Expand All @@ -77,14 +85,14 @@ class JavaUtilLoggingTest extends AgentInstrumentationSpecification {
}

where:
[args, exception, parent] << [
[args, testArgs, parent] << [
[
["fine", null, null],
["info", Severity.INFO, "INFO"],
["warning", Severity.WARN, "WARNING"],
["severe", Severity.ERROR, "SEVERE"]
],
[true, false],
["none", "exception", "param"],
[true, false]
].combinations()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ class Log4j2Test extends AgentInstrumentationSpecification {
if (parent) {
runWithSpan("parent") {
if (exception) {
logger."$testMethod"("xyz", new IllegalStateException("hello"))
logger."$testMethod"("xyz: {}", 123, new IllegalStateException("hello"))
} else {
logger."$testMethod"("xyz")
logger."$testMethod"("xyz: {}", 123)
}
}
} else {
if (exception) {
logger."$testMethod"("xyz", new IllegalStateException("hello"))
logger."$testMethod"("xyz: {}", 123, new IllegalStateException("hello"))
} else {
logger."$testMethod"("xyz")
logger."$testMethod"("xyz: {}", 123)
}
}

Expand All @@ -52,7 +52,7 @@ class Log4j2Test extends AgentInstrumentationSpecification {
assertThat(logs).hasSize(1)
})
def log = logs.get(0)
assertThat(log.getBody().asString()).isEqualTo("xyz")
assertThat(log.getBody().asString()).isEqualTo("xyz: 123")
assertThat(log.getInstrumentationLibraryInfo().getName()).isEqualTo("abc")
assertThat(log.getSeverity()).isEqualTo(severity)
assertThat(log.getSeverityText()).isEqualTo(severityText)
Expand Down Expand Up @@ -101,7 +101,7 @@ class Log4j2Test extends AgentInstrumentationSpecification {
ThreadContext.put("key1", "val1")
ThreadContext.put("key2", "val2")
try {
logger.info("xyz")
logger.info("xyz: {}", 123)
} finally {
ThreadContext.clearMap()
}
Expand All @@ -114,7 +114,7 @@ class Log4j2Test extends AgentInstrumentationSpecification {
assertThat(logs).hasSize(1)
})
def log = logs.get(0)
assertThat(log.getBody().asString()).isEqualTo("xyz")
assertThat(log.getBody().asString()).isEqualTo("xyz: 123")
assertThat(log.getInstrumentationLibraryInfo().getName()).isEqualTo("abc")
assertThat(log.getSeverity()).isEqualTo(Severity.INFO)
assertThat(log.getSeverityText()).isEqualTo("INFO")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ class LogbackTest extends AgentInstrumentationSpecification {
if (parent) {
runWithSpan("parent") {
if (exception) {
logger."$testMethod"("xyz", new IllegalStateException("hello"))
logger."$testMethod"("xyz: {}", 123, new IllegalStateException("hello"))
} else {
logger."$testMethod"("xyz")
logger."$testMethod"("xyz: {}", 123)
}
}
} else {
if (exception) {
logger."$testMethod"("xyz", new IllegalStateException("hello"))
logger."$testMethod"("xyz: {}", 123, new IllegalStateException("hello"))
} else {
logger."$testMethod"("xyz")
logger."$testMethod"("xyz: {}", 123)
}
}

Expand All @@ -51,7 +51,7 @@ class LogbackTest extends AgentInstrumentationSpecification {
assertThat(logs).hasSize(1)
})
def log = logs.get(0)
assertThat(log.getBody().asString()).isEqualTo("xyz")
assertThat(log.getBody().asString()).isEqualTo("xyz: 123")
assertThat(log.getInstrumentationLibraryInfo().getName()).isEqualTo(loggerName)
assertThat(log.getSeverity()).isEqualTo(severity)
assertThat(log.getSeverityText()).isEqualTo(severityText)
Expand Down Expand Up @@ -106,7 +106,7 @@ class LogbackTest extends AgentInstrumentationSpecification {
MDC.put("key1", "val1")
MDC.put("key2", "val2")
try {
abcLogger.info("xyz")
abcLogger.info("xyz: {}", 123)
} finally {
MDC.clear()
}
Expand All @@ -119,7 +119,7 @@ class LogbackTest extends AgentInstrumentationSpecification {
assertThat(logs).hasSize(1)
})
def log = logs.get(0)
assertThat(log.getBody().asString()).isEqualTo("xyz")
assertThat(log.getBody().asString()).isEqualTo("xyz: 123")
assertThat(log.getInstrumentationLibraryInfo().getName()).isEqualTo("abc")
assertThat(log.getSeverity()).isEqualTo(Severity.INFO)
assertThat(log.getSeverityText()).isEqualTo("INFO")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public void emit(LogEmitterProvider logEmitterProvider, ILoggingEvent event) {
*/
private void mapLoggingEvent(LogBuilder builder, ILoggingEvent loggingEvent) {
// message
String message = loggingEvent.getMessage();
String message = loggingEvent.getFormattedMessage();
if (message != null) {
builder.setBody(message);
}
Expand Down