Skip to content

Instantly share code, notes, and snippets.

@soulduse
Last active September 12, 2018 05:48
Show Gist options
  • Save soulduse/c89aed8403ff4d5c50bda78cd47b7f27 to your computer and use it in GitHub Desktop.
Save soulduse/c89aed8403ff4d5c50bda78cd47b7f27 to your computer and use it in GitHub Desktop.
This is log util for Android. You don't need TAG and care about showing log after released.
/**
* Usage
* DLog.w("This is awesome!")
*
* print -> W/Dave: [xx.kt::methodName]This is awesome!
*/
object DLog {
private const val TAG : String = "Dave"
fun e(message : String){
if(BuildConfig.DEBUG) Log.e(TAG, buildLogMsg(message))
}
fun w(message : String){
if(BuildConfig.DEBUG) Log.w(TAG, buildLogMsg(message))
}
fun i(message : String){
if(BuildConfig.DEBUG) Log.i(TAG, buildLogMsg(message))
}
fun d(message : String){
if(BuildConfig.DEBUG) Log.d(TAG, buildLogMsg(message))
}
fun v(message : String){
if(BuildConfig.DEBUG) Log.v(TAG, buildLogMsg(message))
}
private fun buildLogMsg(message : String) : String{
val sb = StringBuilder()
try{
val ste : StackTraceElement ?= Thread.currentThread().stackTrace[4]
sb.run {
append("[")
append(ste?.fileName?.replace(".java", ""))
append("::")
append(ste?.methodName)
append("]")
append(message)
}
}catch (e: Exception){
Log.e(TAG, "buildLogMsg Error: ${e.localizedMessage}")
sb.append(e.localizedMessage)
}
return sb.toString()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment