Code Monkey home page Code Monkey logo

kotlin_android_live_template's Introduction

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

Windows: <user_home>\.AndroidStudio<version>\config\templates
Linux: ~/.AndroidStudio<version>/config/templates
macOS: ~/Library/Preferences/Android Studio<version>/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.

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.

kotlin_android_live_template's People

Contributors

timtsj avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.