How to enter logger definition in IntelliJ fast?

不羁的心 提交于 2020-12-05 07:03:05

问题


Are there some live template or something, to add logger definition into a class?

In Eclipse I had a template:

private static final Logger log = LoggerFactory.getLogger(${enclosing_type}.class);

Don't see something about this in Log Support plugin.


回答1:


I am using this live template:

private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger( $CLASSNAME$.class );

Go to "Edit variables" and set the CLASSNAME variable to be the expression className() so IntelliJ will automatically insert the class name.

Further, enable the checkbox 'Shorten FQ names' so IntelliJ will use import statements.




回答2:


you can add template by adding following code in:

Settings -> Editor -> File and Code Templates -> class

private static final Logger log = LoggerFactory.getLogger(${NAME}.class);




回答3:


Have you tried Lombok? It's not a live template, but it helps with a lot of boilerplate code (getters/setters, equals, hash codes, loggers...)

To create a logger, annotate your class with @Log/@Slf4j/@CommonsLog and Lombok will create a static field log for you to use.

It plays well with IntelliJ - IDE plugin will inject the bolierplate code in your class during compilation phase while keeping your sources lean.




回答4:


Using Intellij Live Template:

Settings -> Editor -> Live Templates

Setting like bellow:

private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger($CLASSNAME$.class);

To expand a code snippet, type "logger" and press Tab




回答5:


Not by default but you can create a new one with the live template:

See this: Intellij Live Template



来源:https://stackoverflow.com/questions/32058864/how-to-enter-logger-definition-in-intellij-fast

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!