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

时间:2019-04-18 07:21:22

标签: logback vert.x

我尝试编写http服务器,并且我希望按照建议的方式进行注销。但是,我编写的顶点似乎不受我使用的logback.xml的控制。

然后,我写出我能想到的最简单的顶点,但看来我的logback.xml完全不起作用。

这是我的示例文字:

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”生成的胖子罐时,输出为:

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级别的日志。

1 个答案:

答案 0 :(得分:1)

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

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

从版本4.0(尚未发布)开始,Vert.x将按以下方式解析日志记录后端:

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

如果以上方法均无效,则将退回到jdk日志记录。

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