How to change the logger in the spring from logback to log4j2?

In debug mode, spring prints info into console. It is engaged in either logback.

Log4J2 configuration:

<?xml version="1.0" encoding="UTF-8"?> <Configuration status="error" monitorInterval = "30"> <Properties> <Property name="pattern">%d{yyyy-MM-dd HH:mm:ss.SSS} [%level] [thread-id %T] %l - %msg%n</Property> </Properties> <Appenders> <RollingFile name="fileLogger" fileName="/log.log" filePattern="/log-%d{yyyy-MM-dd}.log"> <PatternLayout> <pattern>${pattern}</pattern> </PatternLayout> <Policies> <TimeBasedTriggeringPolicy interval="1" modulate="true" /> </Policies> </RollingFile> <Console name="console" target="SYSTEM_OUT"> <PatternLayout pattern="${pattern}" /> </Console> </Appenders> <Loggers> <Root level="info" additivity="false"> <appender-ref ref="console" /> <appender-ref ref="fileLogger" /> </Root> <Logger name="com.spring.*" level="info"> <appender-ref ref="fileLogger" level="info" /> </Logger> </Loggers> </Configuration> 

    1 answer 1

    In order to replace the logging library in Spring Boot, you need to exclude spring-boot-starter-logging from dependencies and add spring-boot-starter-log4j2 like this:

     <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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency> 

    Documentation

    If Spring Boot is not used, it is enough to add Log4J to the dependencies, and Spring will start using it. For Spring 5, add Log4J2:

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

    For Spring 4 - Log4J:

     <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> 
    • Spring Boot not used - Tsyklop
    • Completed the answer. - fori1ton
    • does not work. added depending - DEBUG infa ceased to be displayed. - Tsyklop
    • And you have a log4j config? And is it configured appender for those packages from which you write messages? And the level of rootLogger logging is not accidentally set to INFO? Create a minimal, self-sufficient, reproducible example and add it to the question, otherwise attempts to help you turn into fortune telling. - fori1ton
    • Added a config to the topic header. If I call the logger myself then everything works as it should. - Tsyklop