47

In Intellij Idea when i typing psfs and then press Ctrl+J IDE was getting me a dialog :

enter image description here

And when i press Enter i get an

enter image description here

I know where i can customize my own output

enter image description here

But i can't any doc's how i can write my own live template.

In the end i want to get next result :

Typing : psfst -> press Ctrl+J -> press Enter

Result :

public static final String TAG = <currentClassName>.class.getSimpleName();

It will be so helpfull, because i have a habit to log my classes.

Community
  • 1
  • 1
Sergey Shustikov
  • 15,377
  • 12
  • 67
  • 119

6 Answers6

51

I find a solution

1) Create a new live template in plain group
2) In template text :

private static final String TAG = $CLASS_NAME$.class.getSimpleName();

3) Define a usage scope :

enter image description here

4) Choose a shortcut :

enter image description here

finally click on Edit variables and change expression value to className()

enter image description here

Click Ok , Apply, Ok and use.

Sergey Shustikov
  • 15,377
  • 12
  • 67
  • 119
41

There already exists such shortcut in Android Studio - write logt and enter while cursor is at the class scope.

more here :

for more see here

Ofek Ron
  • 8,354
  • 13
  • 55
  • 103
  • Not exactly. It will generated template as `private static final String TAG = "";` not requested. But I will plus, because however it good 0,02$ to this. – Sergey Shustikov May 31 '16 at 08:08
17

It's super simple in Android studio, just type logt and press Tab.

It generates: private static final String TAG = "xyzActivity";

CapTen101
  • 476
  • 6
  • 12
11

For those using Android Studio 3 and Kotlin it is necessary to change how Live Templates set:

Editor >> Live Templates >> AndroidLog

By default it is just for Java

enter image description here

Add Kotlin pressing "change" button and check Kotlin

enter image description here

and after that it will work again!

enter image description here


Updated: 2020

Android Studio: 3.6.2

enter image description here

Steps:

  1. Add
  2. Call logtk are whatever you want to call
  3. Use this code as template text
private val TAG = this::class.java.simpleName
  1. Add a description
  2. Do it applicable to Kotlin: in my case I used just for Class

Update June 2020

There's no need to set anything up anymore. Android Studio 4.0 comes with this functionality by default. The same with Toast

enter image description here

Jorge Casariego
  • 21,948
  • 6
  • 90
  • 97
  • `this` is evaluated for each instance. But the class name is static. Therefor it might be better to put the Log TAG into the companion object. – ceving Nov 24 '22 at 16:19
1

Another solution is not use a default TAG for each class and use this method to get the TAG:

public class Utils {
        public static String getTAG(Object o) {
            StackTraceElement[] elements = Thread.currentThread().getStackTrace();

            int position = 0;
            for (int i = 0; i < elements.length; i++) {
                if (elements[i].getFileName().contains(o.getClass().getSimpleName())
                        && !elements[i+1].getFileName().contains(o.getClass().getSimpleName())){
                    position = i;
                    break;
                }
            }

            StackTraceElement element = elements[position];
            String className = element.getFileName().replace(".java", "");
            return "[" + className + "](" + element.getMethodName() + ":" + element.getLineNumber() + ")";
        }
}

Example call from MainActivity - onResume:

Log.v(Utils.getTAG(this), "hello world");

Log output:

[MainActivity](onResume:79): hello world
elabi3
  • 70
  • 3
0

For Kotlin create a live template with private val TAG = $CLASS_NAME$::class.java.simpleName as the template text and in set expression for CLASS_NAME as kotlinClassName() in edit variables.