Trying to override the JUL logger used by vert.x

1.5k Views Asked by At

I'd like to use Log4j2 and am setting up my MainVerticle with system properties to do that,

public class MainVerticle extends AbstractVerticle
{
    static {
        System.setProperty( "vertx.logger-delegate-factory-class-name",
                            "io.vertx.core.logging.Log4j2LogDelegateFactory" );
        System.setProperty( "log4j2.debug", "true" );
    }
....
}

I then deploy my HttpVerticle from this verticle, and in the HTTP verticle, I'm trying to use parameterized statements, which aren't working. So I added a couple of logging statements to show the logger delegate in-use, as well as the system property:

public class HttpServerVerticle extends AbstractVerticle
{
    private static final Logger LOGGER = LoggerFactory.getLogger( HttpServerVerticle.class );

    @Override
    public void start() throws Exception
    {
        LOGGER.info( System.getProperty( "vertx.logger-delegate-factory-class-name" ) );
        LOGGER.info( LOGGER.getDelegate().getClass().getName() );
        ....

And below in a handler of the incoming message, I'm using this:

LOGGER.info( "Chat message received: {}" + message.body(), message.body() );

Note that I'm adding the message.body() using concatenation to prove that the message is not an empty string.

The output of these log statements are:

[INFO] Sep 24, 2018 2:46:09 AM ca.LinkEdTutoring.chat.http.HttpServerVerticle
[INFO] INFO: io.vertx.core.logging.Log4j2LogDelegateFactory
[INFO] Sep 24, 2018 2:46:09 AM ca.LinkEdTutoring.chat.http.HttpServerVerticle
[INFO] INFO: io.vertx.core.logging.JULLogDelegate

and for an incoming message of the letter "b":

[INFO] INFO: Chat message received: {}b

I've tried setting the system properties in the pom.xml file and on the command line with -D arguments.

This is with vert.x 3.5.3

Any thoughts on what I've forgotten to do?

================

EDIT: capturing the salient points from the comment thread.

  1. cannot set system properties in a verticle, because the vert.x JUL logger gets initiated before the main verticle.
  2. cannot set ... in the pom.xml when running the code with the vertx plugin. The mvn vertx plugin must get invoked after vertx is initialized.
  3. only way it seems possible to override the JUL logger is the command line, using -D vargs.
  4. do not forget that vargs are set before the -jar switch, i.e., $ java -Dx=y -jar jarname.jar
1

There are 1 best solutions below

0
On

If you use the command line to start, you can configure it with -Dvertx.logger-delegate-factory-class-name=io.vertx.core.logging.Log4j2LogDelegateFactory. This is the easiest.

Of course, you can also set it directly in the code via System.setProperty, which is the same as the -D setting, but this must be done before the LoggerFactory is initialized. Obviously your code in the Verticle subclass must be executed after the Vertx initialization is successful. The LoggerFactory has already been initialized.