开发者

Can't make tomcat-maven-plugin create a log file?

开发者 https://www.devze.com 2023-04-10 01:34 出处:网络
I am running the mvn tomcat:run-war target, and get a directory structure that has a logs directory. But alas, no log. I would just replace this with log4j logging, but this has proven difficult for a

I am running the mvn tomcat:run-war target, and get a directory structure that has a logs directory. But alas, no log. I would just replace this with log4j logging, but this has proven difficult for a variety of reasons.

I have tried explicitly setting the log file configuration. My pom.xml definition currently looks like:

 <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>tomcat-maven-plugin</artifactId>
            <configuration>
                <port>8084</port>
                <systemProperties>
                     <java.util.logging.config.file>${basedir}/src/main/webapp/WEB-INF/logging.properties</java.util.logging.config.file>
                </systemProperties>
            </configuration>
            <version>1.1</version>
  </plugin>

I see during startup that the property is being read. My properties file is below; I am dumping things to /tmp just to be sure I know where to look.

handlers = 1catalina.org.apache.juli.FileHandler, \
           2localhost.org.apache.juli.FileHandler, \
           3manager.org.apache.juli.FileHandler, \
           java.util.logging.ConsoleHandler

.handlers = 1catalina.org.apache.juli.FileHandler, java.util.logging.ConsoleHandler

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

1catalina.org.apache.juli.FileHandler.level = FINE
1catalina.org.apache.juli.FileHandler.directory = /tmp/logs
1catalina.org.apache.juli.FileHandler.prefix = catalina.

2localhost.org.apache.juli.FileHandler.level = FINE
2localhost.org.apache.juli.FileHandler.directory = /tmp/logs
2localhost.org.apache.juli.FileHandler.prefix = localhost.

3manager.org.apache.juli.FileHandler.level = FINE
3manager.org.apache.juli.FileHandler.directory = /tmp/logs
3manager.org.apache.juli.FileHandler.prefix = manager.
3manager.org.apac开发者_开发百科he.juli.FileHandler.bufferSize = 16384

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


############################################################
# Facility specific properties.
# Provides extra control for each logger.
############################################################

org.apache.catalina.core.ContainerBase.[Catalina].[localhost].level = INFO
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].handlers = \
   2localhost.org.apache.juli.FileHandler

org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager].level = INFO
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager].handlers = \
   3manager.org.apache.juli.FileHandler

I would be really grateful if anyone had any thoughts.


I've just been through a similar process, not entirely successfully...

The first thing to note is that you need to be clear if you want to change the logging configuration for your web application, or for Tomcat itself. See logging documentation on the Tomcat website for some details - in particular:

This means that logging can be configured at the following layers:

  • Globally. That is usually done in the ${catalina.base}/conf/logging.properties file. The file is specified by the java.util.logging.config.file System property which is set by the startup scripts. If it is not readable or is not configured, the default is to use the ${java.home}/lib/logging.properties file in the JRE.
  • In the web application. The file will be WEB-INF/classes/logging.properties

Having done this I am able reconfigure my application's logging, when deployed to a standalone Tomcat server. However I was unable to get this to work with the Maven Tomcat plugin - I then discovered that somebody had filed bug MTOMCAT-127 which at the time of writing is unresolved, and would seem to describe what I've been seeing.

So not entirely successful - but I hope I can come back and update this answer once the MTOMCAT-127 issue has progressed...


It's not really a tomcat plugin bug, what happens is if you choose the java api logging for the tomcat maven plugin you should know that there is a maven core library that also uses the java api logging called sisu-guice- 3.2.3-no_aop.jar (class is com.google.inject.internal.util.Stopwatch). This library instantiates the LogManager with the default values ​​of the VM (INFO, etc) but not with the class that Tomcat needs to write its logs org.apache.juli.ClassLoaderLogManager. This class has an attribute called manager which is of type class, not instance. If we want to modify it, we must restart the VM but the Maven library sisu-guice-3.2.3-no_aop.jar will overwrite the LogManager and the RootLogger again.

Regarding the comment:

mvn tomcat7: run -Djava.util.logging.config.file = src / main / webapp / WEB-INF / classes / logging.properties

This is incorrect since this property is going to be set when the LogManager has already been instantiated.

One solution is to copy the mvn.bat to your working directory, but you have to edit it, adding to the classpath the dependency tomcat-embed-logging-juli-7.0.47.jar and the directory where you will keep your logging.properties with this already works that the SystemClassLoader will start it sooner.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号