简体   繁体   中英

Logger not printing with log4j.properties within Spring Boot 1.5.7

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 option
log4j.rootLogger=INFO, stdout

# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

It 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-04

so, it doesn't show the line where it has been logged. Should I add anything else?

slf4j-api by itself doesn't provide any concrete implementation of logging system. It's a set of interfaces if you wish.

So adding only slf4j-api is not enough.

Logback as was suggested in comments, contains a real implementation of logging system. This library can be imported explicitly of, if you prefer a zero logging configuration, make sure that spring-boot-starter-logging is imported.

So, logback should be used as an alternative to log4j which is also a real implementation of logging system (it also can be used, but frankly speaking logback is superior to log4j1 in terms of functionality, so I don't see any reason to do so).

Once you have logback, the way to set it is to provide a logback.xml file in resources that should contain any configuration you need there.

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%n

Spring 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM