Skip to content
Draft
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

### Fixes

- Prevent Logback, Log4j2, and JUL log arguments containing percent format sequences from being formatted a second time, which could corrupt messages or exhaust memory ([#6737](https://github.com/getsentry/sentry-java/pull/6737))
- Kotlin now sees `Attachment.getBytes()`, `Device.getArchs()`, `Spring.getActiveProfiles()` and `JsonSerializationUtils.bytesFrom(...)` as nullable, because `@Nullable` now annotates the returned array instead of its elements ([#6558](https://github.com/getsentry/sentry-java/pull/6558))
- Shake-to-report no longer leaks the first Activity that resumes after `Sentry.init` ([#6686](https://github.com/getsentry/sentry-java/pull/6686))
- Android app start: Reopening the app within a minute of process start is now reported as a warm start timed from the reopened screen, instead of reusing the cold start's timing. In standalone app start mode, this no longer sends the cold start a second time, as a warm start ([#6687](https://github.com/getsentry/sentry-java/pull/6687))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ protected void captureLog(@NotNull LogRecord loggingEvent) {

final @NotNull SentryLogParameters params = SentryLogParameters.create(attributes);
params.setOrigin("auto.log.jul");
params.setMessageAlreadyFormatted(true);

Sentry.logger().log(sentryLevel, params, formattedMessage, arguments);
}
Expand Down
18 changes: 18 additions & 0 deletions sentry-jul/src/test/kotlin/io/sentry/jul/SentryHandlerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,24 @@ class SentryHandlerTest {
)
}

@Test
fun `does not format an already formatted log message again`() {
fixture = Fixture(minimumLevel = Level.SEVERE)
fixture.logger.log(Level.SEVERE, "q={0}", arrayOf("PO%2010B"))

Sentry.flush(1000)

verify(fixture.transport)
.send(
checkLogs { logs ->
val log = logs.items.first()
assertEquals("q=PO%2010B", log.body)
assertEquals("q={0}", log.attributes?.get("sentry.message.template")?.value)
assertEquals("PO%2010B", log.attributes?.get("sentry.message.parameter.0")?.value)
},
)
}

@Test
fun `sets template on log when logging message with parameters and using printfStyle`() {
fixture = Fixture(minimumLevel = Level.SEVERE, printfStyle = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ protected void captureLog(@NotNull LogEvent loggingEvent) {

final @NotNull SentryLogParameters params = SentryLogParameters.create(attributes);
params.setOrigin("auto.log.log4j2");
params.setMessageAlreadyFormatted(true);

scopes.logger().log(sentryLevel, params, formattedMessage, arguments);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,24 @@ class SentryAppenderTest {
)
}

@Test
fun `does not format an already formatted log message again`() {
val logger = fixture.getSut(minimumLevel = Level.ERROR)
logger.error("q={}", "PO%2010B")

Sentry.flush(1000)

verify(fixture.transport)
.send(
checkLogs { logs ->
val log = logs.items.first()
assertEquals("q=PO%2010B", log.body)
assertEquals("q={}", log.attributes?.get("sentry.message.template")?.value)
assertEquals("PO%2010B", log.attributes?.get("sentry.message.parameter.0")?.value)
},
)
}

@Test
fun `sets template on log when logging message with parameters and number of parameters is wrong`() {
val logger = fixture.getSut(minimumLevel = Level.ERROR)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ protected void captureLog(@NotNull ILoggingEvent loggingEvent) {

final @NotNull SentryLogParameters params = SentryLogParameters.create(attributes);
params.setOrigin("auto.log.logback");
params.setMessageAlreadyFormatted(true);

Sentry.logger()
.log(sentryLevel, params, formattedMessage, arguments != null ? arguments : new Object[0]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,24 @@ class SentryAppenderTest {
)
}

@Test
fun `does not format an already formatted log message again`() {
fixture = Fixture(minimumLevel = Level.ERROR, enableLogs = true)
fixture.logger.error("q={}", "PO%2010B")

Sentry.flush(1000)

verify(fixture.transport)
.send(
checkLogs { logs ->
val log = logs.items.first()
assertEquals("q=PO%2010B", log.body)
assertEquals("q={}", log.attributes?.get("sentry.message.template")?.value)
assertEquals("PO%2010B", log.attributes?.get("sentry.message.parameter.0")?.value)
},
)
}

@Test
fun `sets template on log when logging message with parameters and number of parameters is wrong`() {
fixture = Fixture(minimumLevel = Level.ERROR, enableLogs = true)
Expand Down
2 changes: 2 additions & 0 deletions sentry/api/sentry.api
Original file line number Diff line number Diff line change
Expand Up @@ -5620,7 +5620,9 @@ public final class io/sentry/logger/SentryLogParameters {
public fun getAttributes ()Lio/sentry/SentryAttributes;
public fun getOrigin ()Ljava/lang/String;
public fun getTimestamp ()Lio/sentry/SentryDate;
public fun isMessageAlreadyFormatted ()Z
public fun setAttributes (Lio/sentry/SentryAttributes;)V
public fun setMessageAlreadyFormatted (Z)V
public fun setOrigin (Ljava/lang/String;)V
public fun setTimestamp (Lio/sentry/SentryDate;)V
}
Expand Down
3 changes: 2 additions & 1 deletion sentry/src/main/java/io/sentry/logger/LoggerApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ private void captureLog(
final @Nullable SentryDate timestamp = params.getTimestamp();
final @NotNull SentryDate timestampToUse =
timestamp == null ? options.getDateProvider().now() : timestamp;
final @NotNull String messageToUse = maybeFormatMessage(message, args);
final @NotNull String messageToUse =
params.isMessageAlreadyFormatted() ? message : maybeFormatMessage(message, args);

final @NotNull IScope combinedScope = scopes.getCombinedScopeView();
final @NotNull PropagationContext propagationContext = combinedScope.getPropagationContext();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public final class SentryLogParameters {
private @Nullable SentryDate timestamp;
private @Nullable SentryAttributes attributes;
private @NotNull String origin = "manual";
private boolean messageAlreadyFormatted = false;

public @Nullable SentryDate getTimestamp() {
return timestamp;
Expand All @@ -35,6 +36,14 @@ public void setOrigin(final @NotNull String origin) {
this.origin = origin;
}

public boolean isMessageAlreadyFormatted() {
return messageAlreadyFormatted;
}

public void setMessageAlreadyFormatted(final boolean messageAlreadyFormatted) {
this.messageAlreadyFormatted = messageAlreadyFormatted;
}

public static @NotNull SentryLogParameters create(
final @Nullable SentryDate timestamp, final @Nullable SentryAttributes attributes) {
final @NotNull SentryLogParameters params = new SentryLogParameters();
Expand Down
22 changes: 22 additions & 0 deletions sentry/src/test/java/io/sentry/ScopesTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2877,6 +2877,28 @@ class ScopesTest {
)
}

@Test
fun `already formatted log is not formatted again`() {
val (sut, mockClient) = getEnabledScopes { it.logs.isEnabled = true }
val params =
SentryLogParameters.create(
SentryAttributes.of(SentryAttribute.stringAttribute("sentry.message.template", "q={}")),
)
params.setMessageAlreadyFormatted(true)

sut.logger().log(SentryLogLevel.INFO, params, "q=PO%2010B", "PO%2010B")

verify(mockClient)
.captureLog(
check {
assertEquals("q=PO%2010B", it.body)
assertEquals("q={}", it.attributes?.get("sentry.message.template")?.value)
assertEquals("PO%2010B", it.attributes?.get("sentry.message.parameter.0")?.value)
},
anyOrNull(),
)
}

@Test
fun `log environment attribute uses the scope environment`() {
val (sut, mockClient) =
Expand Down
Loading