我正在构建一个将利用 org.apache.zookeeper.ZooKeeper 的测试驱动程序。该库正在将嘈杂的输出转储到我想控制或抑制的控制台。
我尝试创建自定义 logj2.properties 文件和自定义 log4j2.xml 文件。 我尝试传递几个运行时属性来覆盖 log4j 配置,但我无法抑制此输出。
我认为记录器是在here创建的。
docker-compose.yml
version: '3.9'
services:
zoo:
container_name: zoo
image: zookeeper:3.8.3
ports:
- published: 8084
target: 2181
restart: unless-stopped
stdin_open: true
tty: true
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.my</groupId>
<artifactId>MyApp</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>Test</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.8.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>org.my.Test</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<!-- Includes Test* and *Test by default -->
<!-- skips surefire tests without skipping failsafe tests.
Property
value seems to magically default to false -->
<skipTests>${skip.surefire.tests}</skipTests>
</configuration>
</plugin>
</plugins>
<directory>target</directory>
<outputDirectory>target/classes</outputDirectory>
<finalName>${project.artifactId}-${project.version}</finalName>
<sourceDirectory>src/main/java</sourceDirectory>
</build>
</project>
src/main/java/org/my/Test.java
package org.my;
import java.io.IOException;
import org.apache.zookeeper.ZooKeeper;
public class Test {
public static void main(String[] argv) throws IOException {
System.out.println("hello");
ZooKeeper zk = new ZooKeeper("localhost:8084", 100, null);
}
}
mvn clean install
java -jar target/MyApp-1.0.jar
hello
17:26:50.713 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment:zookeeper.version=3.8.3-6ad6d364c7c0bcf0de452d54ebefa3058098ab56, built on 2023-10-05 10:34 UTC
17:26:50.716 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment:host.name=...
17:26:50.716 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment:java.version=11.0.22
17:26:50.716 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment:java.vendor=Red Hat, Inc.
17:26:50.716 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment:java.home=/usr/lib/jvm/java-11-openjdk-11.0.22.0.7-1.amzn2.0.1.x86_64
17:26:50.716 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment:java.class.path=target/MyApp-1.0.jar
17:26:50.716 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment:java.library.path=/usr/java/packages/lib:/usr/lib64:/lib64:/lib:/usr/lib
17:26:50.716 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment:java.io.tmpdir=/tmp
17:26:50.716 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment:java.compiler=<NA>
17:26:50.716 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment:os.name=Linux
17:26:50.716 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment:os.arch=amd64
17:26:50.716 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment:os.version=4.14.336-257.562.amzn2.x86_64
17:26:50.716 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment:user.name=...
17:26:50.716 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment:user.home=...
17:26:50.716 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment:user.dir=...
17:26:50.716 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment:os.memory.free=244MB
17:26:50.716 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment:os.memory.max=3962MB
17:26:50.716 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment:os.memory.total=250MB
17:26:50.721 [main] INFO org.apache.zookeeper.ZooKeeper - Initiating client connection, connectString=localhost:8084 sessionTimeout=100 watcher=null
17:26:50.728 [main] INFO org.apache.zookeeper.common.X509Util - Setting -D jdk.tls.rejectClientInitiatedRenegotiation=true to disable client-initiated TLS renegotiation
17:26:50.734 [main] INFO org.apache.zookeeper.ClientCnxnSocket - jute.maxbuffer value is 1048575 Bytes
17:26:50.745 [main] INFO org.apache.zookeeper.ClientCnxn - zookeeper.request.timeout value is 0. feature enabled=false
仔细观察 ZooKeeper.java 类,我发现代码正在使用 SLF4J。
import org.slf4j.LoggerFactory;
查看构建中创建的 jar 文件,我发现包含了 logback。
bash-4.2$ ls -1 target
audience-annotations-0.12.0.jar
classes
commons-io-2.11.0.jar
generated-sources
logback-classic-1.2.10.jar
logback-core-1.2.10.jar
maven-archiver
MyApp-1.0.jar
netty-buffer-4.1.94.Final.jar
netty-codec-4.1.94.Final.jar
netty-common-4.1.94.Final.jar
netty-handler-4.1.94.Final.jar
netty-resolver-4.1.94.Final.jar
netty-transport-4.1.94.Final.jar
netty-transport-classes-epoll-4.1.94.Final.jar
netty-transport-native-epoll-4.1.94.Final.jar
netty-transport-native-unix-common-4.1.94.Final.jar
slf4j-api-1.7.30.jar
zookeeper-3.8.3.jar
zookeeper-jute-3.8.3.jar
这导致我创建
src/main/resources/logback.xml
来覆盖日志记录级别。
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="error">
<appender-ref ref="STDOUT" />
</root>
</configuration>
一旦将此文件放置到位,我就会看到我希望看到的输出。
bash-4.2$ java -jar target/MyApp-1.0.jar
hello