Skip to content

Send logcat through Sentry Logs #4487

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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

### Fixes

- Send logcat through Sentry Logs ([#4487](https://github.com/getsentry/sentry-java/pull/4487))
- Enable the Logs feature in your `SentryOptions` or with the `io.sentry.logs.enabled` manifest option and the SDK will automatically send logcat logs to Sentry, if the Sentry Android Gradle plugin is applied.
- To set the logcat level check the [Logcat integration documentation](https://docs.sentry.io/platforms/android/integrations/logcat/#configure).
- No longer send out empty log envelopes ([#4497](https://github.com/getsentry/sentry-java/pull/4497))

### Dependencies
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

import android.util.Log;
import io.sentry.Breadcrumb;
import io.sentry.ScopesAdapter;
import io.sentry.Sentry;
import io.sentry.SentryLevel;
import io.sentry.SentryLogLevel;
import java.io.PrintWriter;
import java.io.StringWriter;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -44,73 +48,108 @@ private static void addAsBreadcrumb(
Sentry.addBreadcrumb(breadcrumb);
}

private static void addAsLog(
@NotNull final SentryLogLevel level,
@Nullable final String msg,
@Nullable final Throwable tr) {
final @NotNull ScopesAdapter scopes = ScopesAdapter.getInstance();
// Check if logs are enabled before doing expensive operations
if (!scopes.getOptions().getLogs().isEnabled()) {
return;
}
if (tr == null) {
scopes.logger().log(level, msg);
} else {
StringWriter sw = new StringWriter(256);
PrintWriter pw = new PrintWriter(sw, false);
tr.printStackTrace(pw);
pw.flush();
scopes.logger().log(level, msg != null ? (msg + "\n" + sw.toString()) : sw.toString());
pw.close();
}
}

public static int v(@Nullable String tag, @Nullable String msg) {
addAsBreadcrumb(tag, SentryLevel.DEBUG, msg);
addAsLog(SentryLogLevel.TRACE, msg, null);
return Log.v(tag, msg);
}

public static int v(@Nullable String tag, @Nullable String msg, @Nullable Throwable tr) {
addAsBreadcrumb(tag, SentryLevel.DEBUG, msg, tr);
addAsLog(SentryLogLevel.TRACE, msg, tr);
return Log.v(tag, msg, tr);
}

public static int d(@Nullable String tag, @Nullable String msg) {
addAsBreadcrumb(tag, SentryLevel.DEBUG, msg);
addAsLog(SentryLogLevel.DEBUG, msg, null);
return Log.d(tag, msg);
}

public static int d(@Nullable String tag, @Nullable String msg, @Nullable Throwable tr) {
addAsBreadcrumb(tag, SentryLevel.DEBUG, msg, tr);
addAsLog(SentryLogLevel.DEBUG, msg, tr);
return Log.d(tag, msg, tr);
}

public static int i(@Nullable String tag, @Nullable String msg) {
addAsBreadcrumb(tag, SentryLevel.INFO, msg);
addAsLog(SentryLogLevel.INFO, msg, null);
return Log.i(tag, msg);
}

public static int i(@Nullable String tag, @Nullable String msg, @Nullable Throwable tr) {
addAsBreadcrumb(tag, SentryLevel.INFO, msg, tr);
addAsLog(SentryLogLevel.INFO, msg, tr);
return Log.i(tag, msg, tr);
}

public static int w(@Nullable String tag, @Nullable String msg) {
addAsBreadcrumb(tag, SentryLevel.WARNING, msg);
addAsLog(SentryLogLevel.WARN, msg, null);
return Log.w(tag, msg);
}

public static int w(@Nullable String tag, @Nullable String msg, @Nullable Throwable tr) {
addAsBreadcrumb(tag, SentryLevel.WARNING, msg, tr);
addAsLog(SentryLogLevel.WARN, msg, tr);
return Log.w(tag, msg, tr);
}

public static int w(@Nullable String tag, @Nullable Throwable tr) {
addAsBreadcrumb(tag, SentryLevel.WARNING, tr);
addAsLog(SentryLogLevel.WARN, null, tr);
return Log.w(tag, tr);
}

public static int e(@Nullable String tag, @Nullable String msg) {
addAsBreadcrumb(tag, SentryLevel.ERROR, msg);
addAsLog(SentryLogLevel.ERROR, msg, null);
return Log.e(tag, msg);
}

public static int e(@Nullable String tag, @Nullable String msg, @Nullable Throwable tr) {
addAsBreadcrumb(tag, SentryLevel.ERROR, msg, tr);
addAsLog(SentryLogLevel.ERROR, msg, tr);
return Log.e(tag, msg, tr);
}

public static int wtf(@Nullable String tag, @Nullable String msg) {
addAsBreadcrumb(tag, SentryLevel.ERROR, msg);
addAsLog(SentryLogLevel.FATAL, msg, null);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'm not too sure about the log level here.
should we use error instead of fatal?

return Log.wtf(tag, msg);
}

public static int wtf(@Nullable String tag, @Nullable Throwable tr) {
addAsBreadcrumb(tag, SentryLevel.ERROR, tr);
addAsLog(SentryLogLevel.FATAL, null, tr);
return Log.wtf(tag, tr);
}

public static int wtf(@Nullable String tag, @Nullable String msg, @Nullable Throwable tr) {
addAsBreadcrumb(tag, SentryLevel.ERROR, msg, tr);
addAsLog(SentryLogLevel.FATAL, msg, tr);
return Log.wtf(tag, msg, tr);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,9 @@ class InternalSentrySdkTest {
Sentry.configureScope { scope ->
assertEquals(3, scope.breadcrumbs.size)
}

// Ensure we don't interfere with other tests
Sentry.configureScope(ScopeType.GLOBAL) { scope -> scope.clear() }
}

@Test
Expand Down
Loading
Loading