TestNG - Logger类的使用

TestNG提供了类似于Log4j的logging系统,为了控制logging,你需要增加一个名字为"log4testng.properties"的文件到你的classpath中国,TestNg的logging一共有六个级别: TRACE, DEBUG, INFO, WARN, ERROR 和FATAL,logging框架有如下的特性:

1. 所有的logging都是用System.out(levels < ERROR) 和System.err来完成,没有办法改变输出源

2. 不可以通过动态编程的方式来控制logging(我理解为,只能通过log4testng.properties文件中的属性来控制)

3.在第一次call logging API的时候,会在classpath中查找log4testng.properties 资源文件,如果没有找到,logging设置默认级别为WARN


log4testng.properties文件包含的内容格式如下所示:

# log4testng will log its own behavior (generally used for debugging this package only). log4testng会输出它自身的行为(通常用于调试logging系统自生)
log4testng.debug=true

# Specifies the root Loggers logging level. Will log DEBUG level and above 指定root Loggers的logging lvel,下面的设置将输出>=DEBUG级别的信息
log4testng.rootLogger=DEBUG

# The org.testng.reporters.EmailableReporter Logger will log TRACE level and above  //org.testng.reporters.EmailableReporter Logger输出>=TRACE级别的信息
log4testng.logger.org.testng.reporters.EmailableReporter=TRACE

# All Logger in packages below org.testng will log WARN level and above //包中的所有Logger将输出>=WARN级别的信息
log4testng.logger.org.testng=WARN

接下来,我将结合自身实践和Logger的source code做出一些解读,首先来看一个例子:

package com.spring.test;

import org.testng.log4testng.Logger;

public class TestngLoggerDemo {
	public static Logger logger = Logger.getLogger(TestngLoggerDemo.class);
	
	public static void Test() {
		logger.debug("ssssssssssssss");
	}
	
	public static void main(String[] args) {
		
		System.out.println(logger.isDebugEnabled());
		logger.debug("ffffffffffffff");
		logger.trace("dddddddd");
		//logger.error("ddddd");
	}
}

package com.spring.test;

import org.testng.log4testng.Logger;

public class TestngLoggerDemo2 {
	public static void main(String[] args) {
		TestngLoggerDemo.logger.trace("dddddddddddd");
		
		Logger logger2 = Logger.getLogger(TestngLoggerDemo2.class);
		logger2.debug("vvvvvvvvvv");
		
	}
}
"log4testng.properties"文件内容如下所示:
log4testng.debug=true
 log4testng.rootLogger=DEBUG
 log4testng.logger.com.spring.TestngLoggerDemo=TRACE

1) 首先来看第一条 log4testng.debug=true

[log4testng] [debug] log4testng.debug set to true
[log4testng] [debug] Root level logger set to DEBUG level.
[log4testng] [debug] logger log4testng.logger.com.spring.TestngLoggerDemo set to 0 level.
[log4testng] [debug] Getting level for logger com.spring.test.TestngLoggerDemo
[log4testng] [debug] Found level 1 for root logger
true
[TestngLoggerDemo] [DEBUG] ffffffffffffff

[log4testng] [debug] tag开头的信息,都是因为这条语句的设置而输出的,它展示给用户当前的logging系统的信息,log4testng.debug设置为false,那么只有最后两条语句会被输出,查看Logger类的源代码就会得出原因,带有[log4testng][debug]tag的信息,都是

  /**
   * Logs the message to System.out of debug is on.
   * @param pmessage the message to log to the console
   */
  private static void loglog4testng(String pmessage) {
    if(debug) {
      out.println("[log4testng] [debug] " + pmessage);
    }
  }
log4testng(String pmesage)中的debug开关是在下面的函数中设定

  private static void checkProperties(Properties pProperties) {
    {
      // See if we want to debug log4testng
      String debugStr= pProperties.getProperty(DEBUG_PROPERTY);
      if(debugStr != null) {
        if(debugStr.equalsIgnoreCase("true")) {
          debug= true;
        }
        else if(debugStr.equalsIgnoreCase("false")) {
          ...
}

checkProperties函数在getLogger函数张被调用

2) 接下来来看语句 log4testng.rootLogger=DEBUG, 他设定logger的默认logging levei为DEBUG在getLogger的时候,如果properties文件中没有显示的为当前类设定logging level,那么当前类的logging level默认为DEBUG,这也验证了文章一开头所说的框架属性的第三条。查看下面的函数就可以从代码中找到佐证

private static void checkProperties(Properties pProperties) {
    {
  
    {
      // Set the value of the root logger (if any).
      String rootLevelStr= pProperties.getProperty(ROOT_LOGGER);
      if(rootLevelStr != null) {
        Integer ilevel= levelMap.get(rootLevelStr.toUpperCase());
        if(ilevel == null) {
          throw new IllegalArgumentException("Unknown level for log4testng.rootLogger "
                                             + rootLevelStr + " in log4testng.properties");
        }
        rootLoggerLevel= ilevel.intValue();
        loglog4testng("Root level logger set to " + rootLevelStr + " level.");
      }
    }

  }


你可能感兴趣的:(TestNG - Logger类的使用)