How to change velocity template in Intellij for catch statement

334 Views Asked by At

I want to print in catch block directly with logger like:

Logger.getLogger(Example.class.getName()).log(Level.SEVERE, null, ex);

I checked Velocity template in Settings - File and Code Templates for Catch Statement body

By Default it is given with

${EXCEPTION}.printStackTrace()

Tried changing with:

Logger.getLogger(${NAME}.class.getName()).log(LEVEL.INFO,${EXCEPTION}.printStackTrace(),${EXCEPTION});

but the Logger statement is getting wrong while, NAME is not picking class Name as provided in guidelines: https://www.jetbrains.com/help/idea/file-template-variables.html

Want to automatically put logger statement in catch block whenever I am using try-catch block or try with resources block to catch such that it has like:

Logger.getLogger(Example.class.getName()).log(Level.SEVERE, null, ex);
1

There are 1 best solutions below

0
On

It is not possible to use ${NAME} variable in Code Templates yet. You may try to use the following construction to get the class name:

Logger.getLogger(this.getClass().getName()).log(LEVEL.INFO,${EXCEPTION}.printStackTrace(),${EXCEPTION});

There is a similar request on JetBrains issues tracker: IDEA-55300