Kotlin Android print to console

AndroidPrintingKotlin

Android Problem Overview


I need to print some str to console (Android Studio) using Kotlin. I've tried the:

Log.v() 
Log.d() 
Log.i() 
Log.w() 
Log.e() 

methods. But it seems to work only on Java. What should I use to print using Kotlin? Thanks

Android Solutions


Solution 1 - Android

There are a couple of ways.

You can use Log.d("TAG", "message"); for example but first you need to import Log.

import android.util.Log

{...}

Log.d("TAG", "message")

{...}

Source: https://developer.android.com/reference/android/util/Log.html

You can also use kotlin's print and println function.

Example:

{...}

print("message")

println("other message")

{...}

Source: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.io/

Solution 2 - Android

I've written some extension functions that make use of reified type parameters in order to avoid dealing with declaring log tags in all project's classes. The basic idea is shown by the following snippet:

inline fun <reified T> T.logi(message: String) = Log.i(T::class.java.simpleName, message)

Basically, you can log something to the logcat with the following invocation (W/O external dependencies):

logi("My log message")

You can find a gist here. The functions declared in the gist are a little more elaborated given that they allow:

  • Lazy evaluation of the string to be logged out (if for example the string needs to be generated in some way)
  • Logging only when in debug mode by default
  • Use a given class name when you need to log from within an anonymous class that has no name

Solution 3 - Android

androidKotlin is deprecated and use Anko instead.

https://github.com/Kotlin/anko/wiki/Anko-Commons-%E2%80%93-Logging

class SomeActivity : Activity(), AnkoLogger {
    private fun someMethod() {
        info("London is the capital of Great Britain")
        debug(5) // .toString() method will be executed
        warn(null) // "null" will be printed
    }
}

Solution 4 - Android

At this moment (android studio 2.3.3 with Kotlin plugin),

Log.i(TAG, "Hello World")

Just works. It will import android.util.Log

Solution 5 - Android

You can use Anko library to do it. You would have code like below:

class MyActivity : Activity(), AnkoLogger {
    private fun someMethod() {
        info("This is my first app and it's awesome")
        debug(1234) 
        warn("Warning")
    }
}

or you can also use this small written in Kotlin library called StaticLog then your code would looks like:

Log.info("This is an info message")
Log.debug("This is a debug message")
Log.warn("This is a warning message","WithACustomTag")
Log.error("This is an error message with an additional Exception for output", "AndACustomTag", exception )

Log.logLevel = LogLevel.WARN
Log.info("This message will not be shown")\

The second solution might better if you would like to define an output format for logging method like:

Log.newFormat {
    line(date("yyyy-MM-dd HH:mm:ss"), space, level, text("/"), tag, space(2), message, space(2), occurrence)
}

or use filters, for example:

Log.filterTag = "filterTag"
Log.info("This log will be filtered out", "otherTag")
Log.info("This log has the right tag", "filterTag")

If you already used Jake Wharton's Timber logging library check this project: https://github.com/ajalt/timberkt.

Check also: Logging in Kotlin & Android: AnkoLogger vs kotlin-logging

Hope it will help

Solution 6 - Android

For example:

Log.i("info message")
Log.d("debug message")
Log.w("warning message","warningOutPut")
Log.e("error message","AndACustomTag",exception)

You can write them with kotlin too.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPavel BredelevView Question on Stackoverflow
Solution 1 - AndroidronniemagattiView Answer on Stackoverflow
Solution 2 - AndroidPaoloView Answer on Stackoverflow
Solution 3 - AndroidYe Lin AungView Answer on Stackoverflow
Solution 4 - AndroidPedro GonzalezView Answer on Stackoverflow
Solution 5 - Androidpiotrek1543View Answer on Stackoverflow
Solution 6 - AndroidyagmurerdoganView Answer on Stackoverflow