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

4

15

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.

Goiter answered 17/1, 2011 at 15:21 Comment(0)
M
19

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.

Mahlstick answered 16/1, 2013 at 15:30 Comment(0)
H
11

you should put in src/main/resources

Haas answered 17/1, 2011 at 15:30 Comment(4)
This is the correct answer to the question and I've up-voted it as such. However, is it a good practice to include properties files like this in the .jar/.war/.ear/etc... for an application? Shouldn't the application provide a sample configuration (or at most a default one) and allow the installer to configure the logging on their system however they like? I realize this is an open ended question, so what's the accepted best practice? (assuming there is one)Beebeebe
and where and what should I place instead of this: java -Djava.util.logging.config.file=mylogging.properties ? in tomcat 6Goiter
I see that by default (no own logging.properties) tomcat6 send default java.util.logging log.info() to file /var/log/tomcat6/catalina-(date)-.log This must be enough for me now, later I'll read this tomcat.apache.org/tomcat-6.0-doc/…Goiter
AFAIK the logging.properties file won't be read from the classpath, so just putting it into src/main/resources won't do.Deadhead
S
0

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.

Supralapsarian answered 13/10, 2018 at 7:51 Comment(0)
F
-7

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

Farrar answered 17/1, 2011 at 15:26 Comment(2)
That doesn't answer the question.Gera
@the-elite-gentleman I'll let the OP be the judge of that. As far as I can tell, the OP wants to know where to log to, since he can't log to console in a webapp. I will happily delete/edit if I misunderstood the question.Farrar

© 2022 - 2024 — McMap. All rights reserved.