如何在Intellij中更改catch语句的速度模板

huangapple 未分类评论49阅读模式
英文:

How to change velocity template in Intellij for catch statement

问题

我想在catch块中直接使用日志记录器打印,如下所示:

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

我检查了设置中的Velocity模板 - Catch语句主体的文件和代码模板

默认情况下,它是这样给出的

${EXCEPTION}.printStackTrace()

尝试更改为:

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

但是Logger语句出错了,而NAME没有按照指南中提供的类名。

想要在我使用try-catch块或尝试使用资源块捕获异常时自动放置日志记录器语句,以便像这样:

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

英文:

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

得分: 0

这时在“代码模板”中还无法使用 ${NAME} 变量。 您可以尝试使用以下结构来获取类名:

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

在JetBrains的问题跟踪器上有一个类似的请求:IDEA-55300

英文:

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

huangapple
  • 本文由 发表于 2020年4月3日 20:38:08
  • 转载请务必保留本文链接:https://java.coder-hub.com/61012088.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定