Decreasing log level in Eclipse WTP with Tomcat 7

2019-04-07 14:46发布

问题:

I am running Tomcat 7 from within Eclipse WTP (Juno) and I cannot seem to tune down the logging level. It logs everything from debug which is too verbose to be of any use (I want it log from INFO). The logging.properties file is as follows:

handlers =  java.util.logging.ConsoleHandler

.handlers = java.util.logging.ConsoleHandler

.level=INFO

############################################################
# Handler specific properties.
# Describes specific configuration info for Handlers.  
############################################################

java.util.logging.ConsoleHandler.level = INFO
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter

回答1:

Adding both of these per the FAQ that Tom Chatt cites:

-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
-Djava.util.logging.config.file="${workspace_loc}/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/conf/logging.properties"

to the arguments to the server's launch configuration will get you what you want. ${workspace_loc} is literal; Eclipse will substitute it with your workspace directory. tmp0 may be tmp1 or such instead; use the same path that's in the server's configuration. Assumes that you've placed the logging.properties file there, of course. That way you'll see the file in the Project Explorer window under Servers. You should start by copying the one from the Tomcat conf directory.

This all assumes that you're intent on using JULI.

Works for me with Tomcat 8.0 and Eclipse Mars. No idea why it didn't work for Tom. I did see ClassNotFounds, for the obvious reason, when I used the wrong class name from someone's post.

I'd advise against the approach of Tom Chatt, since that will affect all of the other uses of that JVM.



回答2:

I had this same issue, wanting to configure the logging levels of Tomcat 7 in Eclipse WTP. I tried putting a logging.properties in the /src directory of my web app. No effect. I tried modifying the logging.properties file in the /conf directory of my Tomcat installation, but no effect there either. I discovered Eclipse WTP's "shadow" Tomcat area, under my workspace directory, in .metadata/.plugins/org.eclipse.wst.server.core/tmp0, and put a logging.properties file in the /conf directory under there. No effect.

I discovered a number of articles (e.g., this FAQ at eclipse.org) noting that while Tomcat in "real life" automatically runs with a logging manager called "JULI", for some reason the Tomcat running inside Eclipse WTP does not run with JULI. I tried adding

-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager

to the VM args in my Tomcat run config, but that just got me ClassNotFound errors. (And yes, I did have tomcat-juli.jar in my classpath, so no idea why it wasn't finding it.)

Finally, it occurred to me to try modifying the logging.properties instance in my jdk jre/lib. Jackpot! It turns out that's the one that does have an effect on the logging done by Tomcat-in-Eclipse. So, the moral of this long tale is that if all you want to do is just simply configure the logging while you're running in Eclipse, with a simple little statement like:

myapp.mypackage.level=FINE

Then $JAVAHOME/lib/logging.properties is the place to put it.



回答3:

To solve this I followed the tomcat log4j guide at http://tomcat.apache.org/tomcat-7.0-doc/logging.html. This will make tomcat use log4j.

Next set up a log4j config in $CATALINA_HOME/lib.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">


   <appender name="null" class="org.apache.log4j.varia.NullAppender" />

   <appender name="console" class="org.apache.log4j.ConsoleAppender">
    <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d %-5p [%c] %m%n" />
    </layout>
</appender>

<category name="org.package.domain">
    <priority value="INFO" />
    <appender-ref ref="console" />
 </category>

 </log4j:configuration>