【问题标题】:How to reduce Logger.getLogger(...) boilerplate in every class如何减少每个类中的 Logger.getLogger(...) 样板
【发布时间】:2016-07-04 02:47:49
【问题描述】:

要在 Java 中使用记录器,现在我使用这样的代码:

Logger logger = Logger.getLogger("MyLog");
FileHandler fh;

try {

  // This block configure the logger with handler and formatter
  fh = new FileHandler("c:\\MyLogFile.log", true);
  logger.addHandler(fh);
  logger.setLevel(Level.ALL);
  SimpleFormatter formatter = new SimpleFormatter();
  fh.setFormatter(formatter);

  // the following statement is used to log any messages   
  logger.log(Level.WARNING,"My first log");

} catch (SecurityException e) {
  e.printStackTrace();
} catch (IOException e) {
  e.printStackTrace();
}

但是为每个类文件添加这个总是让我有些不安:

Logger l = Logger.getLogger("MyLog");

我怎样才能重构出这种重复?

【问题讨论】:

    标签: java logging refactoring code-duplication


    【解决方案1】:

    是否有更好的推荐日志记录方式,可以减少干扰/减少重复但提供相同甚至更好的功能?

    您可以将l 设置为类的private staticprivate 字段,但除此之外没有显着改进。

    (有一些不好的想法,例如 public static“全局”变量,或“通用”基类中的 private static 声明。但这些都是坏想法......不是更好的方法。)

    但是,嘿,声明本地记录器对象的一行代码几乎不是“侵入性”……是吗?

    【讨论】:

    • 不是每个人都这样做。见下文。
    【解决方案2】:

    登录每个类文件是正确的方法,但看起来有点冗余。

    日志记录的重要功能是记录异常。如果我们想直接找到异常发生在哪里,我们应该知道类名和方法名。

    但是你可能会在捕获异常时忘记记录。

    【讨论】:

      【解决方案3】:

      【讨论】:

      • 感谢您的想法。但我确实希望尽可能少地涉及库。我始终相信最简单的就是最好的。如果我能避免 log4j,我会尽量避免它....
      • 我不是建议你使用这个库。而是你看看它是如何完成的,看看你是否喜欢这种方法,也许可以复制它。
      【解决方案4】:

      您可以使用项目 lombok 来执行此操作:

      project lombok is here

      以下是他们网页中的示例,如果它不可用。需要明确的是,它仍然会在生成的字节码中生成语句,但它会停止 bolierplate,现在您将有一个较小的语句,但它用于课程,但我目前工作的项目团队喜欢它。

      import lombok.extern.java.Log;
      import lombok.extern.slf4j.Slf4j;
      
      @Log
      public class LogExample {
      
        public static void main(String... args) {
          log.error("Something's wrong here");
        }
      }
      
      @Slf4j
      public class LogExampleOther {
      
        public static void main(String... args) {
          log.error("Something else is wrong here");
        }
      }
      
      @CommonsLog(topic="CounterLog")
      public class LogExampleCategory {
      
        public static void main(String... args) {
          log.error("Calling the 'CounterLog' with a message");
        }
      }
      Vanilla Java
       public class LogExample {
        private static final java.util.logging.Logger log = java.util.logging.Logger.getLogger(LogExample.class.getName());
      
        public static void main(String... args) {
          log.error("Something's wrong here");
        }
      }
      
      public class LogExampleOther {
        private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LogExampleOther.class);
      
        public static void main(String... args) {
          log.error("Something else is wrong here");
        }
      }
      
      public class LogExampleCategory {
        private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog("CounterLog");
      
        public static void main(String... args) {
          log.error("Calling the 'CounterLog' with a message");
        }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-16
        • 1970-01-01
        • 2013-03-23
        • 1970-01-01
        • 2016-09-22
        • 1970-01-01
        相关资源
        最近更新 更多