Vert.x Logging

Robin Coe
1 min readDec 17, 2018

--

Buried in the core vert.x docs is a description for overriding the built-in JUL logger using a system property, vertx.logger-delegate-factory-class-name. My preference for logging is the Log4j2 framework, partly for its ability to use parameterized statements:

String some_val = "my_val";
logger.info( "this is how you inject {} into your statement.", some_val );

I find this much preferable to the JUL logger, which requires string concatenation to accommodate the same thing:

logger.info( "this is how you inject " + some_val + " into your statement." );

To set Log4j2 dependencies in your pom.xml, include the following:

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</dependency> <dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId> </dependency>

and refer to your logger exactly as you would with any other factory, by importing the vertx logger:

import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;
public class MainVerticle extends AbstractVerticle
{
private final Logger LOGGER = LoggerFactory.getLogger( MainVerticle.class );
...
}

Activating the Log4j2 factory from the command line relies on the -D JVM switch:

$ java -jar -Dvertx.logger-delegate-factory-class-name=io.vertx.core.logging.Log4j2LogDelegateFactory <your-fatjar>.jar

What’s important to note is that the vert.x launcher initializes the logger factory before your verticle starts. In other words, you can’t programmatically set a system property in your verticle implementation. Also note that you cannot set the system property in your pom.xml when using the fabric8 Maven plugin:

<property name="logger-delegate-factory-class-name">io.vertx.core.logging.Log4j2LogDelegateFactory</property>

$ mvn package vertx:run

will not work. Not sure if this is a bug or expected behaviour but I am hoping to spare some cycles some time to look into it.

--

--

Robin Coe

Long term lurker, short term contributor. Decided it was time to document what I find interesting in software development, heavily weighted towards Java.