logback - 如何正确配置 vert.x 以使用 logback.xml?

标签 logback vert.x

我尝试编写一个 http 服务器,我想按照建议进行登录。但是,我写的verticles似乎不受我使用的logback.xml的控制。

然后我写了我能想出的最简单的verticle,但我的logback.xml 似乎完全不起作用。

这是我的示例verticle:

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;
import io.vertx.core.logging.SLF4JLogDelegateFactory;
public class SimplestVerticle extends AbstractVerticle {
    private static final Logger LOGGER = LoggerFactory.getLogger(SimplestVerticle.class);
    @Override
    public void start(Future<Void> future) {
        Future<Void> newFuture = Future.future();
        LOGGER.debug("This is debug");
        LOGGER.info("This is info");
        LOGGER.error("This is error");
        newFuture.setHandler(future);
    }
}

这是我的 pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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>[xxx]</groupId>
    <artifactId>[xxx]</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>io.vertx</groupId>
            <artifactId>vertx-core</artifactId>
            <version>3.7.0</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.3</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.3</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <manifestEntries>
                                        <Main-Class>io.vertx.core.Starter</Main-Class>
                                        <Main-Verticle>
                                            [somepackage.]SimplestVerticle
                                        </Main-Verticle>
                                    </manifestEntries>
                                </transformer>
                            </transformers>
                            <artifactSet/>
                            <outputFile>${project.build.directory}/${project.artifactId}-${project.version}-fat.jar</outputFile>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>


</project>

这是我的 logback.xml 放在 src/main/resources/下

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

但是,当我运行“mvn package”生成的 fat jar 时,输出是:

Apr 18, 2019 7:03:31 AM [package.name].SimplestVerticle
INFO: This is info
Apr 18, 2019 7:03:31 AM [package.name].SimplestVerticle
SEVERE: This is error

此处仍然显示INFO级别的日志。

最佳答案

在 3.x 及以下版本中,您必须使用系统属性指定日志记录后端:

-Dvertx.logger-delegate-factory-class-name=io.vertx.core.logging.Log4jLogDelegateFactory

从 4.0 版开始,Vert.x 将按如下方式解析日志后端:

  • vertx.logger-delegate-factory-class-name sysprop 的值(如果存在),或者
  • 如果存在 vertx-default-jul-logging.properties 文件,则进行 jdk 日志记录:
  • slf4j,或者
  • log4j,或者
  • log4j2

如果以上都不起作用,它将回退到 jdk 日志记录。

此行为与 Netty 的日志记录后端解析过程一致。

关于logback - 如何正确配置 vert.x 以使用 logback.xml?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55741006/

相关文章:

java - 如何获取 Maven 项目 list 中 Implementation-Version 字段中的 git SHA1 值?

java - Java-在多节点环境中记录最佳实践

java - 无法让 GELF Appender 添加 Sleuth Span Id

java - 无法使用 Vert.x 通过 TLS 连接到主机

java - 选择适用于 Java 的响应式 Web 框架

java - CXF拦截器阶段用于清理MDC

java - 在客户端服务器程序中使用 logback

java - 使用 Lambda 表达式参数化的 Vert.x 日志语句

java - Vertx 实例化多个垂直实例监听同一套接字

vert.x - Vert.x 中的顺序处理消息