Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring-Boot logging with log4j2?

I'm using spring-boot-starter, and would like to configure log4j2.xml to log asynchron + different content to different logfiles.

I created the log4j2 file, but Spring still uses the spring-boot default logging. How can I switch the logging?

like image 834
membersound Avatar asked Sep 05 '14 09:09

membersound


People also ask

Does spring boot logging use Log4j?

Spring Boot also supports either Log4j or Log4j 2 for logging configuration, but only if one of them is on the classpath. If you are using the starter poms for assembling dependencies that means you have to exclude Logback and then include your chosen version of Log4j instead.

How do you configure Log4j for logging in spring boot?

Adding Log4j2 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.

Is Spring Boot affected by log4j2?

Spring Boot users are only affected by this vulnerability if they have switched the default logging system to Log4J2.


2 Answers

I've a better way:

  1. Exclude logback logger:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    
  2. Add log4j2 boot starter:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-log4j2</artifactId>
    </dependency>
    

Source: http://docs.spring.io/spring-boot/docs/1.5.1.RELEASE/reference/htmlsingle/#howto-configure-log4j-for-logging

Enjoy!

like image 124
kles4eko Avatar answered Oct 04 '22 21:10

kles4eko


Try this:

  1. Exclude spring-boot-starter-logging e.g.

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    
  2. Add dependencies for your logging interface e.g. slf4j

    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-slf4j-impl</artifactId>
        <version>2.0.2</version>
    </dependency>
    
  3. Add other logging implementations pointing to chosen logging interface e.g.

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jcl-over-slf4j</artifactId>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jul-to-slf4j</artifactId>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>log4j-over-slf4j</artifactId>
    </dependency>
    
  4. Add your target logging implementation e.g.

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

And it should work.

like image 42
Grzegorz Solecki Avatar answered Oct 04 '22 23:10

Grzegorz Solecki