Logger4j2-框架
log4j由三个组件组成:
Logger:这是一个类,它帮助您记录不同日志级别的信息 Root level展示级别 AppenderRef=“展示位置”
Appenders:Appenders是对象,它帮助Logger对象将日志写入不同的输出目的地。Appenders可以指定一个文件、控制台或数据库作为输出位置。
Layouts:Layout类帮助我们定义如何在输出终端显示日志:
配置文件-Console Logging:log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration xmlns="http://logging.apache.org/log4j/2.0/config">
<Appenders>
<Console name = "Console" target="">
<!-- 指定一个文件、控制台或数据库作为输出位置。-->
<PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %c{2} -%msg%n"></PatternLayout>
<!-- 定义如何在输出终端显示日志:时间 级别 包名.类名(层级) 响应信息-->
</Console>
</Appenders>
<Loggers>
<Root level="info">
<!-- 展示级别 它帮助您记录不同日志级别的信息-->
<AppenderRef ref="Console"></AppenderRef>
</Root>
</Loggers>
</Configuration>
LoggingConsoleDemo:
package utility;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/**
* @author 96510
* @version 1.0
* @date 2021/7/15
*/
public class LoggingConsole {
private static final Logger Log = LogManager.getLogger(LoggingConsole.class.getName());
public static void main(String[] args) {
Log.debug("debug");
Log.error("error");
Log.fatal("fatal");
//默认记录error及以上等级的日志
}
}
Policies策略
timeBasedTriggeringPolicy 时间策略interval指定封存间隔 modulate说明是否对封存时间进行调制 若modulate=true ,则封存时间将以0点为边界进行偏移计算
SizeBasedTriggeringPolicy 大小策略 size设置大小
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Properties>
<Property name="basePath">./logs</Property>
</Properties>
<Appenders>
<RollingFile name="File" fileName="${basePath}/app.log" filePattern="${basePath}/app-%d{yyyy-MM-dd}.log">
<PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %c{2} -%msg%n"/>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
<SizeBasedTriggeringPolicy size="100 KB"/>
</Policies>
</RollingFile>
<Console name="Console">
<PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %c{2} -%msg%n"/>
</Console>
</Appenders>
<Loggers>
<Logger name="log4jerror" level="error" additivity="false">
<AppenderRef ref="File" /> //不同的包设置不同的级别 name包名 level级别 additivity的作用在于 children-logger是否使用 rootLogger的配置 false不使用
</Logger>
<Logger name="log4jinfo" level="info" additivity="false">
<AppenderRef ref="File" />
</Logger>
<Root level="trace">
<AppenderRef ref="File" />
</Root>
</Loggers>
</Configuration>
package log4jerror;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/**
* @author 96510
* @version 1.0
* @date 2021/7/15
*/
public class LoggingFileDemo {
private static final Logger Log = LogManager.getLogger(LoggingFileDemo.class.getName());
public static void main(String[] args) {
Log.debug("debug");
Log.error("error");
Log.fatal("fatal");
//默认记录error及以上等级的日志
}
}
package log4jinfo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/**
* @author 96510
* @version 1.0
* @date 2021/7/15
*/
public class LoggingFileDemo {
private static final Logger Log = LogManager.getLogger(LoggingFileDemo.class.getName());
public static void main(String[] args) {
Log.debug("debug");
Log.error("error");
Log.fatal("fatal");
Log.info("info");
//默认记录error及以上等级的日志
}
}
10:14:32.818 DEBUG LoggingFileDemo -debug
10:14:32.820 ERROR LoggingFileDemo -error
10:14:32.820 FATAL LoggingFileDemo -fatal
10:30:45.559 ERROR LoggingFileDemo -error
10:30:45.561 FATAL LoggingFileDemo -fatal
10:32:09.848 ERROR LoggingFileDemo -error
10:32:09.849 FATAL LoggingFileDemo -fatal
10:32:14.305 ERROR LoggingFileDemo -error
10:32:14.307 FATAL LoggingFileDemo -fatal
10:32:54.112 ERROR log4jerror.LoggingFileDemo -error
10:32:54.114 FATAL log4jerror.LoggingFileDemo -fatal
10:32:56.586 ERROR log4jinfo.LoggingFileDemo -error
10:32:56.588 FATAL log4jinfo.LoggingFileDemo -fatal
10:35:17.049 ERROR log4jerror.LoggingFileDemo -error
10:35:17.051 FATAL log4jerror.LoggingFileDemo -fatal
10:35:50.108 ERROR log4jinfo.LoggingFileDemo -error
10:35:50.113 FATAL log4jinfo.LoggingFileDemo -fatal
10:35:50.113 INFO log4jinfo.LoggingFileDemo -info
在testNG中使用:
package TestNG;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
/**
* @author 96510
* @version 1.0
* @date 2021/7/16
*/
public class TestNG_TestCase {
private WebDriver driver;
private String baseurl;
private static final Logger log = LogManager.getLogger(TestNG_TestCase.class.getName());
@BeforeClass
public void beforeClass() {
driver = new ChromeDriver();
baseurl = "https://www.baidu.com/";
driver.manage().window().maximize();
log.info("第一步:数据初始化,打来浏览器");
}
@Test
public void test() {
driver.get(baseurl);
log.info("第二步:打开百度");
}
@AfterClass
public void afterClas() {
driver.quit();
log.info("关闭浏览器");
}
}