Skip to content

Instantly share code, notes, and snippets.

@ErikHellman
Last active April 1, 2022 03:04
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ErikHellman/084e22d928398dd5faf3d2fc196744eb to your computer and use it in GitHub Desktop.
Save ErikHellman/084e22d928398dd5faf3d2fc196744eb to your computer and use it in GitHub Desktop.
A super tiny wrapper for Android Log utility that allows log printing in unit tests. For a more advanced log wrapper for Android, see https://github.com/JakeWharton/timber/
/*
Licensed under Apache 2.0 (https://www.apache.org/licenses/LICENSE-2.0.txt)
This is a tiny log utility for Android Logcat. It allows logs to be used in code that is unit
tested on host (without Android framework available).
The only advantage with this utility above Timber is that it doesn't require any setup.
For real-world, production application I strongly recommend using Timber (see https://github.com/JakeWharton/timber/) instead.
*/
@file:Suppress("unused")
package se.hellsoft.kittlog
import android.util.Log
// TODO: Change this to whatever you want your logcat tag to be.
internal const val TAG = "KittyLog"
fun logi(message: String, throwable: Throwable? = null) {
try {
Log.i(TAG, message, throwable)
} catch (e: RuntimeException) {
println(message)
throwable?.printStackTrace()
}
}
fun logv(message: String, throwable: Throwable? = null) {
try {
Log.v(TAG, message, throwable)
} catch (e: RuntimeException) {
println(message)
throwable?.printStackTrace()
}
}
fun logd(message: String, throwable: Throwable? = null) {
try {
Log.d(TAG, message, throwable)
} catch (e: RuntimeException) {
println(message)
throwable?.printStackTrace()
}
}
fun logw(message: String, throwable: Throwable? = null) {
try {
Log.w(TAG, message, throwable)
} catch (e: RuntimeException) {
println(message)
throwable?.printStackTrace()
}
}
fun loge(message: String, throwable: Throwable? = null) {
try {
Log.e(TAG, message, throwable)
} catch (e: RuntimeException) {
println(message)
throwable?.printStackTrace()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment