touchlab / Kermit

Kermit by Touchlab is a Kotlin Multiplatform centralized logging utility.
https://kermit.touchlab.co
Apache License 2.0
732 stars 41 forks source link

minSeverity not work on iOS #380

Open KevinnZou opened 1 year ago

KevinnZou commented 1 year ago

Logger.d does not print logs on iOS platforms even if I set minSeverity to Debug.

kpgalligan commented 1 year ago

Which version of Kermit? How is your Kermit configured? What log writer(s) are you using?

KevinnZou commented 1 year ago

I am using version 2.0.0-RC5. I tried it in several ways but all failed.

  1. Directly using the global Logger Logger.d { "" }
  2. Using global Logger and set min severity first
    Logger.setMinSeverity(Severity.Debug)
    Logger.d { "" }
  3. Using a custom Logger
    internal object KLogger : Logger(
    config = loggerConfigInit(
        platformLogWriter(DefaultFormatter),
        minSeverity = Severity.Debug
    ),
    tag = "ComposeWebView"
    )

    All three ways do not work and it will only print out the logs with Logger.i or higher severity. They work fine on Android and Desktop platforms

KevinnZou commented 1 year ago

@kpgalligan Are there any updates?

KevinnZou commented 1 year ago

@kpgalligan You can directly test this project https://github.com/KevinnZou/compose-webview-multiplatform. Logger.d works well on Android and Desktop but fails on iOS.

kpgalligan commented 1 year ago

I added this:

fun MainViewController() = ComposeUIViewController {
    Logger.v { "Verbose" }
    Logger.d { "Debug" }
    Logger.i { "Info" }
    Logger.w { "Warn" }
    Logger.e { "Error" }
    WebViewApp()
}

This was printed in the Xcode log:

⚪️ Verbose
🔵 Debug
🟢 Info
🟡 Warn
🔴 Error
Screenshot 2023-10-26 at 8 11 35 AM

If you're not seeing this, then I'm guessing you have a min log set in Xcode?

kpgalligan commented 1 year ago

Added my changes https://github.com/kpgalligan/compose-webview-multiplatform

KevinnZou commented 1 year ago

@kpgalligan Thanks for your reply! I am wondering did you test the log in Android Studio? Since we can directly run iosApp in Android Studio and see logs in the console, we don't need to open Xcode at all.

kpgalligan commented 1 year ago

I never run iOS apps from Android Studio :) Hmm.

kpgalligan commented 1 year ago

I've looked at it in AS. I can't find any config to set the level that is logged there. I assume the KMM plugin for AS is not showing everything for whatever reason. By that I mean they've made a decision to not show OS_LOG_TYPE_DEBUG statements in the log for whatever reason.

I will need to think about how to make some kind of config for the Apple-side log writer to allow you to override how that log writer works by default. We can't (AFAIK) detect if you're in AS, Xcode, or nothing. I also don't really want to force all verbose and debug statements to output at OS_LOG_TYPE_INFO solely because of the KMM AS plugin.

So, maybe there's a way to pass a param to platformLogWriter, but that's a pretty specific param for a global helper. Maybe just OSLogWriter/XcodeSeverityWriter, but then the out-of-the-box experience isn't ideal.

Anyway, for reference, here's the code mapping log levels:

private fun kermitSeverityToOsLogType(severity: Severity): UByte = when (severity) {
        Severity.Verbose, Severity.Debug -> OS_LOG_TYPE_DEBUG
        Severity.Info -> OS_LOG_TYPE_INFO
        Severity.Warn -> OS_LOG_TYPE_DEFAULT
        Severity.Error -> OS_LOG_TYPE_ERROR
        Severity.Assert -> OS_LOG_TYPE_FAULT
    }

If I'd known about this earlier I would've at least made that open and not private, so writing a custom version would involve minimal code.

I think this needs to have a bug filed for the KMM Plugin for AS, as it is dropping log statements. You should be able to select your own filter there, similar to Logcat's view

Screenshot 2023-10-26 at 3 15 33 PM
KevinnZou commented 1 year ago

@kpgalligan Thank you for your reply! I truly appreciate your investigation. I also attempted to run the app in Xcode and was able to see all the logs. Therefore, as you mentioned, the issue lies with the KMM Plugin for AS. I will open an issue with them. Thank you once again for your help!

hanrw commented 8 months ago

same issue with AppCode but not Xcode AppCode show below

2024-03-10 22:35:21.140636+0800 Mint[29800:899637] 🟢 info
2024-03-10 22:35:21.140694+0800 Mint[29800:899637] 🟡 warning
2024-03-10 22:35:21.140731+0800 Mint[29800:899637] 🔴 error

Xcode shows below

🔵 debug
🟢 info
🟡 warning
🔴 error