I build my "superWebApp" with the next technology stack:
persistence provider - Hibernate 4.x
webMvc and beans container - Spring 4.x
web containter - Tomcat 7.5.x
I have a task to write all logs to db. And it would be a pain to do it for each logging framework separately. That's why I need to redirect all logs to single framework and then using a DBAppender wouldn't be a problem.
I was thinking about log4j2, since I use it to write logs in "superWebApp". So is there any idea how to redirect all logs from hibernate and spring to log4j2? (it would be good to redirect tomcat loogs too)?
If it is not possible, maybe there is another logging framework that can be central?
Hibernate uses Simple Logging Facade for Java (SLF4J) to redirect the logging output to your perfer logging frameworkis (log4j, JCL, JDK logging, lofback…).
All the Spring Boot starters depend on spring-boot-starter-logging , which uses Logback by default. For using Log4j2, you need to exclude spring-boot-starter-logging and add spring-boot-starter-log4j2 dependency.
Community support: Log4j 1. x is not actively maintained, whereas Log4j 2 has an active community where questions are answered, features are added and bugs are fixed. Automatically reload its configuration upon modification without losing log events while reconfiguring.
This worked perfectly for me:
<properties>
<logger.version>2.0-rc1</logger.version>
</properties>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${logger.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${logger.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-1.2-api</artifactId>
<version>${logger.version}</version>
</dependency>
<dependency>
<!--HIBERNATE-SPRING - LOGGER (log4j)-->
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.6</version>
</dependency>
From Logging Spring using Log4j2, we must use log4j-slf4j-impl.
I have tested it with Spring 4 also struts 2 and it works fine.
<log4j2.version>2.1</log4j2.version>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-1.2-api</artifactId>
<version>${log4j2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>${log4j2.version}</version>
</dependency>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With