1

I have Spring Boot application and I want to use log4j2 instead of logback. I've already done all the necessary steps to exclude spring-boot-starter-logging from spring-boot-starter-web and added spring-boot-starter-log4j2 dependency. When I add log4j2.properties file under resources folder it works correct and I can see logs. But when I change file name to, lets say log4j2-demo.properties and set logging.config=classpath:log4j2-demo.properties nothing prints to console. For testing purpose I've set different name for lo4gj2 file in logging.config and it gives error with file not found and I think logging.config property actually working and reading my properties file.

application.properties file:

spring.profiles.active=dev

application-dev.properties file:

logging.config=classpath:log4j2-demo.properties

log4j2 properties file:

status=debug
name=PropertiesConfig
#
filters=threshold
#
filter.threshold.type=ThresholdFilter
filter.threshold.level=debug
#
appenders=console
#
appender.console.type=Console
appender.console.name=STDOUT
appender.console.layout.type=PatternLayout
appender.console.layout.pattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
#
rootLogger.level=info
rootLogger.appenderRefs=stdout
rootLogger.appenderRef.stdout.ref=STDOUT

my application structure:

src
  main
    java
    resources
       application.properties
       application-dev.properties
       log4j2-demo.properties

pom.xml :

...
<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.graylog2.log4j2</groupId>
        <artifactId>log4j2-gelf</artifactId>
        <version>1.3.1</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-log4j2</artifactId>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.zalando/logbook-spring-boot-starter -->
    <dependency>
        <groupId>org.zalando</groupId>
        <artifactId>logbook-spring-boot-starter</artifactId>
        <version>2.0.0</version>
    </dependency>
  ...
0

2 Answers 2

0

If you use log4j 2.13.0 you can use profiles to define log4j 2 system properties in your spring configuration. So you could have:

application-dev.properties

log4j2.configurationFile=log4j2-demo.properties.

Log4j 2.13.0 also supports placing the log4j 2 configuration file in Spring Cloud Config.

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

Comments

0

I'm not sure if this answer applies to this question, but in my case, I added the resource to the build configuration on my pom.xml, e.g.

...
<build>
    ...
    <resources>
        <resource>
            <directory>${basedir}/src/main/resources</directory>
            <filtering>true</filtering>
            <includes>
                <include>**/*.xml</include> <!-- or **/*.properties -->
            </includes>
        </resource>
    </resources>
    ...
</build>
...

Of course, I excluded spring-boot-starter-logging from spring-boot-starter-web and spring-boot-starter-actuator

...
<exclusions>
    <exclusion>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-logging</artifactId>
    </exclusion>
</exclusions>
...

Although, I used the default log4j2 file name log4j2.xml and the step configuration on the application.yml was not required, I mean this configuration logging.config=classpath:log4j2-demo.properties

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.