oppia / oppia-android

A free, online & offline learning platform to make quality education accessible for all.
https://www.oppia.org
Apache License 2.0
311 stars 512 forks source link

[BUG]: ConsoleLogger overwrites local log file for each line write #5232

Open BenHenning opened 10 months ago

BenHenning commented 10 months ago

Describe the bug

ConsoleLogger has support for logging output lines to a local log (rather than just logcat). However, it seems that this is currently configured such that each line will completely overwrite the original log file.

Steps To Reproduce

Expected Behavior

All lines should be kept, not just the most recent.

Screenshots/Videos

No response

What device/emulator are you using?

Pixel 6a emulator

Which Android version is your device/emulator running?

SDK 33

Which version of the Oppia Android app are you using?

0.11-dev-80a9a09723

Additional Context

From the logic in ConsoleLogger:

https://github.com/oppia/oppia-android/blob/6006277f33af2e981964fc4ab58e366c7f39e21a/utility/src/main/java/org/oppia/android/util/logging/ConsoleLogger.kt#L108

I suspect this is caused by the open FileWriter just always, by default, overwriting the file. We should, instead, open the writer in append mode & add tests to verify that this functionality now works.

Also, it's probably a good idea to keep a long-lived PrintStream open for the log file rather than reopening it for each line (which is going to be less performant). We should overwrite the file in this case (to avoid indefinitely using more disk space for debug logs). This includes adding tests for verifying that multiple ConsoleLogger instances do overwrite the file from the previous logger.

BenHenning commented 10 months ago

Suggest that this is a good potential starter issue since it should be straightforward to investigate, fix, and add tests for it.

kmanikanta335 commented 9 months ago

can i work on this issue @adhiamboperes

adhiamboperes commented 9 months ago

Hi @kmanikanta335. Could you please share an overview of your proposed solution?

kmanikanta335 commented 9 months ago

@adhiamboperes proposed solution

  1. Open ConsoleLogger.kt: Locate the file ConsoleLogger.kt in the Oppia Android project.

  2. Modify File Opening Mode: Change the file opening mode to append. Replace the existing line: blockingScope.launch { logDirectory.printWriter().use { out -> out.println(text) } } with: blockingScope.launch { logDirectory.appendText("$text\n") }

  3. Use Long-Lived PrintStream: Maintain a long-lived PrintStream for the log file. You can create it during the initialization of the ConsoleLogger or at an appropriate point where it won't be repeatedly opened. For example:

    private val logPrintStream: PrintStream = logDirectory.appendText().bufferedWriter().use { it }

    // ...

    blockingScope.launch { logPrintStream.println(text) }

adhiamboperes commented 9 months ago

@kmanikanta335, you can put up a draft PR with these changes that you have proposed.

Saifuddin53 commented 9 months ago

Can I work on this issue as @kmanikanta335 has not posted any PR till now?