LoggerFactory.getLogger(ClassName.class) vs LoggerFactory.getLogger(this.getClass().getName())

前端 未结 7 746
小蘑菇
小蘑菇 2021-02-04 00:48

I\'m trying to improve my optimization skills in Java. In order to achieve that, I\'ve got an old program I made and I\'m trying my best to make it better. In this program I\'m

相关标签:
7条回答
  • 2021-02-04 01:33

    If you don't want to write the class name every time you declare a logger, you can use the following utility method:

        public static org.slf4j.Logger getLogger() {
            final Throwable t = new Throwable();
            t.fillInStackTrace();
            return LoggerFactory.getLogger(t.getStackTrace()[1].getClassName());
        }
    

    The method can be used tis way:

    private static final Logger LOG = TheClassContainingTheMethod.getLogger();
    

    With such an approach, the logger declaration is always the same for all the classes.

    0 讨论(0)
提交回复
热议问题