欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-07-06 09:42:08
...

不是一定要加载一个类,但是加载一个类也是有好处的。如果类有了包声明后,在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