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

前端 未结 4 1884
天命终不由人
天命终不由人 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: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:

    
      
        maven-surefire-plugin
        
           
              
               java.util.logging.config.file
               src/test/resources/logging.properties
             
             
               java.util.logging.manager
               org.apache.juli.ClassLoaderLogManager
             
           
        
      
    
    

    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.

提交回复
热议问题