Redirecting embedded jetty log to log4j2

When using embedded jetty within an application, per default there is no support for log4j2. Using slf4j it is possible to redirect the logging to log4j2. But without this redirection there is no way to directly use it. But of course there is a way to use it. Only a piece of simple adapter code is required.

Writing an adapter for the jetty logging api

Jetty uses an own logging api that redirects logging events to a provided logging api. For that api a simple logging adapter is required. This adapter must extend the org.eclipse.jetty.util.log.AbstractLogger. Before starting jetty simply org.eclipse.jetty.util.log.Log.setLog(AbstractLogger) must be invoked in order to replace the default logging mechanism of jetty.
In order to get the example simply checkout the source code or go to github.

Sources

4 thoughts on “Redirecting embedded jetty log to log4j2

  1. Hi Robert, thanks for the piece of code. I was exaclty looking for an answer. But i dont get what i need to do to get a full working set. What should i call in the main-method? And do i need a jetty.properties for the jetty-logger. My log4j2 works fine.

    • Haha, forget my question. It is just as you said: Log.setLog(new Jetty2Log4j2Bridge(“Jetty-Server”)); and everything works fine. Thank you.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.