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

前端 未结 4 1906
天命终不由人
天命终不由人 2021-02-13 22:59

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.

相关标签:
4条回答
  • 2021-02-13 23:13

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

    Just to clarify this subject... the logging.properties must go in WEB-INF/classes directory. 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.

    0 讨论(0)
  • 2021-02-13 23:18

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

    TL;DR: src/test/resources

    Deploy to 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). 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.

    This answer talks about setting up different logging properties per application, which is what putting the file in src/main/resources does.

    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. I had to add org.slf4j:slf4j-jdk14 to 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 handlers fixed that after I added some Surefire configuration:

    <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.

    Summary

    If you want to set how your regular application is logging, then src/main/resources may be the right place. But I wanted to change how my tests were logging, so I used src/test/resources instead. 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.

    0 讨论(0)
  • 2021-02-13 23:28

    you should put in src/main/resources

    0 讨论(0)
  • 2021-02-13 23:29

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

    0 讨论(0)
提交回复
热议问题