Plain Kotlin Android Style Logging
Plain Kotlin Android Style Logging
Overview
Android Studio Android Logging for any Kotlin project.
Introduction
In Android Studio Android projects, most people take advantage of the Android Logging Framework. However, if you write code externally or without that framework available, you would likely write all your output as println and then have to add the proper logging later.
Moving the code back and forth would require changes and rewrites.
To avoid this, I've put together a small logging class.
The Code
The code is pretty simple so here is everything:
package com.xxxxxxxxxxxxx
// https://source.android.com/docs/core/tests/debug/understanding-logging
/*
RFC 5424 Level RFC 5424 Severity RFC 5424 Description android.util.Log java.util.logging.Level
0 Emergency System is unusable Log.e / Log.wtf SEVERE
1 Alert Action must be taken immediately Log.e / Log.wtf SEVERE
2 Critical Critical conditions Log.e / Log.wtf SEVERE
3 Error Error conditions Log.e SEVERE
4 Warning Warning conditions Log.w WARNING
5 Notice Normal but significant Log.w WARNING
6 Info Information messaging Log.i INFO
7 Debug Debug-level messages Log.d CONFIG, FINE
- - Verbose messaging Log.v FINER / FINEST
*/
object Log {
// 256-Color (8-bit) ANSI Codes: \u001B[38;5;<color_index>m
private const val ANSI_RESET = "\u001B[0m"
private const val ANSI_COLOR_ERROR = "\u001B[38;5;203m" // Error - F75464 -> Bright Coral (203)
private const val ANSI_COLOR_INFO = "\u001B[38;5;179m" // Info - E0BB65 -> Mustard Yellow (179)
private const val ANSI_COLOR_DEBUG = "\u001B[38;5;30m" // Debug - 299999 -> Dark Cyan-Green (30)
fun wtf(tag: String, text: String) { println("$ANSI_COLOR_ERROR[E] $tag: $text$ANSI_RESET") } // ⚠️
fun e(tag: String, text: String) { println("$ANSI_COLOR_ERROR[E] $tag: $text$ANSI_RESET") } // ❌
fun w(tag: String, text: String) { println("$ANSI_COLOR_INFO[W] $tag: $text$ANSI_RESET") } // ⚠️
fun i(tag: String, text: String) { println("$ANSI_COLOR_INFO[I] $tag: $text$ANSI_RESET") } // ℹ️
fun d(tag: String, text: String) { println("$ANSI_COLOR_DEBUG[D] $tag: $text$ANSI_RESET") } // 🔍
fun v(tag: String, text: String) { println("[V] $tag: $text") } // 💬 // No color needed
object Tests {
fun testLogs() {
Log.wtf("TAG", "Text for WTF (severity 0-2 - Emergency, Alert, or Critical)")
Log.e("TAG", "Text for Error (severity 1-3 - Emergency, Alert, Critical, or Error)")
Log.w("TAG", "Text for Warning (severity 4-5 - Warning or Notice")
Log.i("TAG", "Text for Info (severity 6 - Info")
Log.d("TAG", "Text for Debug (severity 7 - Debug)")
Log.v("TAG", "Text for Verbose (-)")
}
}
}
Example Output
Android Logging:
Summary
Above is some colored logging for standard Kotlin projects that you can just copy into Android projects without worrying about anything.
If you haven't really looked into Logging yet, check the link from the start (repeated below).
Comments
Post a Comment