简体   繁体   English

记录器无法在Spring Boot 1.5.7中使用log4j.properties打印

[英]Logger not printing with log4j.properties within Spring Boot 1.5.7

I'm using slf4j-api for logging and log4j as a logger. 我正在使用slf4j-api进行日志记录,并使用log4j作为记录程序。 But in my Spring Boot project it doesn't show proper logs with custom log4j settings in log4j.properties . 但是在我的Spring Boot项目中,在log4j.properties中没有显示带有自定义log4j设置的正确日志。

POM : 聚甲醛

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.25</version>
</dependency>

log4j.properties : 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. slf4j-api本身不提供日志记录系统的任何具体实现。 It's a set of interfaces if you wish. 如果需要,它是一组接口。

So adding only slf4j-api is not enough. 因此,仅添加slf4j-api是不够的。

Logback as was suggested in comments, contains a real implementation of logging system. 正如评论中所建议的那样,Logback包含日志系统的实际实现。 This library can be imported explicitly of, if you prefer a zero logging configuration, make sure that spring-boot-starter-logging is imported. 如果您希望使用零日志记录配置,则可以显式导入该库,请确保已导入spring-boot-starter-logging。

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). 因此,应该使用logback作为log4j的替代方法,log4j也是日志系统的真实实现(也可以使用,但坦白地说,logback在功能方面优于log4j1,所以我看不出有任何理由要做所以)。

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. 登录后,设置它的方法是在资源中提供logback.xml文件,该文件中应包含您所需的任何配置。

If you want the default logger to print your desired pattern, you just need to add the following in your application.properties : 如果要让默认记录器打印所需的图案,只需在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. Spring Boot提供Logback作为其默认记录器。 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 ). 另外,在CLASSPATH添加logback.xml可以使您更好地配置其他所有内容,例如AppendersPatterns等( 也可以通过application.properties进行相同操作 )。

As @MarkBrammik already mentions, sl4j is only an abstraction and is therefore not enough. 正如@MarkBrammik已经提到的那样, sl4j只是一个抽象,因此还不够。 We use sl4j as an interface to utilize other concrete logging APIs such as Logback , log4j , JDK(java.util.logging) , etc. 我们使用sl4j作为接口来利用其他具体的日志API,例如Logbacklog4jJDK(java.util.logging)等。

Additionally, if you want to use log4j , then you will have to add the following dependency in your pom.xml 另外,如果要使用log4j ,则必须在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 . 然后您可以使用放在CLASSPATHlog4j.properties对其进行进一步配置。 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 : 如果您使用log4j ,则必须从Spring依赖项中排除Logback ,否则您可能会得到Class path contains multiple SLF4J bindings错误:

<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 您可以查看下面提供的链接以获取更多信息,我希望您会发现它有用: https : //docs.spring.io/spring-boot/docs/current/reference/html/boot-features-logging.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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