WhitneyChia
WhitneyChia

Reputation: 796

Gradle test log4j.xml

Sorry, if this isn't enough information, new to gradle, let me know if more is needed. I have a log4j2.xml in a config folder that I define in a gradle.properties file.

project.ext.logConfigFile="./config/log4j2.xml"

The structure of my project

project
    src
    test
    lib
    config
        log4j2.xml

Now when I run gradle test, it says that it can't find the log4j2 configuration.

This is what my build.gradle file looks like..

sourceSets {
    test {
        java {
            srcDirs = ["test"]
        }
    }
    main {
        java {
            srcDirs = ["src"]
        }
    }
}


test {
    testLogging {
        outputs.upToDateWhen {false}
        showStandardStreams = true
        events "PASSED", "STARTED", "FAILED", "SKIPPED"
        afterSuite { desc, result ->
            if (!desc.parent) { // will match the outermost suite
                println "Results: ${result.resultType} (${result.testCount} tests, ${result.successfulTestCount} successes, ${result.failedTestCount} failures, ${result.skippedTestCount} skipped)"
      }
    }
  }
}

Been dinking around with things I found on the internet, that's why all those statements in the "testLogging" part. But basically, I have LOGGER.info() statements that aren't coming up because it defaults to errors only.

ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.

Can I tell gradle test exactly where to go for my log4j2.xml? Something like this? Though I don't think this works....

tasks.withType(Test) { 
     systemProperty 'log4j.configuration', logConfigFile
}

A little stuck right now. Thanks!

Upvotes: 0

Views: 4510

Answers (2)

Abhijit Sarkar
Abhijit Sarkar

Reputation: 24637

Just put it in src/test/resources. That comes first on the classpath during testing. Then configure log4j to load from classpath, not a file path; I think that’s the default behavior anyway

Upvotes: 1

WhitneyChia
WhitneyChia

Reputation: 796

Believe I solved the problem, you just define where the resources are in the sourceSets also. It will look there for the log4j2.xml.

sourceSets {
    test {
        java {
            srcDirs = ["test"]
        }
        resources {
            srcDirs = ["config"]
        }
    }
    main {
        java {
            srcDirs = ["src"]
        }
    }
}

Upvotes: 0

Related Questions