【问题标题】:I can not turn off testng internal logging in console我无法在控制台中关闭 testng 内部日志记录
【发布时间】:2022-10-18 18:38:51
【问题描述】:

输出

    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

我的记录器实现

<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>

我的记录器类

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();
}

}

我正在使用 mvn 命令开始我的测试服,但我无法阻止从 testng 记录日志,因为它在我的代码之前运行。我在 testng 文档中发现了这一点,但我不知道该怎么做。

从 TestNG 7.5 版开始,TestNG 使用 Slf4j 提供的日志外观。 TestNG 默认不引入任何显式的 Slf4j 外观实现。 要控制 TestNG 内部输出的日志,请从此处添加对任何合适的 Slf4j 实现(Native 或 Wrapped 实现)的依赖

我的 logback.xml 路径 => src/test/resources/logback.xml

Pom 测试依赖

<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 插件

<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>

【问题讨论】:

    标签: java logging testng logback slf4j


    【解决方案1】:

    您需要执行以下操作才能使其正常工作

    将以下内容添加到您的logback.xml

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

    记录器在调用@BeforeSuite 之前就已初始化,这可能就是您尝试设置的日志记录级别未得到尊重的原因。

    但是,如果您从日志配置文件(在这种情况下为logback.xml)设置日志级别,您应该没问题!

    【讨论】:

    • 我试过了,但仍然有日志不断出现
    • 您能否编辑您的问题并添加有关文件添加位置的信息(src/main/resources(或)src/test/resources?)并添加有关 TestNG 依赖范围的详细信息?
    • 我更新了问题
    猜你喜欢
    • 2016-09-07
    • 1970-01-01
    • 2012-05-29
    • 1970-01-01
    • 2017-03-21
    • 1970-01-01
    • 2016-03-14
    • 2021-06-01
    • 1970-01-01
    相关资源
    最近更新 更多