简体   繁体   中英

Adding a Log4j logging to a Vaadin 8 application

I've tried to follow a documentation here Piping to Log4j using SLF4J , but still can't get a correct adapter used. So, how could one use a Log4j in Vaadin 8 application? What are the dependencies to be added to pom.xml ?

There are multiple additions/changes required to get logging working.

  1. Along the dependencies mentioned in the documentation to be added:

    • SLF4J
    • jul-to-slf4j
    • slf4j-log4j12-xxxj

A log4j-core should be added as well. The relevant snippet of pom.xml looks like this:

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

<dependency>
   <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.29</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>jul-to-slf4j</artifactId>
    <version>1.7.29</version>
</dependency>
  1. The next step is to add a log4j.properties file (in my case under src/main/resources/log4j.properties ). Here you can configure your logging properties

  2. As mentioned in the documentation, a SLF4JBridgeHandler should be added to a Servlet definition (in case, there is only one servlet)

 @WebServlet(urlPatterns = "/*", name = "MyUIServlet", asyncSupported = true)
    @VaadinServletConfiguration(ui = MyUI.class, productionMode = false)
    public static class MyUIServlet extends VaadinServlet {
        static {
            SLF4JBridgeHandler.install();
        }
    }
  1. Imports used for Logger and LoggerFactory are:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.bridge.SLF4JBridgeHandler;
  1. To verify a correct implementation is picked for a logger its simple name is printed System.out.println("Adapter name:" + logger.getClass().getSimpleName()); . Output should be : Log4jLoggerAdapter

Note: I've used a log4-core less than 2.9 since I am using Java 8 and jetty Error scanning entry ... . You should upgrade to a newer version if your jre is > 8

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM