认识log4j2【一】:http://note.youdao.com/noteshare?id=e50776166acf36b9f65adf91a4f4b9f7
log4j2了解调用和配置【二】: http://note.youdao.com/noteshare?id=0a3816a150ed7c62b9fcb6d216bc3ecd
log4j2配置【三】实战:是我搭建的自动化测试框架下使用log4j2:
1,首先把配置文件放到项目下。
2配置:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/2002/xmlspec/dtd/2.10/xmlspec.dtd">
<!-- 设置log4j2的自身log级别为INFO -->
<!-- OFF > FATAL > ERROR > WARN > INFO > DEBUG > TRACE > ALL -->
<configuration status="WARN" monitorInterval="30">
<properties>
<property name="LOG_HOME">src/main/resources/log4j2/logs</property>
</properties>
<appenders>
<RollingFile name="RollingFile"
fileName="${LOG_HOME}/${date:yyyy-MM-dd}/test.log"
filePattern="log4j2/info/$${date:yyyy-MM}/%d{MM-dd-yyyy}-%i.log">
<PatternLayout
pattern="[ %d{yyyy-MM-dd HH:mm:ss} ] %msg%n" />
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="50 MB" />
</Policies>
<DefaultRolloverStrategy max="100" />
</RollingFile>
</appenders>
<Loggers>
<Root level="INFO">
<AppenderRef ref="RollingFile" />
</Root>
</Loggers>
</configuration>
3调用:
首先在管理常量的类里:
public static final String Log4jFile_Path = rootPath + "\\resources\\log4j2.xml";
然后在Log类里面:
package autotest;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class Log {
private static Logger Log = LogManager.getLogger(Log.class.getName());
public static void testStart(String testCaseName) {
Log.info("【-Info-】: ==开始执行测试案例 :【" + testCaseName + "】");
}
public static void testEnd(String testCaseName) {
Log.info("【-Info-】: ==结束执行测试案例 :【" + testCaseName + "】");
}
public static void info(String message) {
Log.info("【-Info-】: "+message);
}
public static void error(String message) {
Log.error("【-Erorr-】: "+message);
}
public static void debug(String message) {
Log.debug("【-Debug-】: "+message);
}
public static void warn(String message) {
Log.warn("【-Warning-】: "+message);
}
}
4 使用:
生成的log文件:
网友评论