Skip to content

Commit

Permalink
Capture logback formatted message (open-telemetry#5497)
Browse files Browse the repository at this point in the history
  • Loading branch information
trask authored Mar 4, 2022
1 parent c342e1a commit b0e4be0
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 22 deletions.
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

0 comments on commit b0e4be0

Please sign in to comment.