Logger.getLogger(LogTestAction.class)为什么一定要加载一个类

不是一定要加载一个类,但是加载一个类也是有好处的。如果类有了包声明后,在log4j的配置文件中,可以声明属于某个包下的类用什么方式来显示日志,或只显示某个包下的类的日志。

 

这个方法的源码是:

 

  /**
   * Retrieve a logger named according to the value of the
   * <code>name</code> parameter. If the named logger already exists,
   * then the existing instance will be returned. Otherwise, a new
   * instance is created.  
   *
   * <p>By default, loggers do not have a set level but inherit it
   * from their neareast ancestor with a set level. This is one of the
   * central features of log4j.
   *
   * @param name The name of the logger to retrieve.  
  */
  static
  public
  Logger getLogger(String name) {
    return LogManager.getLogger(name);
  }

  /**
   * Shorthand for <code>getLogger(clazz.getName())</code>.
   *
   * @param clazz The name of <code>clazz</code> will be used as the
   * name of the logger to retrieve.  See {@link #getLogger(String)}
   * for more detailed information.
   */
  static
  public
  Logger getLogger(Class<?> clazz) {
    return LogManager.getLogger(clazz.getName());
  }

  

 

你可能感兴趣的:(log4j)