log4j2 日志未记录在日志文件夹下的自动生成的日志文件中
log4j2 logs are not getting logged in auto generated log file under logs folder
这里我试图将执行期间生成的日志保存在日志文件中我使用 lo4j2.properties 和 Testng 侦听器来保存我的日志,我的 log4j2.properties 如下
log4j2.properties
name=PropertiesConfig
property.filename = logs
appenders = console, file
appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
appender.file.type = File
appender.file.name = LOGFILE
appender.file.fileName=${filename}/automationLogs.log
appender.file.layout.type=PatternLayout
appender.file.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
appender.file.append=true
loggers=file
logger.file.name=Demo
logger.file.level = debug
logger.file.appenderRefs = file
logger.file.appenderRef.file.ref = LOGFILE
rootLogger.level = debug
rootLogger.appenderRefs = stdout
rootLogger.appenderRef.stdout.ref = STDOUT
TestListeners.java file 文件有以下代码
TestListeners.java
package Listners;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;
import RecallAgreementPojo.RecallAgreementPayload;
import io.restassured.response.Response;
import utils.ConvertJSON;
public class TestListeners implements ITestListener{
private static Logger logger = LogManager.getLogger(TestListeners.class);
Response response = null;
public void onTestStart(ITestResult result) {
logger.info("********* Test started **********" + result.getName());
}
public void onTestSuccess(ITestResult result) {
logger.info("********* Test Success **********");
}
public void onTestFailure(ITestResult result) {
ITestContext context = result.getTestContext();
response = (Response) context.getAttribute("Response");
if(response != null)
logger.error("********* Test Failed **********" + response.prettyPrint());
}
public void onTestSkipped(ITestResult result) {
logger.warn("********* Test Skipped **********" + result.getName());
}
public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
// logger.warn("********* Test passed partially with **********" + result.SUCCESS_PERCENTAGE_FAILURE);
}
public void onStart(ITestContext context) {
logger.info("=========== onStart :-" + context.getName() + "===============");
}
public void onFinish(ITestContext context) {
logger.info("********* Test Completed **********" + context.getName());
Map<String,List> newAgreementDetails = (Map<String, List>) context.getAttribute("AgreementDetails");
Map<String,RecallAgreementPayload> newAgreementsMap = (Map<String, RecallAgreementPayload>) context.getAttribute("AgreementRequests");
for(Entry<String, List> entry : newAgreementDetails.entrySet()) {
logger.info(entry.getKey() + " : " + entry.getValue());
}
for(Entry<String, RecallAgreementPayload> entry : newAgreementsMap.entrySet()) {
logger.info(entry.getKey() + " : " );
ConvertJSON.JSONConverter(entry.getValue());
}
}
}
但是我得到的是空文件 automationLogs.log
log4j2.properties 中的记录器名为 Demo
。
logger.file.name=Demo <<-- logger name
logger.file.appenderRef.file.ref = LOGFILE <<-- reference to the appender that will write to file
为了写入文件,您需要提供 LogManager 记录器的名称 (Demo
) 以便 LogManager 使用它。 LogManager.getLogger("<logger name>")
.
附加程序 LOGFILE
,记录器 Demo
正在引用,然后会将日志写入文件 automationLogs.log
.
换句话说,改变这个:
private static Logger logger = LogManager.getLogger(TestListeners.class);
对此:
private static Logger logger = LogManager.getLogger("Demo");
如果这不是您期望的行为,并且确实对将 TestListeners.class 传递给 LogManager 更感兴趣,那么使用 RollingFile 可能更适合您的需要。
虽然我没有在您的配置上测试以下内容,但我的想法是这样的:
appender.rolling.type = RollingFile
appender.rolling.name = fileLogger
appender.rolling.fileName= ${filename}/automationLogs.log
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern =[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
appender.rolling.policies.type = Policies
并使用根记录器引用它:
rootLogger.appenderRef.rolling.ref = fileLogger
希望这对您有所帮助:)
这里我试图将执行期间生成的日志保存在日志文件中我使用 lo4j2.properties 和 Testng 侦听器来保存我的日志,我的 log4j2.properties 如下
log4j2.properties
name=PropertiesConfig
property.filename = logs
appenders = console, file
appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
appender.file.type = File
appender.file.name = LOGFILE
appender.file.fileName=${filename}/automationLogs.log
appender.file.layout.type=PatternLayout
appender.file.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
appender.file.append=true
loggers=file
logger.file.name=Demo
logger.file.level = debug
logger.file.appenderRefs = file
logger.file.appenderRef.file.ref = LOGFILE
rootLogger.level = debug
rootLogger.appenderRefs = stdout
rootLogger.appenderRef.stdout.ref = STDOUT
TestListeners.java file 文件有以下代码
TestListeners.java
package Listners;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;
import RecallAgreementPojo.RecallAgreementPayload;
import io.restassured.response.Response;
import utils.ConvertJSON;
public class TestListeners implements ITestListener{
private static Logger logger = LogManager.getLogger(TestListeners.class);
Response response = null;
public void onTestStart(ITestResult result) {
logger.info("********* Test started **********" + result.getName());
}
public void onTestSuccess(ITestResult result) {
logger.info("********* Test Success **********");
}
public void onTestFailure(ITestResult result) {
ITestContext context = result.getTestContext();
response = (Response) context.getAttribute("Response");
if(response != null)
logger.error("********* Test Failed **********" + response.prettyPrint());
}
public void onTestSkipped(ITestResult result) {
logger.warn("********* Test Skipped **********" + result.getName());
}
public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
// logger.warn("********* Test passed partially with **********" + result.SUCCESS_PERCENTAGE_FAILURE);
}
public void onStart(ITestContext context) {
logger.info("=========== onStart :-" + context.getName() + "===============");
}
public void onFinish(ITestContext context) {
logger.info("********* Test Completed **********" + context.getName());
Map<String,List> newAgreementDetails = (Map<String, List>) context.getAttribute("AgreementDetails");
Map<String,RecallAgreementPayload> newAgreementsMap = (Map<String, RecallAgreementPayload>) context.getAttribute("AgreementRequests");
for(Entry<String, List> entry : newAgreementDetails.entrySet()) {
logger.info(entry.getKey() + " : " + entry.getValue());
}
for(Entry<String, RecallAgreementPayload> entry : newAgreementsMap.entrySet()) {
logger.info(entry.getKey() + " : " );
ConvertJSON.JSONConverter(entry.getValue());
}
}
}
但是我得到的是空文件 automationLogs.log
log4j2.properties 中的记录器名为 Demo
。
logger.file.name=Demo <<-- logger name
logger.file.appenderRef.file.ref = LOGFILE <<-- reference to the appender that will write to file
为了写入文件,您需要提供 LogManager 记录器的名称 (Demo
) 以便 LogManager 使用它。 LogManager.getLogger("<logger name>")
.
附加程序 LOGFILE
,记录器 Demo
正在引用,然后会将日志写入文件 automationLogs.log
.
换句话说,改变这个:
private static Logger logger = LogManager.getLogger(TestListeners.class);
对此:
private static Logger logger = LogManager.getLogger("Demo");
如果这不是您期望的行为,并且确实对将 TestListeners.class 传递给 LogManager 更感兴趣,那么使用 RollingFile 可能更适合您的需要。
虽然我没有在您的配置上测试以下内容,但我的想法是这样的:
appender.rolling.type = RollingFile
appender.rolling.name = fileLogger
appender.rolling.fileName= ${filename}/automationLogs.log
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern =[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
appender.rolling.policies.type = Policies
并使用根记录器引用它:
rootLogger.appenderRef.rolling.ref = fileLogger
希望这对您有所帮助:)