记录器无法在Spring Boot 1.5.7中使用log4j.properties打印 [英] Logger not printing with log4j.properties within Spring Boot 1.5.7

查看:68
本文介绍了记录器无法在Spring Boot 1.5.7中使用log4j.properties打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用slf4j-api进行日志记录,并使用log4j作为记录器.但是在我的 Spring Boot 项目中,它没有在 log4j.properties 中显示带有自定义 log4j 设置的正确日志.

POM :

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

log4j.properties :

 #根记录器选项log4j.rootLogger = INFO,标准输出#将日志消息直接发送到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%n 

它打印类似:

  2018-10-29 13:47:40.601 INFO 7740 --- [nio-8080-exec-1] k.a.o.controller.CustomController:2018-08-02 2018-08-04 

因此,它不会在记录它的位置显示行.我还应该添加其他内容吗?

解决方案

如果希望 default 记录器打印所需的模式,则只需在应用程序中添加以下内容.属性:

  logging.pattern.console =%d {yyyy-MM-dd HH:mm:ss}%-5p%c {1}:%L-%m%n 

Spring Boot提供 Logback 作为其默认记录器.另外,在 CLASSPATH 中添加 logback.xml 可以使您更好地配置其他所有内容,例如 Appenders Patterns 等等(您也可以通过 application.properties 进行相同操作).

正如 @MarkBrammik 所述, sl4j 仅仅是一个抽象,因此还不够.我们使用 sl4j 作为接口来利用其他具体的日志记录API,例如 Logback log4j JDK(java.util.logging)等.

此外,如果要使用 log4j ,则必须在 pom.xml 中添加以下依赖项

 < dependency>< groupId> org.springframework.boot</groupId>< artifactId> spring-boot-starter-log4j</artifactId></dependency> 

,然后您可以使用放置在 CLASSPATH 中的 log4j.properties 对其进行进一步配置.如果使用 log4j ,则必须从Spring依赖项中排除 Logback ,否则您可能会得到 Class路径包含多个SLF4J绑定错误:

 < dependency>< groupId> org.springframework.boot</groupId>< artifactId> spring-boot-starter</artifactId><排除项><排除>< groupId> org.springframework.boot</groupId>< artifactId> spring-boot-starter-logging</artifactId></exclusion></exclusions></dependency> 

您可以查看下面提供的链接以获取更多信息,我希望您会发现它有用: 解决方案

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

这篇关于记录器无法在Spring Boot 1.5.7中使用log4j.properties打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆