Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to redirect all logs from hibernate and spring to log4j2?

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?

like image 212
Yurii Bondarenko Avatar asked Mar 08 '14 14:03

Yurii Bondarenko


People also ask

Does hibernate use Log4j?

Hibernate uses Simple Logging Facade for Java (SLF4J) to redirect the logging output to your perfer logging frameworkis (log4j, JCL, JDK logging, lofback…).

What is the starter for using Log4j2 for logging in spring boot?

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.

What is difference between Log4j and Log4j2?

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.


2 Answers

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>
like image 194
Yurii Bondarenko Avatar answered Oct 05 '22 23:10

Yurii Bondarenko


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>
like image 22
Alireza Fattahi Avatar answered Oct 06 '22 00:10

Alireza Fattahi