简体   繁体   English

Spring 引导 - 无法关闭日志记录

[英]Spring boot - Cannot turn off logging

I am trying to turn off the console output in STS for a spring boot application using the application.properties file.我正在尝试使用 application.properties 文件为 spring 启动应用程序关闭 STS 中的控制台 output。

Setting the value logging.level.root does seem to have some effect but I can never turn it off completely and nor can I turn off the auto configuration report output.设置logging.level.root这个值好像有一些效果,但是我永远不能完全关闭它,也不能关闭自动配置报告output。

logging.level.root=OFF
spring.main.banner-mode=OFF
application.version=@project.version@

The banner does get turned off by the property spring.main.banner-mode.横幅确实会被属性 spring.main.banner-mode 关闭。

For some reason with the above properties I still get DEBUG output from spring on startup:由于上述属性的某些原因,我仍然在启动时从 spring 获得 DEBUG output:

 2017-05-09 15:33:16.744 DEBUG 11772 --- [           main] .b.l.ClasspathLoggingApplicationListener : Application started with classpath:
 2017-05-09 15:33:16.798 DEBUG 11772 --- [           main] o.s.boot.SpringApplication               : Loading source class 

There are more lines telling me which properties files are being loaded but I dont want to fill up this post with them.还有更多行告诉我正在加载哪些属性文件,但我不想用它们填满这篇文章。

Following from this I then get the autoconfiguration report output.之后我得到了自动配置报告 output。

I am wondering if I have a configuration issue and if this would cause spring to continue to output on start up?我想知道我是否有配置问题,这是否会导致 spring 在启动时继续到 output?

To answer my own question: after trial and error, I finally ended up with the following which suppresses all output on startup via the application.properties file:回答我自己的问题:经过反复试验,我最终得到了以下内容,它通过application.properties文件抑制了启动时的所有输出:

 logging.level.root=OFF
 logging.level.org.springframework.boot=OFF
 spring.main.banner-mode=OFF

Add the following to the the package of your choice.将以下内容添加到您选择的包中。

logging.level.<package>=OFF

logging.level.root=OFF doesn´t work for me logging.level.root=OFF对我不起作用

您可以在bootstrap.yml中定义日志记录属性。我使用这种方式成功禁用了日志

The problem with your approach is that it only configures the root level logger;你的方法的问题在于它只配置了根级记录器; individual logs will be turned back on if the matching logger properties are set (eg logging.level.org.springframework.boot=DEBUG ).如果设置了匹配的记录器属性(例如logging.level.org.springframework.boot=DEBUG ),将重新打开单个日志。 I'm guessing that's why your self answer needed to explicitly disable the org.springframework.boot logger as well as the root logger.我猜这就是为什么您的自我回答需要明确禁用org.springframework.boot记录器以及根记录器的原因。

One approach to completely disable logging would be to create a special logback.xml file that disables logging completely.完全禁用日志记录的一种方法是创建一个特殊的logback.xml文件来完全禁用日志记录。

<configuration>
</configuration>

If you always want logging turned off, you can just create the logback.xml file and put it in the root package (eg src/main/resources/logback.xml ).如果你总是想关闭日志,你可以创建logback.xml文件并将它放在根包中(例如src/main/resources/logback.xml )。 If you only want it disabled in certain scenarios (eg if in a "nologging" Spring profile ), you could create a custom file with a different name ( logback-off.xml ) and specify that as the config file for the profiles/cases where you want it disabled.如果您只想在某些情况下禁用它(例如,如果在“nologging”Spring 配置文件中),您可以创建一个具有不同名称的自定义文件( logback-off.xml )并将其指定为配置文件/案例的配置文件你希望它被禁用的地方。

logging.config=classpath:logback-off.xml

For those who still couldn't fix problem.对于那些仍然无法解决问题的人。 I was calling jar file to create some objects.我正在调用 jar 文件来创建一些对象。 The class in that jar file was writing unwanted log.该 jar 文件中的 class 正在写入不需要的日志。 I tried everything, went through so many forums.我尝试了一切,浏览了很多论坛。

So solution for me was to set ROOT to only ERROR:所以我的解决方案是将 ROOT 设置为仅错误:

In application.ymlapplication.yml

logging:
  level:
    ROOT: ERROR
    com.mypackage: INFO
    org.springframework.*: INFO

So, this way only errors will be shown in the log but also info logs that you have configured.因此,这种方式只会在日志中显示错误,还会显示您配置的信息日志。

I'm putting here some codes I've tried didn't work for me but may work for someone else:我在这里放一些我试过的代码对我不起作用,但可能对其他人有用:

In application.yml , I tried this:application.yml中,我试过这个:

logging:
  level:
    com.organization.package: OFF

I tried this:我试过这个:

logging:
  level:
    com.organization.package.*: OFF

I tried this:我试过这个:

ClassThatLogsUnwanted class = new ClassThatLogsUnwanted();
Logger.getLogger(class.getName()).setLevel(Level.OFF);

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

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