无法通过Gradle使用log4j将日志写入日志文件。

huangapple go评论119阅读模式
英文:

unable to write the logs to log file using log4j through Gradle

问题

  1. 我正在进行Cucumber TestNG Gradle自动化项目的工作。由于我对Gradle不熟悉,虽然我已经在build.gradle文件中添加了适当的log4j依赖项,但仍然无法解决问题。有人可以查看一下这个问题,并提供解决方案,或者指导我出了什么问题,导致日志没有写入名为"ApplicationLogs.log"的日志文件。
  2. **以下是我的项目结构**
  3. **以下是我的Log4j.properties文件内容**
  4. # 根日志记录器选项
  5. log4j.rootLogger=INFO, RFILE, STDOUT
  6. # 控制台Appender
  7. log4j.appender.name=STDOUT
  8. log4j.appender.STDOUT=org.apache.log4j.ConsoleAppender
  9. log4j.appender.STDOUT.Target=System.out
  10. log4j.appender.STDOUT.layout=org.apache.log4j.PatternLayout
  11. log4j.appender.STDOUT.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%-5p] [%c{1}] - [%M] %m%n
  12. # 滚动文件Appender
  13. log4j.appender.name=RFILE
  14. log4j.appender.RFILE=org.apache.log4j.RollingFileAppender
  15. log4j.appender.RFILE.File=./Automation_Report/logs/ApplicationLogs.log
  16. log4j.appender.RFILE.threshold=INFO
  17. log4j.appender.RFILE.maxFileSize=25MB
  18. log4j.appender.RFILE.maxBackupIndex=100
  19. log4j.appender.RFILE.layout=org.apache.log4j.PatternLayout
  20. log4j.appender.RFILE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%-5p] [%c{1}] - [%M] %m%n
  21. **以下是我的build.gradle文件内容**
  22. plugins {
  23. id 'java'
  24. id 'maven-publish'
  25. id 'java-library'
  26. id 'java-gradle-plugin'
  27. id 'application'
  28. }
  29. apply plugin: 'java'
  30. repositories {
  31. mavenCentral()
  32. jcenter()
  33. }
  34. task deleteGraphicsAssets(type: Delete) {
  35. delete "build"
  36. }
  37. version '1.0-SNAPSHOT'
  38. sourceCompatibility = 1.8
  39. dependencies {
  40. // ... (依赖项列表,已省略)
  41. }
  42. test {
  43. testLogging.showStandardStreams = true
  44. systemProperties System.getProperties()
  45. }
  46. configurations {
  47. cucumberRuntime.extendsFrom testImplementation
  48. }
  49. task cucumber() {
  50. // ... Cucumber任务设置,已省略)
  51. }
  52. tasks.test {
  53. finalizedBy cucumber
  54. }
英文:

I am working on Cucumber TestNg Gradle automation project. As I am new to Gradle and not able to figure the issue though I have put in appropriate log4j dependencies in build.gradle file. Can somebody look into this issue and provide the solutions or advise me on what went wrong due to which logs are not written to log file called "ApplicationLogs.log"

Below is my project structure

无法通过Gradle使用log4j将日志写入日志文件。

Below is my Log4j.properties file contents

Root logger option

  1. log4j.rootLogger=INFO, RFILE,STDOUT
  2. # Console Appender
  3. log4j.appender.name=STDOUT
  4. log4j.appender.STDOUT=org.apache.log4j.ConsoleAppender
  5. log4j.appender.STDOUT.Target=System.out
  6. log4j.appender.STDOUT.layout=org.apache.log4j.PatternLayout
  7. log4j.appender.STDOUT.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%-5p] [%c{1}] - [%M] %m%n
  8. # Rolling File Appender
  9. log4j.appender.name=RFILE
  10. log4j.appender.RFILE=org.apache.log4j.RollingFileAppender
  11. log4j.appender.RFILE.File=./Automation_Report/logs/ApplicationLogs.log
  12. log4j.appender.RFILE.threshold=INFO
  13. log4j.appender.RFILE.maxFileSize=25MB
  14. log4j.appender.RFILE.maxBackupIndex=100
  15. log4j.appender.RFILE.layout=org.apache.log4j.PatternLayout
  16. log4j.appender.RFILE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%-5p] [%c{1}] - [%M] %m%n

Below is my build.gradle File Contents

  1. plugins {
  2. id 'java'
  3. id 'maven-publish'
  4. id 'java-library'
  5. id 'java-gradle-plugin'
  6. id 'application'
  7. }
  8. apply plugin: 'java'
  9. repositories {
  10. mavenCentral()
  11. jcenter()
  12. }
  13. task deleteGraphicsAssets(type: Delete) {
  14. delete "build"
  15. }
  16. version '1.0-SNAPSHOT'
  17. sourceCompatibility = 1.8
  18. dependencies {
  19. // This dependency is exported to consumers, that is to say found on their compile classpath.
  20. api 'org.apache.commons:commons-math3:3.6.1'
  21. // This dependency is used internally, and not exposed to consumers on their own compile classpath.
  22. implementation 'com.google.guava:guava:28.2-jre'
  23. implementation gradleApi()
  24. //Cucumber
  25. testImplementation 'io.cucumber:cucumber-java:4.8.0'
  26. //compile group: 'io.cucumber', name: 'cucumber-jvm', version: '4.8.0', ext: 'pom'
  27. testImplementation 'io.cucumber:cucumber-jvm:4.8.0'
  28. compile group: 'io.cucumber', name: 'cucumber-core', version: '4.8.0'
  29. // testImplementation 'io.cucumber:cucumber-core:4.8.0'
  30. compile group: 'io.cucumber', name: 'cucumber-testng', version: '4.8.0'
  31. //testImplementation 'io.cucumber:cucumber-testng:4.8.0'
  32. implementation 'io.cucumber:cucumber-picocontainer:4.7.4'
  33. testImplementation 'io.cucumber:cucumber-gherkin:5.1.0'
  34. //TestNg
  35. // testImplementation 'org.testng:testng:6.14.3'
  36. testCompile group: 'org.testng', name: 'testng', version: '6.14.3'
  37. //Reports
  38. testImplementation 'com.aventstack:extentreports:4.1.7'
  39. testImplementation 'com.aventstack:extentreports-cucumber4-adapter:1.0.12'
  40. //Maven Compiler , Surefire Plugin ,Poi
  41. testImplementation 'org.apache.maven.plugins:maven-surefire-plugin:2.22.2'
  42. testImplementation 'org.apache.maven.plugins:maven-compiler-plugin:3.8.1'
  43. testImplementation 'org.apache.poi:poi-ooxml:4.1.2'
  44. //Log4j & SLF4J
  45. compile group: 'log4j', name: 'log4j', version: '1.2.17'
  46. // #Rest Assured Apis
  47. testImplementation 'io.rest-assured:rest-assured:3.3.0'
  48. testImplementation 'org.json:json:20180813'
  49. testImplementation 'com.googlecode.json-simple:json-simple:1.1.1'
  50. //#Selenium Apis
  51. compile group: 'org.seleniumhq.selenium', name: 'selenium-server', version: '3.141.59'
  52. implementation 'com.paulhammant:ngwebdriver:1.1.4'
  53. compile group: 'org.seleniumhq.selenium', name: 'selenium-htmlunit-driver', version: '2.52.0'
  54. implementation 'org.seleniumhq.selenium:selenium-java:4.0.0-alpha-4'
  55. //testImplementation 'org.hamcrest:hamcrest-all:2.2'
  56. }
  57. test {
  58. testLogging.showStandardStreams = true
  59. systemProperties System.getProperties()
  60. }
  61. configurations {
  62. cucumberRuntime.extendsFrom testImplementation
  63. }
  64. task cucumber() {
  65. dependsOn assemble,testClasses
  66. doLast {
  67. javaexec {
  68. main = "io.cucumber.core.cli.Main"
  69. classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
  70. args = ['--plugin', 'pretty',
  71. '--plugin', 'json:target/AppleBrands.json',
  72. '--plugin', 'com.aventstack.extentreports.cucumber.adapter.ExtentCucumberAdapter:Report',
  73. '--glue','com.apple.brands.test.stepdefinition',
  74. 'src/test/resources','src/main/java',
  75. '--tags', '@Api'
  76. ]
  77. }
  78. }
  79. }
  80. tasks.test {
  81. finalizedBy cucumber
  82. }

答案1

得分: 0

我找到了问题。

问题是你没有使用 LoggerHelper。

这是我的项目结构:

无法通过Gradle使用log4j将日志写入日志文件。

更新后的 LoggerHelper 类:

无法通过Gradle使用log4j将日志写入日志文件。

为了测试 LoggerHelper 是否工作,我添加了一个带有主方法的 Test 类:

无法通过Gradle使用log4j将日志写入日志文件。

控制台:

无法通过Gradle使用log4j将日志写入日志文件。

生成的日志文件。

无法通过Gradle使用log4j将日志写入日志文件。

英文:

I found the issue.

The problem was that you didn't use the LoggerHelper.

This is my project structure :

无法通过Gradle使用log4j将日志写入日志文件。

Updated LoggerHelper class :

无法通过Gradle使用log4j将日志写入日志文件。

To test whether LoggerHelper is working or not. I added one Test class with main method :

无法通过Gradle使用log4j将日志写入日志文件。

Console :

无法通过Gradle使用log4j将日志写入日志文件。

The log generated file.

无法通过Gradle使用log4j将日志写入日志文件。

答案2

得分: 0

经过大量的分析和比较,我找到了解决我的问题的方法。解决方案非常简单,只需要移除不需要的插件,这些插件导致了所有的问题。

当我像下面的插件部分所示移除了应用程序插件,它开始正常工作,甚至日志也开始正常工作。

  1. plugins {
  2. id 'java'
  3. id 'maven-publish'
  4. id 'java-library'
  5. id 'java-gradle-plugin'
  6. }
英文:

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.

  1. plugins {
  2. id 'java'
  3. id 'maven-publish'
  4. id 'java-library'
  5. id 'java-gradle-plugin'
  6. }

huangapple
  • 本文由 发表于 2020年9月7日 21:53:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/63778957.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定