20

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.

Dims
  • 47,675
  • 117
  • 331
  • 600
  • possible duplicate of http://stackoverflow.com/questions/6097646/intellij-live-template – cahen Aug 17 '15 at 20:11

5 Answers5

27

Using Intellij Live Template:

Settings -> Editor -> Live Templates

Setting like bellow:

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

enter image description here

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

huytmb
  • 3,647
  • 3
  • 12
  • 18
  • As I was landing here because i searched that for Kotlin: Note that you need to use kotlinClassName() with Kotlin – Alex Jul 10 '22 at 10:51
22

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.

Wim Deblauwe
  • 25,113
  • 20
  • 133
  • 211
4

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);

Dmitry Zagorulkin
  • 8,370
  • 4
  • 37
  • 60
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.

mzc
  • 3,265
  • 1
  • 20
  • 25
0

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

See this: Intellij Live Template

Community
  • 1
  • 1
OscarRyz
  • 196,001
  • 113
  • 385
  • 569