Speed Up Java Boilerplate with Custom IntelliJ Live Templates
Learn how to create a custom Live Template for a logger statement, see the setup steps, and understand the maintenance trade‑offs.
17 Apr 2026, 21:17 UTC

Problem: Repetitive Logger Boilerplate
When starting a new Java class, many developers type the same logger declaration:
private static final Logger LOG = LoggerFactory.getLogger(MyClass.class);Repeating this line across dozens of files wastes time and introduces the risk of typos or mismatched class names.
Thesis: A custom Live Template lets you expand a short abbreviation into the full logger statement with the correct class name inserted automatically.
Understanding Live Templates
Live Templates are IDE snippets that expand when you type an abbreviation and press Tab (or a shortcut you choose). Each template consists of:
- An abbreviation – the short trigger you type.
- The template text – the code that appears after expansion.
- Optional variables – placeholders that IntelliJ fills in using expressions or user input.
- A scope – the file types (Java, Kotlin, XML, etc.) where the template is active.
The underlying language is Velocity‑based, allowing simple expressions like $CLASS$ to retrieve the current class name.
Creating a Logger Template for Java
- Open Settings/Preferences → Editor → Live Templates.
- Select the Java group (or click the
+button and choose Live Template). - In the Abbreviation field, enter
logi. - In the Template text field, paste:
private static final Logger LOG = LoggerFactory.getLogger($CLASS$.class);
- Click Edit variables. Ensure that
CLASSis defined with the expressionclassName()(IntelliJ’s built‑in function that returns the name of the class containing the cursor). If the variable list is empty, addCLASSand set its expression toclassName().- Optionally check Skip if defined so IntelliJ does not ask for input when the variable can be resolved automatically.
- Click Apply and then OK to close the settings.
Testing the template
- Open or create a Java file, e.g.,
MyService.java.- Place the cursor inside the class body where you want the logger.
- Type
logiand press Tab.- You should see the expanded statement:
private static final Logger LOG = LoggerFactory.getLogger(MyService.class);The class name is correctly substituted, and the cursor ends after the semicolon, ready for the next line.
Sharing Templates Across a Team
Live Template definitions are stored in the IDE’s settings. To share them:
- Choose File → Manage IDE Settings → Export Settings.
- Deselect all options except Live Templates (you can also keep other categories if needed).
- Export to a
.jarfile and commit it to your version‑control repository, or send it directly to teammates.- Team members import the file via File → Manage IDE Settings → Import Settings and restart the IDE if prompted.
Because the template is scoped to Java, it will not trigger in Kotlin or SQL files unless you explicitly add those scopes in the template definition.
Trade‑offs and Limitations
- Maintenance overhead: A template with many variables or complex Velocity expressions can become hard to read. Keep each template focused on a single boilerplate pattern.
- Abbreviation collisions: If you choose an abbreviation that already exists in another plugin or built‑in template, the expansion may behave unexpectedly. Prefix your team’s abbreviations (e.g.,
tlogi) to reduce clashes. - Scope specificity: A template defined for Java will not appear in other languages unless you add those scopes. Forgetting to do so leads to the template seeming “missing” when you work in a Kotlin file.
- Learning curve: New team members need to know the abbreviation and that Tab triggers the expansion. Including the template in the project’s onboarding documentation helps.
Actionable Closing
Start by adding the logger template shown above; it costs only a few minutes to set up and saves seconds each time you create a class. After you confirm it works in a test file, export the Live Templates jar and share it with your team so everyone benefits from the same expansion. Periodically review your template list, retire any that are rarely used, and keep abbreviations short but unique to maintain a smooth workflow.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.