Create a Logger Live Template in IntelliJ IDEA
Learn how to define a Live Template that expands 'logd' into a SLF4J logger statement, with steps, limits and verification tips.
29 May 2026, 19:01 UTC

Quick answer
In IntelliJ IDEA you can create a Live Template that expands a short abbreviation into a ready‑to‑use logger declaration. After the template is saved, typing the abbreviation and pressing Tab inserts the snippet with the current class name already filled in.
How a Live Template works
A Live Template consists of four parts:
- Abbreviation – the text you type to trigger the expansion.
- Template text – the code that appears, containing variables like $CLASS_NAME$.
- Expressions (optional) – Groovy scripts or default values that compute a variable’s value when the template expands.
- Context – the file types or scopes where the abbreviation is active (e.g., Java files).
When the abbreviation is recognized in a matching context, IDEA replaces it with the template text, evaluates any expressions, and places the cursor on the first editable variable so you can continue typing.
Worked example: a logger template
We will create a template called logd that expands to a typical SLF4J logger field.
- Open Settings (or Preferences on macOS) → Editor → Live Templates.
- Select the Java group, click the + button and choose Live Template.
- In the Abbreviation field enter logd.
- In the Template text field paste:
private static final Logger LOG = LoggerFactory.getLogger($CLASS_NAME$.class); - Leave the Expression field empty; the built‑in variable $CLASS_NAME$ already resolves to the name of the class where the template is expanded.
- Click Define next to the context and ensure only Java is checked (or create a custom scope limited to Java files).
- Press Apply and OK to save.
To use the template, open any Java class, place the cursor inside the class body, type logd and press Tab. The editor will replace the abbreviation with:
private static final Logger LOG = LoggerFactory.getLogger(YourClassName.class);
The cursor ends up after the inserted line, ready for you to add further code or move on.
Limits and common mistakes
Context too broad
If you leave the context set to Everywhere, typing logd in an XML or HTML file will also trigger the expansion, producing invalid markup. Keep the scope limited to Java (or to the specific file types where the snippet makes sense).
Stale snippets after editing the template
Changing the abbreviation, template text, or expressions does not modify code that was already generated. Existing logger fields stay as they were; you must re‑apply the template (e.g., delete the line and type logd + Tab) to see the update. Forgetting this step can lead to inconsistent code.
Sharing with a team
Live Template definitions are stored in the IDE settings. To share them, export the settings (File → Manage IDE Settings → Export Settings) and select only the Live Templates section, then distribute the resulting JAR or XML. Teammates import the file on their machines. Without this step, each developer must recreate the template manually.
Missing imports
The example assumes org.slf4j.Logger and org.slf4j.LoggerFactory are already imported. If they are not, the expansion will compile with errors. You can let IDEA’s auto‑import add the missing imports after the template expands, or add an import placeholder and a Groovy expression to generate the needed imports.
Verifying the result
- Open a Java class and type logd + Tab. Confirm the line appears with the correct class name.
- Check that the Logger and LoggerFactory classes are highlighted (meaning they are resolvable) or that IDEA offers to add the imports.
- Switch to an XML or HTML file and type logd + Tab. The abbreviation should not expand if the context is limited to Java.
If any of these checks fail, revisit the template’s context setting or the template text.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.