使用Log4在测试过程中打印执行日志 及配置log4j.properties!
http://zengxiantao.iteye.com/blog/1881706
1.环境配置:到网上下载log4j-1.2.17.jar包!完后 添加到 项目的build path 中即可!
2.在eclipse测试代码的根目录创建一个名为Log4j.xml 文件,
xml 的内容为:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false"> <appender name="fileAppender" class="org.apache.log4j.FileAppender"> <param name="Threshold" value="INFO"/> <param name="File" value="logfile.log"/> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d %-5p [%c{1}] %m %n"/> </layout> </appender> <root> <level value="INFO"/> <appender-ref ref="fileAppender"/> </root> </log4j:configuration>
log4j.properties
log4j.rootLogger=INFO, stdout, fileout
log4j.logger.TestProject=INFO
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-5p %d{yyyy-MM-dd HH:mm:ss} %l %c : %m%n
log4j.appender.fileout=org.apache.log4j.FileAppender
log4j.appender.fileout.File=c:/test.log
log4j.appender.fileout.layout=org.apache.log4j.PatternLayout
log4j.appender.fileout.layout.ConversionPattern=%-5p %d{yyyy-MM-dd HH:mm:ss} %l %c : %m%n
Log类
1 mport java.io.File; 2 import org.apache.log4j.Logger; 3 import org.apache.log4j.PropertyConfigurator; 4 5 public class Log { 6 7 private static Logger logger; 8 9 private static String filePath = "config/log4j.properties"; 10 11 private static boolean flag = false; 12 13 private static synchronized void getPropertyFile() { 14 logger = Logger.getLogger("TestProject"); 15 PropertyConfigurator.configure(new File(filePath).getAbsolutePath()); 16 flag = true; 17 } 18 19 private static void getFlag() { 20 if (flag == false) 21 Log.getPropertyFile(); 22 } 23 24 public static void logInfo(String message) { 25 Log.getFlag(); 26 logger.info(message); 27 } 28 29 public static void logError(String message) { 30 Log.getFlag(); 31 logger.error(message); 32 } 33 34 public static void logWarn(String message) { 35 Log.getFlag(); 36 logger.warn(message); 37 } 38 39 }
3.创建一个工具类 Log ;
package cn.gloryroad; import org.apache.log4j.Logger; public class Log { private static Logger Log=Logger.getLogger(Log.class.getName()); public static void startTextCase(String sTextCaseName){ Log.info("-----------------------------------------------------------------------------------------"); Log.info("***********************************"+sTextCaseName+"*************************************"); } public static void endTestCase(String sTextCaseName ){ System.out.println("****************测试用例结束******************************"); System.out.println("----------------------------------------------------------"); System.out.println(); } public static void info(String message){ Log.info(message); } //定义个warn方法 打印 warn级别的信息 ; public static void warn(String message){ Log.warn(message); } //定义个 error 方法 打印自定义的错误信息 ; public static void error(String message){ Log.error(message); } //fatal(重大的 致命的) 定义一个fatal 方法 ,打印重大级别的信息 ; public static void fatal(String message){ Log.fatal(message); } //定义一个debug方法打印自定义的 debug信息 ! public static void debug(String message){ Log.debug(message); } }
4.测试代码
package scr.comm.testComm; import org.apache.log4j.xml.DOMConfigurator; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import cn.gloryroad.Log; import scr.comm.OpenBrowserInfo; public class TestComm { public static void main(String[] args) { DOMConfigurator.configure("Log4j.xml"); String url="http://www.sogou.com/"; OpenBrowserInfo openinfo =new OpenBrowserInfo() ; openinfo.SystemSetProperty(); WebDriver dr =new FirefoxDriver(); Log.startTextCase("打开搜狗浏览器"); dr.navigate().to(url); Log.info("点击搜索按钮!"); dr.findElement(By.id("stb")).click(); Log.info("close current borwser!"); dr.quit(); Log.endTestCase("souguo"); } }
5.代码执行完毕后会在workspace下产生一个文件filelog.log