当前位置: 首页 > 工具软件 > Log4cl > 使用案例 >

log4j中Logger.getLogger为什么一定要加载一个类

通正平
2023-12-01

  /**
   * 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());
  }

源码可以看出  ---不管是传的  
Class<?> clazz 还是String  最后给getLogger的都是String  

也就是说 
private static Logger logger = LogManager.getLogger(TestTidebuy.class.getName());
private static Logger logger = LogManager.getLogger(TestTidebuy.class);
是一样的

配合log4j.properties中  %d{HH:mm:ss.SSS} %-5level %c{1} %L %M - %msg%xEx%n  的<pre name="code" class="java"> %c{1}
private static Logger logger = LogManager.getLogger(Test.class);   这个对应的输出
23:13:15.921 INFO  Test 11 main - 开始程序.
23:13:15.921 ERROR Test 15 main - hello
private static Logger logger = LogManager.getLogger(HelloLog4j.class);    这个对应的输出
23:13:39.593 INFO  HelloLog4j 11 main - 开始程序.
23:13:39.593 ERROR HelloLog4j 15 main - hello

 

测试代码如下:
<pre name="code" class="html"><?xml version="1.0" encoding="UTF-8"?>

<configuration status="error">
    <!--先定义所有的appender-->
    <appenders>
        <!--这个输出控制台的配置-->
        <Console name="Console" target="SYSTEM_OUT">
            <!--控制台只输出level及以上级别的信息(onMatch),其他的直接拒绝(onMismatch)-->
            <ThresholdFilter level="trace" onMatch="ACCEPT" onMismatch="DENY"/>
            <!--这个都知道是输出日志的格式-->
            <PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %c{1} %L %M - %msg%xEx%n"/>
        </Console>
        <!--文件会打印出所有信息,这个log每次运行程序会自动清空,由append属性决定,这个也挺有用的,适合临时测试用-->
        <File name="log" fileName="log/test.log" append="false">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/>
        </File>

        <!--这个会打印出所有的信息,每次大小超过size,则这size大小的日志会自动存入按年份-月份建立的文件夹下面并进行压缩,作为存档-->
        <RollingFile name="RollingFile" fileName="logs/app.log"
                     filePattern="log/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
            <PatternLayout pattern="%d{yyyy-MM-dd 'at' HH:mm:ss z} %-5level %class{36} %L %M - %msg%xEx%n"/>
            <SizeBasedTriggeringPolicy size="50MB"/>
        </RollingFile>
    </appenders>
    <!--然后定义logger,只有定义了logger并引入的appender,appender才会生效-->
    <loggers>
        <!--建立一个默认的root的logger-->
        <root level="trace">
            <appender-ref ref="RollingFile"/>
            <appender-ref ref="Console"/>
        </root>

    </loggers>
</configuration>

package log4j;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class Test {
	private static Logger logger = LogManager.getLogger(HelloLog4j.class);   
	
	public static void main(String[] args) {
		System.out.println(HelloLog4j.class.getName());
        logger.info("开始程序.");
//        HelloLog4j hello= new HelloLog4j();
       // for (int i = 0; i < 10000;i++){
//            if (!hello.hello()) {
                logger.error("hello");
//            }    // }
//        logger.trace("退出程序.");
    }
}

package log4j;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class Test {
	private static Logger logger = LogManager.getLogger(Test.class);   
	
	public static void main(String[] args) {
		System.out.println(HelloLog4j.class.getName());
        logger.info("开始程序.");
//        HelloLog4j hello= new HelloLog4j();
       // for (int i = 0; i < 10000;i++){
//            if (!hello.hello()) {
                logger.error("hello");
//            }    // }
//        logger.trace("退出程序.");
    }
}


 


 类似资料: