How can I correctly set the logging level when using a test project built from ProjectBuilder?


(mark.malone) #1

When I create a test project with ProjectBuilder, and set the project logging level, but I’m still getting debugging output. I’ve created a test that shows that the logging level for the project is set to Error, the logger still shows that Debug is enabled. What else do I need to do to set the logging level correctly using ProjectBuilder?

class LoggingLevelIssueTest {
    Project project
    @Before
    void setUp() throws Exception {
        project = ProjectBuilder.builder().build()
        project.apply plugin: 'java'
        project.repositories {
            mavenCentral()
        }
        project.dependencies {
            compile 'commons-lang:commons-lang:2.6'
        }
    }
    @Test
    void logLevelSetToError() {
        println "Logging Level: ${project.logging.level}"
        println "Is Debug Enabled: ${project.logger.isDebugEnabled()}"
        project.logging.level = LogLevel.ERROR
          println "Logging Level After Setting to ERROR: ${project.logging.level}"
        println "Is Debug Enabled: ${project.logger.isDebugEnabled()}"
          assert project.logging.level == LogLevel.ERROR && project.logger.isDebugEnabled() == false
    }
}

(William Lichtenberger) #2

Is there a good answer to this problem? I’ve resorted to the following hack:

public class FilteredPrintStream extends PrintStream

{

public FilteredPrintStream(PrintStream source)

{

super(source);

}

@Override

public void write(byte[] buf, int off, int len)

{

String string = new String(buf);

if (!string.contains(" DEBUG "))

{

super.write(buf, off, len);

}

}

}

I create one of those and set System.out to that. If there’s really no elegant solution than I suppose I can expand that class to actually accept a level and filter messages above that level. But seems like there should be a more sane way to do it.