Skip to content

TobiasUhmann/android-studio-log-templates-for-kotlin

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Android Studio Log Templates for Kotlin

Android Studio provides 'Live Templates' for inserting recurring code snippets like loops and log statements. For example, hitting TAB when entering logd will produce

    Log.d(TAG, "divide: foo");

Not all templates are available for Kotlin, yet. To spare you the time of creating them, this repo provides the Kotlin version of the log templates.

Usage

OR

  • Download templates/AndroidLogKotlin.xml and move it into Android Studio's templates directory (on Windows: C:\Users\John\.AndroidStudio3.1\config\templates)

The templates will be available after restarting Android Studio.

Overview

The following subsections show the main differences of Kotlin (below) compared to Java (above):

  • No static members
  • No trailing semicolon
  • Usage of string templates

logt - A static logtag with your current classname

    private static final String TAG = "MainActivity";
    companion object {
        private const val TAG = "MainActivity"
    }

logd - Log.d(TAG, String)

    Log.d(TAG, "divide: foo");
    Log.d(TAG, "divide: foo")

loge - Log.e(TAG, String, Exception)

    Log.e(TAG, "divide: foo", e);
    Log.e(TAG, "divide: foo", e)

logi - Log.i(TAG, String)

    Log.i(TAG, "divide: foo");
    Log.i(TAG, "divide: foo")

logm - Log method name and its arguments

    Log.d(TAG, "divide() called with: a = [" + a + "], b = [" + b + "]");
    Log.d(TAG, "divide() called with: a = [$a], b = [$b]")

logr - Log result of this method

    Log.d(TAG, "divide() returned: " + result);
    Log.d(TAG, "divide() returned: $result")

logw - Log.w(TAG, String, Exception)

    Log.w(TAG, "divide: foo", e);
    Log.w(TAG, "divide: foo", e)

wtf - Log.wtf(TAG, String, Exception)

    Log.wtf(TAG, "divide: foo", e);
    Log.wtf(TAG, "divide: foo", e)

Extended Log Templates

Personally, I use modified versions of the built-in templates that add the current thread name and an unambigious string ('###') to the log output. Printing the thread name proved to be invaluable when debugging multi-threaded apps. The string allows for easy filtering the log output generated by your app.

For example, for logd there is logdt that becomes

    Log.d(TAG, Thread.currentThread().name + " ### "
                + "divide: foo")

The resulting log entry looks something like this:

05-11 21:48:30.338 4988-4988/com.example.test D/MainActivity: main ### divide: foo

The string is part of the message instead of the TAG because the latter is omitted from the log occasionally.

Usage

OR

Note: Any changes you've made to the Java log templates will get lost as the 'AndroidLog.xml' file is overwritten. You can prevent this by merging the XML files manually.

The templates will be available after restarting Android Studio.

Modify Templates

One can edit these templates and add new ones in Android Studio under 'Settings > Editor > Live Templates'.

Also, you can restore the built-in templates there if any problems occur after the import.

About

Android Studio Live Templates for Logging in Java

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published