I'm using slf4j-api for logging and log4j as a logger. But in my Spring Boot project it doesn't show proper logs with custom log4j settings in log4j.properties.POM:<dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.25</version></dependency>log4j.properties:# Root logger optionlog4j.rootLogger=INFO, stdout# Direct log messages to stdoutlog4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.Target=System.outlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%nIt prints something like:2018-10-29 13:47:40.601 INFO 7740 --- [nio-8080-exec-1] k.a.o.controller.CustomController : 2018-08-02 2018-08-04so, it doesn't show the line where it has been logged.Should I add anything else? 解决方案 If you want the default logger to print your desired pattern, you just need to add the following in your application.properties:logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%nSpring Boot provides Logback as its default logger. Also adding a logback.xml in your CLASSPATH will allow you to better configure everything else such as Appenders, Patterns etc (You may do the same through application.properties also). As @MarkBrammik already mentions, sl4j is only an abstraction and is therefore not enough. We use sl4j as an interface to utilize other concrete logging APIs such as Logback, log4j, JDK(java.util.logging), etc.Additionally, if you want to use log4j, then you will have to add the following dependency in your pom.xml<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j</artifactId></dependency>and then you may further configure it using a log4j.properties placed in your CLASSPATH. If you use log4j you will have to exclude Logback from your Spring dependencies, or else you may get the Class path contains multiple SLF4J bindings error : <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>You may check out the link provided below for more info and I hope you will find it useful : https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-logging.html 这篇关于记录器无法在Spring Boot 1.5.7中使用log4j.properties打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-20 04:41