无法通过 Gradle 使用 log4j 将日志写入日志文件
unable to write the logs to log file using log4j through Gradle
我正在从事 Cucumber TestNg Gradle 自动化项目。由于我是 Gradle 的新手,虽然我在 build.gradle 文件中添加了适当的 log4j 依赖项,但无法解决问题。有人可以调查这个问题并提供解决方案或告诉我由于哪些日志未写入名为“ApplicationLogs.log”
的日志文件而导致的错误
下面是我的项目结构
下面是我的Log4j.properties文件内容
根记录器选项
log4j.rootLogger=INFO, RFILE,STDOUT
# Console Appender
log4j.appender.name=STDOUT
log4j.appender.STDOUT=org.apache.log4j.ConsoleAppender
log4j.appender.STDOUT.Target=System.out
log4j.appender.STDOUT.layout=org.apache.log4j.PatternLayout
log4j.appender.STDOUT.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%-5p] [%c{1}] - [%M] %m%n
# Rolling File Appender
log4j.appender.name=RFILE
log4j.appender.RFILE=org.apache.log4j.RollingFileAppender
log4j.appender.RFILE.File=./Automation_Report/logs/ApplicationLogs.log
log4j.appender.RFILE.threshold=INFO
log4j.appender.RFILE.maxFileSize=25MB
log4j.appender.RFILE.maxBackupIndex=100
log4j.appender.RFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.RFILE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%-5p] [%c{1}] - [%M] %m%n
下面是我的build.gradle文件内容
plugins {
id 'java'
id 'maven-publish'
id 'java-library'
id 'java-gradle-plugin'
id 'application'
}
apply plugin: 'java'
repositories {
mavenCentral()
jcenter()
}
task deleteGraphicsAssets(type: Delete) {
delete "build"
}
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
dependencies {
// This dependency is exported to consumers, that is to say found on their compile classpath.
api 'org.apache.commons:commons-math3:3.6.1'
// This dependency is used internally, and not exposed to consumers on their own compile classpath.
implementation 'com.google.guava:guava:28.2-jre'
implementation gradleApi()
//Cucumber
testImplementation 'io.cucumber:cucumber-java:4.8.0'
//compile group: 'io.cucumber', name: 'cucumber-jvm', version: '4.8.0', ext: 'pom'
testImplementation 'io.cucumber:cucumber-jvm:4.8.0'
compile group: 'io.cucumber', name: 'cucumber-core', version: '4.8.0'
// testImplementation 'io.cucumber:cucumber-core:4.8.0'
compile group: 'io.cucumber', name: 'cucumber-testng', version: '4.8.0'
//testImplementation 'io.cucumber:cucumber-testng:4.8.0'
implementation 'io.cucumber:cucumber-picocontainer:4.7.4'
testImplementation 'io.cucumber:cucumber-gherkin:5.1.0'
//TestNg
// testImplementation 'org.testng:testng:6.14.3'
testCompile group: 'org.testng', name: 'testng', version: '6.14.3'
//Reports
testImplementation 'com.aventstack:extentreports:4.1.7'
testImplementation 'com.aventstack:extentreports-cucumber4-adapter:1.0.12'
//Maven Compiler , Surefire Plugin ,Poi
testImplementation 'org.apache.maven.plugins:maven-surefire-plugin:2.22.2'
testImplementation 'org.apache.maven.plugins:maven-compiler-plugin:3.8.1'
testImplementation 'org.apache.poi:poi-ooxml:4.1.2'
//Log4j & SLF4J
compile group: 'log4j', name: 'log4j', version: '1.2.17'
// #Rest Assured Apis
testImplementation 'io.rest-assured:rest-assured:3.3.0'
testImplementation 'org.json:json:20180813'
testImplementation 'com.googlecode.json-simple:json-simple:1.1.1'
//#Selenium Apis
compile group: 'org.seleniumhq.selenium', name: 'selenium-server', version: '3.141.59'
implementation 'com.paulhammant:ngwebdriver:1.1.4'
compile group: 'org.seleniumhq.selenium', name: 'selenium-htmlunit-driver', version: '2.52.0'
implementation 'org.seleniumhq.selenium:selenium-java:4.0.0-alpha-4'
//testImplementation 'org.hamcrest:hamcrest-all:2.2'
}
test {
testLogging.showStandardStreams = true
systemProperties System.getProperties()
}
configurations {
cucumberRuntime.extendsFrom testImplementation
}
task cucumber() {
dependsOn assemble,testClasses
doLast {
javaexec {
main = "io.cucumber.core.cli.Main"
classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
args = ['--plugin', 'pretty',
'--plugin', 'json:target/AppleBrands.json',
'--plugin', 'com.aventstack.extentreports.cucumber.adapter.ExtentCucumberAdapter:Report',
'--glue','com.apple.brands.test.stepdefinition',
'src/test/resources','src/main/java',
'--tags', '@Api'
]
}
}
}
tasks.test {
finalizedBy cucumber
}
我发现了问题。
问题是您没有使用 LoggerHelper。
这是我的项目结构:
更新了 LoggerHelper class :
测试LoggerHelper 是否正常工作。我用主要方法添加了一个测试 class :
控制台:
日志生成文件。
After doing lot of analysis and comparing i found the solution to my question. The solution is very simple only mistake that i need to remove the unwanted plugins used due to which it created all of issues.
When I removed the application plugin as shown below in plugin section it started working properly and even logs started working.
plugins {
id 'java'
id 'maven-publish'
id 'java-library'
id 'java-gradle-plugin'
}
我正在从事 Cucumber TestNg Gradle 自动化项目。由于我是 Gradle 的新手,虽然我在 build.gradle 文件中添加了适当的 log4j 依赖项,但无法解决问题。有人可以调查这个问题并提供解决方案或告诉我由于哪些日志未写入名为“ApplicationLogs.log”
的日志文件而导致的错误下面是我的项目结构
下面是我的Log4j.properties文件内容
根记录器选项
log4j.rootLogger=INFO, RFILE,STDOUT
# Console Appender
log4j.appender.name=STDOUT
log4j.appender.STDOUT=org.apache.log4j.ConsoleAppender
log4j.appender.STDOUT.Target=System.out
log4j.appender.STDOUT.layout=org.apache.log4j.PatternLayout
log4j.appender.STDOUT.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%-5p] [%c{1}] - [%M] %m%n
# Rolling File Appender
log4j.appender.name=RFILE
log4j.appender.RFILE=org.apache.log4j.RollingFileAppender
log4j.appender.RFILE.File=./Automation_Report/logs/ApplicationLogs.log
log4j.appender.RFILE.threshold=INFO
log4j.appender.RFILE.maxFileSize=25MB
log4j.appender.RFILE.maxBackupIndex=100
log4j.appender.RFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.RFILE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%-5p] [%c{1}] - [%M] %m%n
下面是我的build.gradle文件内容
plugins {
id 'java'
id 'maven-publish'
id 'java-library'
id 'java-gradle-plugin'
id 'application'
}
apply plugin: 'java'
repositories {
mavenCentral()
jcenter()
}
task deleteGraphicsAssets(type: Delete) {
delete "build"
}
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
dependencies {
// This dependency is exported to consumers, that is to say found on their compile classpath.
api 'org.apache.commons:commons-math3:3.6.1'
// This dependency is used internally, and not exposed to consumers on their own compile classpath.
implementation 'com.google.guava:guava:28.2-jre'
implementation gradleApi()
//Cucumber
testImplementation 'io.cucumber:cucumber-java:4.8.0'
//compile group: 'io.cucumber', name: 'cucumber-jvm', version: '4.8.0', ext: 'pom'
testImplementation 'io.cucumber:cucumber-jvm:4.8.0'
compile group: 'io.cucumber', name: 'cucumber-core', version: '4.8.0'
// testImplementation 'io.cucumber:cucumber-core:4.8.0'
compile group: 'io.cucumber', name: 'cucumber-testng', version: '4.8.0'
//testImplementation 'io.cucumber:cucumber-testng:4.8.0'
implementation 'io.cucumber:cucumber-picocontainer:4.7.4'
testImplementation 'io.cucumber:cucumber-gherkin:5.1.0'
//TestNg
// testImplementation 'org.testng:testng:6.14.3'
testCompile group: 'org.testng', name: 'testng', version: '6.14.3'
//Reports
testImplementation 'com.aventstack:extentreports:4.1.7'
testImplementation 'com.aventstack:extentreports-cucumber4-adapter:1.0.12'
//Maven Compiler , Surefire Plugin ,Poi
testImplementation 'org.apache.maven.plugins:maven-surefire-plugin:2.22.2'
testImplementation 'org.apache.maven.plugins:maven-compiler-plugin:3.8.1'
testImplementation 'org.apache.poi:poi-ooxml:4.1.2'
//Log4j & SLF4J
compile group: 'log4j', name: 'log4j', version: '1.2.17'
// #Rest Assured Apis
testImplementation 'io.rest-assured:rest-assured:3.3.0'
testImplementation 'org.json:json:20180813'
testImplementation 'com.googlecode.json-simple:json-simple:1.1.1'
//#Selenium Apis
compile group: 'org.seleniumhq.selenium', name: 'selenium-server', version: '3.141.59'
implementation 'com.paulhammant:ngwebdriver:1.1.4'
compile group: 'org.seleniumhq.selenium', name: 'selenium-htmlunit-driver', version: '2.52.0'
implementation 'org.seleniumhq.selenium:selenium-java:4.0.0-alpha-4'
//testImplementation 'org.hamcrest:hamcrest-all:2.2'
}
test {
testLogging.showStandardStreams = true
systemProperties System.getProperties()
}
configurations {
cucumberRuntime.extendsFrom testImplementation
}
task cucumber() {
dependsOn assemble,testClasses
doLast {
javaexec {
main = "io.cucumber.core.cli.Main"
classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
args = ['--plugin', 'pretty',
'--plugin', 'json:target/AppleBrands.json',
'--plugin', 'com.aventstack.extentreports.cucumber.adapter.ExtentCucumberAdapter:Report',
'--glue','com.apple.brands.test.stepdefinition',
'src/test/resources','src/main/java',
'--tags', '@Api'
]
}
}
}
tasks.test {
finalizedBy cucumber
}
我发现了问题。
问题是您没有使用 LoggerHelper。
这是我的项目结构:
更新了 LoggerHelper class :
测试LoggerHelper 是否正常工作。我用主要方法添加了一个测试 class :
控制台:
日志生成文件。
After doing lot of analysis and comparing i found the solution to my question. The solution is very simple only mistake that i need to remove the unwanted plugins used due to which it created all of issues.
When I removed the application plugin as shown below in plugin section it started working properly and even logs started working.
plugins {
id 'java'
id 'maven-publish'
id 'java-library'
id 'java-gradle-plugin'
}