[ https://issues.apache.org/jira/browse/LOG4J2-529?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13894667#comment-13894667 ]
Remko Popma edited comment on LOG4J2-529 at 2/7/14 5:53 PM: ------------------------------------------------------------ Nick, that is a good point. Unfortunately the issue persists even after updating web.xml with the correct version 3.0 header: {code} <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> {code} I've attached the new webapp30.war file to this Jira ticket: It still does not register the MBeans when deploying or unregister them when undeploying. New attachment webapp30-listener.war has the same version=3.0 web-app header but adds listeners and filters. This war file again correctly registers MBeans when deployed and unregisters when undeployed. was (Author: rem...@yahoo.com): Nick, that is a good point. I tested again after updating web.xml with the correct version 3.0 header: {code} <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> {code} Unfortunately the behaviour did not change: It still does not register the MBeans when deploying or unregister them when undeploying. I've attached the new webapp1.war file to this Jira ticket. > Log4j2 does not auto-initialize on webapp deploy or auto-deinitialize on > undeploy > --------------------------------------------------------------------------------- > > Key: LOG4J2-529 > URL: https://issues.apache.org/jira/browse/LOG4J2-529 > Project: Log4j 2 > Issue Type: Bug > Components: Configurators, Core, JMX > Affects Versions: 2.0-beta9, 2.0-rc1 > Environment: Tomcat 7.0.50, Tomcat 8.0.1 > Reporter: Remko Popma > Attachments: webapp2.war > > > The log4j2 manuals > [says|http://logging.apache.org/log4j/2.0/manual/webapp.html#Servlet-3.0] > that "Log4j 2 "just works" in Servlet 3.0 and newer web applications. It is > capable of automatically starting when the application deploys and shutting > down when the application undeploys." > When testing for LOG4J2-500, I found that this does not seem to be the case, > at least on Tomcat 7.0.50 and Tomcat 8.0.1. > *Steps to reproduce the issue* > # Enable JMX in Tomcat: create a file {{setup.bat}} in the {{bin}} directory > with the following contents: {{set > CATALINA_OPTS=-Dcom.sun.management.jmxremote > -Dcom.sun.management.jmxremote.port=54321 > -Dcom.sun.management.jmxremote.ssl=false > -Dcom.sun.management.jmxremote.authenticate=false}} > # To test, I used the Tomcat [sample web > app|http://tomcat.apache.org/tomcat-6.0-doc/appdev/sample/], with the > following modifications: > #* add log4j-api-2.0RC1-SNAPSHOT.jar to WEB-INF/lib > #* add log4j-core-2.0RC1-SNAPSHOT.jar to WEB-INF/lib > #* put log4j2.xml in WEB-INF/classes. Any valid config with > {{status="trace"}} should be sufficient. > #* copy hello.jsp to log.jsp and add these lines: > {code} > <% org.apache.logging.log4j.Logger logger = > org.apache.logging.log4j.LogManager.getLogger(); %> > <% logger.info("Logging from JSP"); %> > <%= new String("Hello! LOGGED FROM JSP OK.") %> > {code} > # create {{webapp1.war}} from the above > # deploy webapp1. Note that in the Tomcat console you will only see "INFO: > Deploying web application archive > D:\apps\apache-tomcat-7.0.50\webapps\webapp1.war", you will *not* see the > Log4J initialization status messages (like "Generated plugins in 0.175750656 > seconds", or "Registering MBean org.apache.logging.log4j2:type=...") > # Open JConsole, and connect to "localhost:54321". In the MBeans tab, you > will *not* see MBeans for {{org.apache.logging.log4j2}}. > # In a browser, open URL http://localhost:8080/webapp1/log.jsp > #* In the Tomcat console you should now see "Generated plugins in 0.000016172 > seconds" and "Registering MBean org.apache.logging.log4j2" > #* In JConsole you should now see MBeans for {{org.apache.logging.log4j2}}. > The LoggerContext name looks something like > {{"org.apache.jasper.servlet.JasperLoader@7da612aa"}}. > # Now, _undeploy_ webapp1. > #* *Bad sign #1:* in the Tomcat console I only see "INFO: Undeploying context > /webapp1", I don't see any status messages for Log4J unloading. For example, > I don't see any "Unregistering MBean ..." messages. > #* *Bad sign #2:* in JConsole, the MBeans for {{org.apache.logging.log4j2}} > are still displayed. Accessing their attributes may cause errors in the > Tomcat console ("INFO: Illegal access: this web application instance has been > stopped already. Could not load > sun.reflect.SerializationConstructorAccessorImpl. The eventual following > stack trace is caused by an error thrown for debugging purposes as well as to > attempt to terminate the thread which caused the illegal access, and has no > functional impact. java.lang.IllegalStateException...") > #* *Bad sign #3:* In the Tomcat manager app, pressing the "Find Leaks" button > shows this message: "The following web applications were stopped (reloaded, > undeployed), but their classes from previous runs are still loaded in memory, > thus causing a memory leak (use a profiler to confirm): /webapp1" > So with the default setup, undeploying a webapp with log4j2 in its > WEB-INF/lib folder does not automatically call {{LoggerContext.stop()}}. > Things do seem to work better if I modify the {{web.xml}} file and add the > listener, filter and filter-mapping [recommended for Servlet 2.5 web > applications|http://logging.apache.org/log4j/2.0/manual/webapp.html#Servlet-2.5]. > # create webapp2.war with the modified {{web.xml}} file > # shutdown and startup Tomcat/7.0.50 > # deploy webapp2.war. *_Do not open log.jsp in a browser yet._* In this case > I immediately see Log4J initialization status messages like "Generated > plugins in 0.000019904 seconds" and "Registering MBean > org.apache.logging.log4j2..." > # open JConsole, and connect to "localhost:54321". In the MBeans tab, I can > see MBeans for {{org.apache.logging.log4j2}}. > #* Interestingly, the LoggerContext name is very different now. It looks like > this: "WebappClassLoader\n context: /webapp2\n delegate: false\n > repositories:\n /WEB-INF/classes/\n----------> Parent > Classloader:\norg.apache.catalina.loader.StandardClassLoader@1ebd2621\n" > {code} > WebappClassLoader > context: /webapp2 > delegate: false > repositories: > /WEB-INF/classes/ > ----------> Parent Classloader: > org.apache.catalina.loader.StandardClassLoader@1ebd2621{code} > # undeploy webapp2 > #* In the Tomcat console, I can see Log4J status messages saying "Stopping > LoggerContext WebappClassLoader...", and "Unregistering MBean > org.apache.logging.log4j2..." > #* In JConsole, the MBeans for {{org.apache.logging.log4j2}} are no longer > displayed. > #* Clicking the "Find Leaks" button in the Tomcat Manager app now shows > message "No web applications appear to have triggered a memory leak on stop, > reload or undeploy." > So, it appears that automatic configuration does not work in Tomcat 7.0.50 > and that listener, filter and filter-mapping in web.xml are necessary for > correct Log4J unloading... -- This message was sent by Atlassian JIRA (v6.1.5#6160) --------------------------------------------------------------------- To unsubscribe, e-mail: log4j-dev-unsubscr...@logging.apache.org For additional commands, e-mail: log4j-dev-h...@logging.apache.org