vertx LoggerHandler not adding logback

放肆的年华 提交于 2019-11-30 14:15:31

This worked for me with Vert.x 3.4.1:

    import static io.vertx.core.logging.LoggerFactory.LOGGER_DELEGATE_FACTORY_CLASS_NAME;
    import io.vertx.core.logging.LoggerFactory;

    // ...

    setProperty (LOGGER_DELEGATE_FACTORY_CLASS_NAME, SLF4JLogDelegateFactory.class.getName ());
    LoggerFactory.getLogger (LoggerFactory.class); // Required for Logback to work in Vertx

The key is to get a logger, which I guess initializes the Logging subsystem, the class that you use to get a Logger seems irrelevant as I tried with two different ones.

I run these lines as the first ones of the program in production code and in the tests to work properly in both contexts.

I was able to get it to work by setting the VM options as such:

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

Then in my log4j.properties, I had to add this:

log4j.category.io.vertx = TRACE

I know this question is getting a bit old, but the only way I was able to get the vertx LoggerHandler to not use JUL was to call LoggerFactory.initialise() after setting the system property as described in the question.

Even better, I set the property in my build.gradle, like so:

run {
  systemProperty(
    "vertx.logger-delegate-factory-class-name",
    "io.vertx.core.logging.SLF4JLogDelegateFactory"
  )
  args = ['run', mainVerticleName, "--redeploy=$watchForChange", "--launcher-class=$mainClassName", "--on-redeploy=$doOnChange",
          "-Dvertx.logger-delegate-factory-class-name=io.vertx.core.logging.SLF4JLogDelegateFactory" ]
}

And then at the very top of my MainVerticle::start I have:

LoggerFactory.initialise()

And, boom. Everything is now formatted correctly, including all the startup output.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!