0

I'm trying to use the new Android Logger for the first time. My TestApplication is:

package com.example.test

import android.app.Application
import android.util.Log
import timber.log.Timber

class TestApplication : Application() {

    override fun onCreate() {
        super.onCreate()
        Log.i("test","Creating application")
        if(BuildConfig.DEBUG){
            Timber.plant(Timber.DebugTree())
        }
    }
}

And my MainActivity is here:

package com.example.test

import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import com.example.test.ui.theme.TestTheme
import timber.log.Timber

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.i("test","Creating activity")
        Timber.i("Activity Created")
        setContent {
            TestTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colors.background
                ) {
                    Greeting("Android")
                }
            }
        }
        Timber.tag("Yo").w("used custom tag for logs")
    }
}

@Composable
fun Greeting(name: String) {
    Text(text = "Hello $name!")
}

@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
    TestTheme {
        Greeting("Android")
    }
}

I'm just simply trying to test the new logcat. I've read the logcat documentation a few times here. When I start up my app, with debugging enabled, to my test phone (A Pixel 7 Pro), this is my logcat output:

2023-04-30 16:41:18.947  3403-3489  ProfileInstaller        com.example.test                     D  Installing profile for com.example.test

I don't see any of my Log statements, unless I hit the restart logcat button. Then it seems like logcat retroactively adds the logs that should have been there:

2023-04-30 16:41:13.759  3403-3403  test                    com.example.test                     I  Creating application
2023-04-30 16:41:13.780  3403-3403  test                    com.example.test                     I  Creating activity
2023-04-30 16:41:13.782  3403-3403  MainActivity            com.example.test                     I  Activity Created
2023-04-30 16:41:13.796  3403-3403  Yo                      com.example.test                     W  used custom tag for logs
2023-04-30 16:41:18.947  3403-3489  ProfileInstaller        com.example.test                     D  Installing profile for com.example.test

I then close the app on the phone completely. The documentation says that I should see Process Ended message, but I do not. I restart the app. I get another ProfileInstaller log, but nothing else. I click the restart logcat button and then get the logs I included in my code.

Why do I need to press restart logcat each time in order to get the logs I programmatically added to my app? This makes it hard to achieve the key improvement in this new logcat, which is tracking the same log through multiple crashes and restart.

I tried playing with the logcat buffer sizes in both AndroidStudio and my phone's developer options, but it didn't change anything. I also think the default size of 1024 kB should be plenty for logging. Is there any other settings I'm missing?

MW2023
  • 183
  • 3
  • 12
  • Hm, there doesn't seem to be a Timber.flush() method, at least in version 5.0.1 that I'm using. But this is also happening with my native `Log` calls as well... – MW2023 May 01 '23 at 02:45

0 Answers0