22

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?

3 Answers 3

52

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!

Sign up to request clarification or add additional context in comments.

5 Comments

I think you meant : "1. exclude logback framework" instead of "exclude slf4j logger" right?
Also make sure you use <artifactId>spring-boot-starter-log4j2</artifactId> if you use log4j2, since <artifactId>spring-boot-starter-log4j</artifactId> is valid but won't work for log4j2
For me I has to add these two <exclusion> <artifactId>logback-classic</artifactId> <groupId>ch.qos.logback</groupId> </exclusion> <exclusion> <artifactId>log4j-over-slf4j</artifactId> <groupId>org.slf4j</groupId> </exclusion> </exclusions>
For me using Gradle I just did this: configurations.all*.exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging' and then in my dependencies {} section I added runtime 'org.springframework.boot:spring-boot-starter-log4j2'
For me it does not work: all logs are printing to console.
11

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.

2 Comments

Nowadays there is also spring-boot-starter-log4j2 since spring-boot-1.2.0.RELEASE. Just for reference.
This does not work for me as well: all logs are printing to console.
2

This is what worked for me. Having two additional exclusions. Else Application was not picking up log4j and was had conflict

Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.0.0</version>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
        <exclusion>
            <artifactId>logback-classic</artifactId>
            <groupId>ch.qos.logback</groupId>
        </exclusion>
        <exclusion>
            <artifactId>log4j-over-slf4j</artifactId>
            <groupId>org.slf4j</groupId>
        </exclusion>            
    </exclusions>           
</dependency>

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.