Search code examples
logbackvert.x

How to properly configure vert.x to use logback.xml?


I try to write an http server and I would like to logback as recommended. However, the verticles I write seems not to be controlled by the logback.xml I use.

Then I write the simplest verticle I can come up with, but it seems that my logback.xml totally doesn't work.

Here is my example 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);
    }
}

Here is my 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>

Here is my logback.xml that is put under 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>

However, when I run the fat jar generated by "mvn package", the output is:

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

The log at INFO level is still shown here.


Solution

  • In versions 3.x and below, you must specify the logging backend with a system property:

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

    Starting with version 4.0, Vert.x will resolve the logging backend as follows:

    • the value of the vertx.logger-delegate-factory-class-name sysprop if present, or
    • jdk logging if vertx-default-jul-logging.properties file is present:
    • slf4j, or
    • log4j, or
    • log4j2

    If none of the above works, it will fallback to jdk logging.

    This behavior is aligned with Netty's logging backend resolution process.