Couldn't find any good answer why slf4j logging not working with spring 4. Thanks to this post I've made particularly all work, but the author didn't mention that:
The mandatory logging dependency in Spring is the Jakarta Commons Logging API (JCL)
(http://docs.spring.io/spring/docs/4.1.x/spring-framework-reference/htmlsingle/).
It means that if we want slf4j to work WE MUST EXCLUDE commons-logging from our classpath (or pom.xml if using maven). While using maven spring-core automatically picks commons-logging. (I understood this when executed the command "mvn dependency:tree").
STEPS TO REPRODUCE:
Exclude commons-logging from spring-core
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> <exclusions> <exclusion> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> </exclusion> </exclusions> </dependency>
Add to the pom.xml next dependencies:
<dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.19</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-slf4j-impl</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> <version>1.7.19</version> </dependency>
Create log4j2.xml file like this (if you want log4j2 logging through slf4j).
<?xml version="1.0" encoding="UTF-8"?> <Configuration status="WARN"> <Appenders> <Console name="CONSOLE" target="SYSTEM_OUT"> <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" /> </Console> </Appenders> <Loggers> <Root level="error"> <AppenderRef ref="CONSOLE" /> </Root> <logger level="info" additivity="false" name="org.springframework.security"> <AppenderRef ref="CONSOLE" /> </logger> <logger level="info" additivity="false" name="org.springframework.web"> <AppenderRef ref="CONSOLE" /> </logger> <logger level="warn" additivity="false" name="controllers"> <AppenderRef ref="CONSOLE" /> </logger> </Loggers> </Configuration>
write log messages in your classes:
private final Logger logger =LoggerFactory.getLogger(this.getClass()); logger.debug("This is a debug message"); logger.info("This is an info message");