我应该将 java.util.logging 的 logging.properties 文件放在 web 应用程序(maven 项目)中的什么位置?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4714767/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 07:40:40  来源:igfitidea点击:

Where should I put logging.properties file for java.util.logging in web application (maven project)?

javaweb-applicationsmavenjava.util.logging

提问by hello_world

I want to logging to file and set it in properties file, because default logger.info() output goes to console and in web application there is no console in my case.

我想记录到文件并将其设置在属性文件中,因为默认的 logger.info() 输出到控制台,而在 Web 应用程序中,我没有控制台。

回答by Victor

As Navi says... it goes in src/main/resources

正如 Navi 所说......它进入 src/main/resources

Just to clarify this subject... the logging.properties mustgo in WEB-INF/classesdirectory. If you are using some kind of framework for organizing your project, you must find out where to place the file in order to stay in WEB-INF/classesIf you are using maven to organize the web app, you must know that everything that lies in src/main/resourcesgoes to WEB-INF/classes.

只是为了澄清这个主题...... logging.properties必须进入WEB-INF/classes目录。如果您正在使用某种形式的组织你的项目框架,你必须找出在何处放置文件,以留在WEB-INF/classes如果你使用Maven组织的Web应用程序,您必须知道的一切,就在于src/main/resourcesWEB-INF/classes.

回答by Navi

you should put in src/main/resources

你应该把 src/main/resources

回答by mdfst13

This is the first place I found when I was trying to figure out where my logging.properties file needed to go while testing.

这是我在尝试找出我的 logging.properties 文件在测试时需要去哪里时找到的第一个地方。

TL;DR: src/test/resources

特尔;博士src/test/resources

Deploy to Tomcat

部署到Tomcat

When running as a web application, as this commentsuggests, I don't need to deploy a logging.properties file to src/main/resources(although that probably would have fixed my immediate problem). My container already has one. For Tomcat, that location is $CATALINA_HOME/conf/logging.properties. If you are running Tomcat from Eclipse, you can set that in launch configuration, as explained here.

当作为 Web 应用程序运行时,正如这条评论所暗示的那样,我不需要将 logging.properties 文件部署到src/main/resources(尽管这可能会解决我的直接问题)。我的容器已经有一个了。对于 Tomcat,该位置是$CATALINA_HOME/conf/logging.properties. 如果您从Eclipse中运行Tomcat,您可以设置在启动配置,如解释在这里

This answertalks about setting up different logging properties per application, which is what putting the file in src/main/resourcesdoes.

这个答案讨论了为每个应用程序设置不同的日志属性,这就是将文件放入的目的src/main/resources

This section is the answer to the poster's question. The rest of this is the answer to my similar but different problem.

本节是对发帖人问题的回答。其余部分是我类似但不同的问题的答案。

Debugging tests

调试测试

The actual problem that I was having was that my java.util.loggingstopped showing the class name and method name after I added an SLF4J-using jar to my project and was trying to debug my unit tests. I had to add org.slf4j:slf4j-jdk14to my POM in Provided scope so that my tests would run at all. But then they didn't have class and method names. Configuring the logging.properties file to match the one for Tomcat but with different handlersfixed that after I added some Surefire configuration:

我遇到的实际问题是,在java.util.logging向项目中添加了使用 SLF4J 的 jar 并尝试调试单元测试后,我停止显示类名和方法名。我必须org.slf4j:slf4j-jdk14在提供的范围内添加到我的 POM 中,以便我的测试能够运行。但是他们没有类名和方法名。配置 logging.properties 文件以匹配 Tomcat 的文件,但使用不同的处理程序修复了我添加一些Surefire 配置后的问题

<plugins>
  <plugin>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
       <systemProperties>
         <property> 
           <name>java.util.logging.config.file</name>
           <value>src/test/resources/logging.properties</value>
         </property>
         <property>
           <name>java.util.logging.manager</name>
           <value>org.apache.juli.ClassLoaderLogManager</value>
         </property>
       </systemProperties>
    </configuration>
  </plugin>
</plugins>

And my logs once again were showing all the information even when running tests from Maven.

即使在从 Maven 运行测试时,我的日志也再次显示了所有信息。

Summary

概括

If you want to set how your regular application is logging, then src/main/resourcesmay be the right place. But I wanted to change how my tests were logging, so I used src/test/resourcesinstead. Since Tomcat's launch configuration doesn't run under typical unit tests, I had to add some configuration to Surefire to serve the same purpose. The logging.properties outside Tomcat does not use any Catalina jars, so I switched it to use different handlers.

如果您想设置常规应用程序的日志记录方式,那么src/main/resources可能是正确的位置。但我想改变我的测试记录方式,所以我src/test/resources改用了。由于 Tomcat 的启动配置不在典型的单元测试下运行,我不得不向 Surefire 添加一些配置以达到相同的目的。Tomcat 外部的 logging.properties 不使用任何 Catalina jar,因此我将其切换为使用不同的处理程序。

回答by dogbane

If you are using Tomcat, then you should log to $CATALINA_HOME/logs/myapp/myapp.log.

如果您使用的是 Tomcat,那么您应该登录到$CATALINA_HOME/logs/myapp/myapp.log.