Java logger that automatically determines caller's class name

后端 未结 21 855
礼貌的吻别
礼貌的吻别 2020-12-07 17:53
public static Logger getLogger() {
    final Throwable t = new Throwable();
    final StackTraceElement methodCaller = t.getStackTrace()[1];
    final Logger logger          


        
21条回答
  •  半阙折子戏
    2020-12-07 18:22

    Take a look at Logger class from jcabi-log. It does exactly what you're looking for, providing a collection of static methods. You don't need to embed loggers into classes any more:

    import com.jcabi.log.Logger;
    class Foo {
      public void bar() {
        Logger.info(this, "doing something...");
      }
    }
    

    Logger sends all logs to SLF4J, which you can redirect to any other logging facility, in runtime.

提交回复
热议问题