Skip to content

Commit

Permalink
KTOR-1219 Initialize Android logger lazily
Browse files Browse the repository at this point in the history
  • Loading branch information
Stexxe committed Apr 3, 2024
1 parent 1d98272 commit 941ff17
Showing 1 changed file with 14 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,24 @@ public actual val Logger.Companion.DEFAULT: Logger
* Breaks up long log messages that would be truncated by Android's max log
* length of 4068 characters.
*/
public val Logger.Companion.ANDROID: Logger
get() {
val logger = Logger.DEFAULT

val logClass = try {
Class.forName("android.util.Log")
} catch (_: ClassNotFoundException) {
return MessageLengthLimitingLogger(delegate = logger)
}
public val Logger.Companion.ANDROID: Logger by lazy { getAndroidLogger() }

if (LoggerFactory.getILoggerFactory() !is NOPLoggerFactory) {
return MessageLengthLimitingLogger(delegate = logger)
}
private fun getAndroidLogger(): Logger {
val logger = Logger.DEFAULT

val logClass = try {
Class.forName("android.util.Log")
} catch (_: ClassNotFoundException) {
return MessageLengthLimitingLogger(delegate = logger)
}

return MessageLengthLimitingLogger(delegate = LogcatLogger(logClass, logger))
if (LoggerFactory.getILoggerFactory() !is NOPLoggerFactory) {
return MessageLengthLimitingLogger(delegate = logger)
}

return MessageLengthLimitingLogger(delegate = LogcatLogger(logClass, logger))
}

private class LogcatLogger(logClass: Class<*>, private val fallback: Logger) : Logger {
private val tag = "Ktor Client"

Expand Down

0 comments on commit 941ff17

Please sign in to comment.