Skip to content
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

Android device logcat debug log cannot be output #349

Open
weijunfeng opened this issue May 26, 2023 · 0 comments
Open

Android device logcat debug log cannot be output #349

weijunfeng opened this issue May 26, 2023 · 0 comments

Comments

@weijunfeng
Copy link

Using Log.d(xx) to output logs on my Android device does not show up on the console.

I tried to solve this problem and found that I need to modify the log output in LogcatWriter

    override fun log(severity: Severity, message: String, tag: String, throwable: Throwable?) {
        val formattedMessage = messageStringFormatter.formatMessage(null, null, Message(message))
        try {
            if(throwable == null){
                when(severity){
                    Severity.Verbose -> Log.v(tag, formattedMessage)
                    Severity.Debug -> Log.d(tag, formattedMessage)
                    Severity.Info -> Log.i(tag, formattedMessage)
                    Severity.Warn -> Log.w(tag, formattedMessage)
                    Severity.Error -> Log.e(tag, formattedMessage)
                    Severity.Assert -> Log.wtf(tag, formattedMessage)
                }
            }else{
                when(severity){
                    Severity.Verbose -> Log.v(tag, formattedMessage, throwable)
                    Severity.Debug -> Log.d(tag, formattedMessage, throwable)
                    Severity.Info -> Log.i(tag, formattedMessage, throwable)
                    Severity.Warn -> Log.w(tag, formattedMessage, throwable)
                    Severity.Error -> Log.e(tag, formattedMessage, throwable)
                    Severity.Assert -> Log.wtf(tag, formattedMessage, throwable)
                }
            }
        } catch (e: Exception) {
            testWriter.log(severity, message, tag, throwable)
        }
    }

replace with

   private fun getSeverity(severity: Severity) = when (severity) {
        Severity.Verbose -> Log.VERBOSE
        Severity.Debug -> Log.DEBUG
        Severity.Info -> Log.INFO
        Severity.Warn -> Log.WARN
        Severity.Error -> Log.ERROR
        Severity.Assert -> Log.ASSERT
    }

    override fun log(severity: Severity, message: String, tag: String, throwable: Throwable?) {
        val formattedMessage = messageStringFormatter.formatMessage(null, null, Message(message))
        try {
            // 使用 Log.println 解决部分设备无法输出debug日志问题
            val stackTraceString = Log.getStackTraceString(throwable)
            Log.println(
                getSeverity(severity), tag, if (stackTraceString.isEmpty()) {
                    formattedMessage
                } else {
                    formattedMessage + '\n' + stackTraceString
                }
            )
        } catch (e: Exception) {
            testWriter.log(severity, message, tag, throwable)
        }
    }

Use Log.println to output logs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant