coderain blog

How to Auto-Generate Android Log TAG with Custom Live Template in Android Studio/IntelliJ IDEA (psfst Shortcut)

If you’ve ever worked on an Android project, you’re likely familiar with the Log class (or libraries like Timber) used for debugging. A critical part of logging is the TAG constant—a string identifier that helps filter logs by source. Typically, a TAG is defined as private static final String TAG = "ClassName", where ClassName matches the name of the class it belongs to.

Manually typing this line in every class is tedious, error-prone (e.g., typos in the class name), and repetitive. Fortunately, Android Studio (and IntelliJ IDEA) offers a powerful feature called Live Templates that lets you automate this process. In this guide, we’ll create a custom live template with the shortcut psfst (mnemonic: Private Static Final String Tag) to auto-generate the TAG line in seconds.

2025-12

Table of Contents#

Prerequisites#

  • Android Studio (v4.0+ recommended) or IntelliJ IDEA (Community/Ultimate Edition).
  • Basic familiarity with Android development and logging (e.g., using Log.d(TAG, "message")).

Step-by-Step Guide to Creating the Live Template#

Step 1: Access Live Template Settings#

First, open the Live Templates configuration menu:

  1. Go to File > Settings (or Android Studio > Preferences on macOS).
  2. In the left sidebar, navigate to Editor > Live Templates.

This is where you’ll manage existing templates and create new ones.

Step 2: Create a Custom Template Group#

To keep your templates organized, create a dedicated group for Android-related templates:

  1. In the Live Templates window, click the + icon (top-right) and select Template Group.
  2. Name the group Android Custom (or any name you prefer) and click OK.

Your new group will appear in the list of template groups.

Step 3: Add a New Live Template#

Now, add a live template to your new group:

  1. Select the Android Custom group.
  2. Click the + icon again and select Live Template.

You’ll now see fields to configure your template:

Step 4: Define the Template Text#

Configure the core details of the template:

FieldValue
Abbreviationpsfst (this is the shortcut you’ll type to trigger the template).
DescriptionAuto-generates Android Log TAG (private static final String TAG).
Template Textprivate static final String TAG = "$CLASS_NAME$";

Why this works:

  • private static final String TAG: The standard modifier for a TAG (immutable, class-level constant).
  • $CLASS_NAME$: A placeholder variable that will be replaced with the current class name (we’ll define this next).

Step 5: Configure Template Variables#

The $CLASS_NAME$ variable needs to dynamically fetch the class name of the file you’re working in. Here’s how to set it up:

  1. Click the Edit Variables button (below the template text box).
  2. In the Edit Template Variables window:
    • For the CLASS_NAME variable, set the Expression to className(). This is a built-in function that returns the name of the current class.
    • Check the Skip if defined box to auto-insert the class name without prompting you.
  3. Click OK to save the variable.

Step 6: Set the Template Context#

By default, live templates work in all file types. We need to restrict this template to Java and Kotlin files (the primary languages for Android development):

  1. Click the Define link (below the template text box, next to "No applicable contexts yet").
  2. In the context menu, check the boxes for:
    • Java: Under Java > Declaration.
    • Kotlin: Under Kotlin > Top-level declarations and Kotlin > Class bodies.
  3. Click OK to confirm.

Final Template Setup#

Your template should now look like this:

  • Abbreviation: psfst
  • Description: Auto-generates Android Log TAG...
  • Template Text: private static final String TAG = "$CLASS_NAME$";
  • Variables: CLASS_NAME = className(), Skip if defined.
  • Context: Java and Kotlin files.

Testing the psfst Template#

Let’s verify the template works in both Java and Kotlin files:

Test in a Java Class#

  1. Open a Java class (e.g., MainActivity.java).
  2. Inside the class body (after the class declaration), type psfst and press Tab (or your configured expansion key).

Expected Output:

public class MainActivity extends AppCompatActivity {  
    private static final String TAG = "MainActivity"; // Generated by psfst  
 
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        Log.d(TAG, "onCreate: Activity started"); // Use the TAG!  
    }  
}  

Test in a Kotlin Class#

For Kotlin, the syntax for constants is slightly different (val instead of static final). We’ll adjust the template text for Kotlin (see Customization Tips below), but even with the Java template, typing psfst in a Kotlin file will generate:

class MainActivity : AppCompatActivity() {  
    private static final String TAG = "MainActivity"; // Works, but not idiomatic Kotlin  
}  

To fix this, we’ll create a Kotlin-specific template in the next section.

Customization Tips#

1. Kotlin-Specific Template#

For idiomatic Kotlin, use val instead of static final. Create a duplicate template with:

  • Abbreviation: psfkt (mnemonic: Private Static Final Kotlin Tag).
  • Template Text: private val TAG: String = "$CLASS_NAME$"
  • Follow the same variable and context steps as before.

Now, in Kotlin files, type psfkt + Tab to get:

private val TAG: String = "MainActivity"  

2. Change the Shortcut#

To use a different shortcut (e.g., tag instead of psfst), simply update the Abbreviation field in the template settings.

3. Add a Prefix/Suffix to the TAG#

To make logs easier to filter (e.g., add a project prefix), modify the template text:

private static final String TAG = "MyApp_" + "$CLASS_NAME$"; // Java  
private val TAG: String = "MyApp_$CLASS_NAME" // Kotlin  

4. Integrate with Timber#

If you use Timber (a popular logging library), create a template to auto-initialize Timber.tag:

private val TAG = Timber.tag("$CLASS_NAME$") // Kotlin Timber template  

Troubleshooting Common Issues#

Template Doesn’t Appear When Typing psfst#

  • Check Context: Ensure you enabled Java/Kotlin contexts (Step 6).
  • File Type: The template only works in Java/Kotlin files. It won’t trigger in XML, Gradle, or other file types.
  • Abbreviation Conflict: Another template might use psfst. Rename your template’s abbreviation (e.g., mypsfst).

CLASS_NAME Shows as null or Incorrect#

  • Cursor Position: The template only works if your cursor is inside a class body (not outside or in a method).
  • Expression Error: Verify the CLASS_NAME variable uses className() (no typos) in Edit Variables.

Kotlin Template Generates static final (Java Syntax)#

  • Context Mismatch: Ensure the Kotlin template is restricted to Kotlin contexts (Step 6). Use a separate template for Kotlin (as shown in Customization Tips).

Conclusion#

Live templates are a game-changer for reducing boilerplate in Android development. With the psfst shortcut, you’ll never manually type a TAG again—saving time and avoiding typos.

Beyond TAG, explore live templates for other repetitive tasks: creating onCreate overrides, ViewHolder patterns, or even Retrofit service interfaces. The possibilities are endless!

References#

Happy coding, and may your logs be ever informative! 🚀