sentry-java: Sentry Android does not capture LogCat logs

Platform:

  • Android -> If yes, which Device API (and compileSdkVersion/targetSdkVersion/Build tools) version?
  • Java -> If yes, which Java (and sourceCompatibility/targetCompatibility) version?
  • Kotlin -> If yes, which Kotlin (and jvmTarget) version?
  • NDK -> If yes, which NDK/CMake version?
  • React-Native -> If yes, which version?
  • Timber -> If yes, which version?
  • Log4j2 -> If yes, which version?
  • Logback -> If yes, which version?
  • Spring -> If yes, which version?

IDE:

  • Android Studio -> If yes, which version?
  • IntelliJ -> If yes, which version?
  • Other -> If yes, which one?

Build system:

  • Gradle -> If yes, which version?
  • Buck -> If yes, which version?
  • Bazel -> If yes, which version?
  • Maven -> If yes, which version?
  • Other -> If yes, which one?

Android Gradle Plugin:

  • Yes -> If yes, which version?
  • No

Sentry Android Gradle Plugin:

  • Yes -> If yes, which version?
  • No

Proguard/R8:

  • Enabled
  • Disabled

Platform installed with:

  • JCenter
  • Bintray
  • Maven Central
  • Manually

The version of the SDK: 3.x.x


I have the following issue:

The description goes here …

Steps to reproduce:

  • run sample

Actual result: Sentry Android does not capture LogCat logs

Expected result: when having things like Log.i(...), I hope sentry can automatically capture it. thank you!

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 4
  • Comments: 32 (17 by maintainers)

Most upvoted comments

Our logcat integration only includes creating breadcrumbs out of logs with a specified minLevel which you can configure in your build.gradle (which also includes logs by third-party libraries since our implementation is based on bytecode manipulation).

If you still want to log the last n number of logs as an attachment you can still do that as you currently do by adding an EventProcessor and injecting a logcat shell command.

We can execute Runtime.getRuntime().exec("logcat -v time"); and listen the buffer in a while loop. https://github.com/pedrovgs/Lynx/blob/master/lynx/src/main/java/com/github/pedrovgs/lynx/model/Logcat.java

@kasogg You would have to manually replace all of your Log.e/d/w... calls with SentryLogcatAdapter.e/d/w..., though the class is marked as Internal, so we do not guarantee api stability.

We can reconsider though, if there’s a good usecase. Are you fine with manually replacing the log calls? We could make it officially public.

My requirement is:

  1. Only when the user agrees privacy policy can app upload the logcat message.
  2. I want to upload not only app logs but also system logs(Not necessarily)
  3. logs upload only when Crash/ANR

I find a workaround:

options.addEventProcessor(object : EventProcessor {
     override fun process(event: SentryEvent, hint: Hint): SentryEvent? {
          if (event.isCrashed || event.exceptions?.any { it.mechanism?.type == "ANR" } == true) {
                                    Runtime.getRuntime().exec("logcat -t 1000 *:I").inputStream.use { input ->
                                        application.openFileOutput("sentry_logcat.txt", MODE_PRIVATE).use { out ->
                                            input.copyTo(out)
                                        }
                                    }
                                    hint.addAttachment(Attachment(application.filesDir.path + "/sentry_logcat.txt", "sentry_logcat.txt", "text/plain"))
                            }
                            return super.process(event, hint)
                        }
                    })
                        

Is there a way to manual install Logcat Integration? I don’t want to install with the Sentry Android Gradle Plugin. @buenaflor

https://developer.android.com/studio/preview/features#logcat

Not sure if the logcat output has changed or the IDE is parsing it, but worth checking it when working on this issue, worst case we have to parse 2 formats.

Use code below to upload logcat including native crash happened

fun initSentry(context: Context, dsn: String) {
    SentryAndroid.init(context) {
        it.dsn = dsn
        it.tracesSampleRate = 0.03
        it.addEventProcessor(object: EventProcessor {
            override fun process(event: SentryEvent, hint: Any?): SentryEvent? {
                Runtime.getRuntime().exec("logcat -t 1000 *:I").inputStream.use { input ->
                    context.openFileOutput("logcat.txt", MODE_PRIVATE).use { out ->
                        input.copyTo(out)
                    }
                }
                return super.process(event, hint)
            }
        })
        it.isEnableScopeSync = true
    }
    Sentry.configureScope { scope ->
        scope.addAttachment(
                Attachment(
                        context.filesDir.path + "/logcat.txt",
                        "logcat.txt", "text/plain"
                )
        )
    }
}

Sounds great! Will it require extra permission or slow down the app - inside release app builds?

It does, but not for all versions apparently. https://github.com/pedrovgs/Lynx/blob/7b1f25172f2bd3f4563076dcd58ec532aebfb9f1/sample/src/main/AndroidManifest.xml#L20-L21 Well, ideally this is done on a background thread, but I don’t have benchmarking data, it’d be an opt-in feature till we get proper feedback anyway.

it’d be possible ootb only via bytecode manipulation or executing logcat params... and writing the output to a file or parsing the text, of course. I’m not sure if it’s a good idea though since it’s rather a good practice to remove every logcat call from production apps via Proguard/R8 to reduce the app’s size, there’s some cost in it, but obviously, a nice feature to have. I’m just questioning the usage of logcat in production Apps here to identify issues.