简体   繁体   English

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

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

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

As Navi says... it goes in src/main/resources 正如Navi所说......它在src/main/resources

Just to clarify this subject... the logging.properties must go in WEB-INF/classes directory. 只是为了澄清这个主题...... logging.properties 必须放在WEB-INF/classes目录中。 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/classes If you are using maven to organize the web app, you must know that everything that lies in src/main/resources goes to WEB-INF/classes. 如果您使用某种框架来组织项目,您必须找到放置文件的位置以便保留在WEB-INF/classes如果您使用maven来组织Web应用程序,您必须知道所有的谎言在src/main/resources转到WEB-INF/classes.

你应该放入src / main / resources

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 TL; DRsrc/test/resources

Deploy to Tomcat 部署到Tomcat

When running as a web application, as this comment suggests, I don't need to deploy a logging.properties file to src/main/resources (although that probably would have fixed my immediate problem). 当作为Web应用程序运行时,正如此评论所示,我不需要将logging.properties文件部署到src/main/resources (尽管这可能已经修复了我的直接问题)。 My container already has one. 我的容器已经有一个。 For Tomcat, that location is $CATALINA_HOME/conf/logging.properties . 对于Tomcat,该位置是$CATALINA_HOME/conf/logging.properties If you are running Tomcat from Eclipse, you can set that in launch configuration, as explained here . 如果您从Eclipse中运行Tomcat,您可以设置在启动配置,如解释在这里

This answer talks about setting up different logging properties per application, which is what putting the file in src/main/resources does. 这个答案讨论了为每个应用程序设置不同的日志记录属性,这是将文件放在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.logging stopped 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. 我遇到的实际问题是我的java.util.logging停止显示类名和方法名后我将SLF4J使用jar添加到我的项目并尝试调试我的单元测试。 I had to add org.slf4j:slf4j-jdk14 to my POM in Provided scope so that my tests would run at all. 我必须将org.slf4j:slf4j-jdk14添加到我提供的范围内的POM中,以便我的测试可以运行。 But then they didn't have class and method names. 但后来他们没有类和方法名称。 Configuring the logging.properties file to match the one for Tomcat but with different handlers fixed that after I added some Surefire configuration : 在添加一些Surefire配置后,配置logging.properties文件以匹配Tomcat,但修复了不同的处理程序

<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/resources may be the right place. 如果要设置常规应用程序的日志记录方式,则src/main/resources可能是正确的位置。 But I wanted to change how my tests were logging, so I used src/test/resources instead. 但我想改变我的测试记录方式,所以我改用了src/test/resources 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. 由于Tomcat的启动配置不能在典型的单元测试下运行,因此我不得不向Surefire添加一些配置以实现相同的目的。 The logging.properties outside Tomcat does not use any Catalina jars, so I switched it to use different handlers. Tomcat外部的logging.properties不使用任何Catalina jar,因此我将其切换为使用不同的处理程序。

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

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

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