Search code examples
javaloggingtestnglogbackslf4j

I can not turn off testng internal logging in console


output

    14:42:32.418 [main] DEBUG org.testng.internal.Graph - [Graph] ================ SORTING
    14:42:32.476 [main] DEBUG org.testng.internal.Graph - [Graph] =============== DONE SORTING
    14:42:32.476 [main] DEBUG org.testng.internal.Graph - [Graph] ====== SORTED NODES
    14:42:32.476 [main] DEBUG org.testng.internal.Graph - [Graph] ====== END SORTED NODES
    14:42:32.476 [main] INFO org.testng.internal.Utils - [Utils] MethodGroupsHelper.sortMethods() took 61 ms.
    14:42:32.478 [main] INFO org.testng.internal.Utils - [Utils] MethodGroupsHelper.collectMethodsByGroup() took 0 ms.
    14:42:32.479 [main] DEBUG org.testng.internal.Graph - [Graph] ADDING NODE TestBase.appiumTearDown()[pri:0, instance:null] 1618865525
    14:42:32.479 [main] DEBUG org.testng.internal.Graph - [Graph] ================ SORTING
    14:42:32.479 [main] DEBUG org.testng.internal.Graph - [Graph] =============== DONE SORTING
    14:42:32.479 [main] DEBUG org.testng.internal.Graph - [Graph] ====== SORTED NODES
    14:42:32.479 [main] DEBUG org.testng.internal.Graph - [Graph] ====== END SORTED NODES

My Logger implementation

<configuration>
<appender name="Console" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
        <pattern>[%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
</appender>
<root level="warn">
    <appender-ref ref="Console" />
</root>

My Logger Class

public class LogManager extends TestBase {
protected static ThreadLocal<StringBuilder> log = ThreadLocal.withInitial(StringBuilder::new);

public static void log(String msg) {
    log.get().append(msg).append("\n");
}

@BeforeSuite(alwaysRun = true)
public void setUp() {
    TestNG testNG = new TestNG();
    testNG.setVerbose(2);
    Logger bonigarcia = (Logger) LoggerFactory.getLogger("io.github.bonigarcia");
    Logger testng = (Logger) LoggerFactory.getLogger("org.testng");
    Logger apache = (Logger) LoggerFactory.getLogger("org.apache");
    Logger slack = (Logger) LoggerFactory.getLogger("com.slack.api");
    Logger gitHub = (Logger) LoggerFactory.getLogger("org.kohsuke");

    bonigarcia.setLevel(Level.WARN);
    testng.setLevel(Level.ERROR);
    apache.setLevel(Level.WARN);
    slack.setLevel(Level.WARN);
    gitHub.setLevel(Level.WARN);
    log.remove();
}

}

I am starting my test suit with mvn command and I can not prevent logging from testng because it runs before my codes. I found that in the testng documentation but I couldn't figure out how to do it.

Starting from TestNG version 7.5 TestNG makes use of the logging facade provided by Slf4j. TestNG by default does not bring in any explicit Slf4j facade implementation. To control the logs being output by TestNG internals, please add a dependency on any suitable Slf4j implementation (Native Or Wrapped implementation) from here

My logback.xml path => src/test/resources/logback.xml

Pom testng dependency

<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>7.5</version>
</dependency>
<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.2.11</version>
</dependency>

Maven plugins

<testResources>
    <testResource>
        <directory>src/test/resources</directory>
        <filtering>true</filtering>
        <includes>
            <include>*.properties</include>
        </includes>
    </testResource>
</testResources>
<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
        <configuration>
            <source>10</source>
            <target>10</target>
        </configuration>
    </plugin>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
        <executions>
            <execution>
                <phase>integration-test</phase>
            </execution>
        </executions>
    </plugin>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <executions>
            <execution>
                <id>copy-dependencies</id>
                <phase>prepare-package</phase>
                <goals>
                    <goal>copy-dependencies</goal>
                </goals>
                <configuration>
                    <outputDirectory>
                        ${project.build.directory}/libs
                    </outputDirectory>
                </configuration>
            </execution>
        </executions>
    </plugin>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>3.2.0</version>
        <executions>
            <execution>
                <goals>
                    <goal>test-jar</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    <plugin>
        <groupId>com.spotify</groupId>
        <artifactId>dockerfile-maven-plugin</artifactId>
        <version>1.3.5</version>
        <configuration>
            <repository>athena</repository>
            <tag>test</tag>
        </configuration>
    </plugin>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>3.0.0-M6</version>
        <configuration>
            <testFailureIgnore>true</testFailureIgnore>
            <parallel>classes</parallel>
            <threadCount>${threadCount}</threadCount>
            <systemPropertyVariables>
                <propertyName>${env}</propertyName>
            </systemPropertyVariables>
            <suiteXmlFiles>
                <suiteXmlFile>${suiteXmlFile}</suiteXmlFile>
            </suiteXmlFiles>
        </configuration>
    </plugin>
</plugins>

Solution

  • You would need to do the following for getting this to work

    Add the below to your logback.xml

    <logger name="org.testng" level="debug">
      <appender-ref ref="Console"/>
    </logger>
    

    The logger is initialised much before the @BeforeSuite is called which perhaps is why the logging levels that you are attempting to set is NOT getting honoured.

    But if you set the log levels from your log configuration file (logback.xml in this case) you should be fine!