问题
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