3 Replies Latest reply on Apr 17, 2012 11:44 AM by jamezp

    NullPointerException with log4j since JBoss 7.1.x

    hd0815

      Hi,

       

      I'm using JBoss 7.1.1.Final and get a NullPointerException when I try to access the Layout of an log4j appender. In JBoss 7.0.2 the same code worked without problems.

       

      Here is an example code snippet

       

      {code}import org.apache.log4j.DailyRollingFileAppender;

      import org.apache.log4j.Level;

      import org.apache.log4j.Logger;

      import org.apache.log4j.PatternLayout;

       

      public class TestLogger {

       

          private Logger logger;

          private final String MYLOGGER = "myLogger";

          private final String MYAPPENDER = "myAppender";

          private final String LOGDIR = System.getProperty("user.home") + System.getProperty("file.separator") + "logs";

       

          public TestLogger() {

              // this works correctly

              logger = Logger.getLogger(MYLOGGER);

              logger.setLevel(Level.DEBUG);

       

              PatternLayout layout = new PatternLayout();

              layout.setConversionPattern("%d %p [%c] - %m%n");

       

              DailyRollingFileAppender appender = new DailyRollingFileAppender(layout, LOGDIR + System.getProperty("file.separator") + "mylog.log", ".yyyy-MM-dd");

              appender.setName(MYAPPENDER);

              logger.addAppender(appender);

       

              // The logger is not null but the appender is since 7.1.x

              // This produces the NullPointer because no appender was found with this name

              // In JBoss 7.0.2 this worked correctly

              logger.getAppender(MYAPPENDER).getLayout();

          }

      }{code}

        • 1. Re: NullPointerException with log4j since JBoss 7.1.x
          jamezp

          If you're using log4j as a log manager, e.g. you're not using the standalone or domain configs, you need to exclude the servers version of log4j and provide your own. Note when doing this though you lose the ability to use any of the consoles to configure your logging.

           

          To exclude the server via the jboss-deployment-structure.xml and include the log4j library in your deployment. You can get more details on doing there here https://docs.jboss.org/author/display/AS71/How+To#HowTo-HowdoIuselog4j.propertiesorlog4j.xmlinsteadofusingtheloggingsubsystemconfiguration%3F

          1 of 1 people found this helpful
          • 2. Re: NullPointerException with log4j since JBoss 7.1.x
            hd0815

            Following the instructions from the linked document brought me to the following result:

             

             

            {noformat}10:47:52,801 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-1) MSC00001: Failed to start service jboss.deployment.unit."testApp.war".STRUCTURE: org.jboss.msc.service.StartException in service jboss.deployment.unit."testApp.war".STRUCTURE: Failed to process phase STRUCTURE of deployment "testApp.war"

                at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:119) [jboss-as-server-7.1.1.Final.jar:7.1.1.Final]

                at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811) [jboss-msc-1.0.2.GA.jar:1.0.2.GA]

                at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746) [jboss-msc-1.0.2.GA.jar:1.0.2.GA]

                at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) [rt.jar:1.6.0_27]

                at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) [rt.jar:1.6.0_27]

                at java.lang.Thread.run(Thread.java:662) [rt.jar:1.6.0_27]

            Caused by: org.jboss.as.server.deployment.DeploymentUnitProcessingException: Error loading jboss-structure.xml from C:\jboss\jboss-as-7.1.1.Final\standalone\deployments\testApp.war\WEB-INF\jboss-deployment-structure.xml

                at org.jboss.as.server.deployment.module.descriptor.DeploymentStructureDescriptorParser.parse(DeploymentStructureDescriptorParser.java:277) [jboss-as-server-7.1.1.Final.jar:7.1.1.Final]

                at org.jboss.as.server.deployment.module.descriptor.DeploymentStructureDescriptorParser.parse(DeploymentStructureDescriptorParser.java:249) [jboss-as-server-7.1.1.Final.jar:7.1.1.Final]

                at org.jboss.as.server.deployment.module.descriptor.DeploymentStructureDescriptorParser.deploy(DeploymentStructureDescriptorParser.java:134) [jboss-as-server-7.1.1.Final.jar:7.1.1.Final]

                at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:113) [jboss-as-server-7.1.1.Final.jar:7.1.1.Final]

                ... 5 more

            Caused by: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[5,13]

            Message: Unexpected content of type 'element start' named 'exclusions'

                at org.jboss.as.server.deployment.module.descriptor.JBossDeploymentStructureParser11.unexpectedContent(JBossDeploymentStructureParser11.java:1098) [jboss-as-server-7.1.1.Final.jar:7.1.1.Final]

                at org.jboss.as.server.deployment.module.descriptor.JBossDeploymentStructureParser11.parseDependencies(JBossDeploymentStructureParser11.java:399) [jboss-as-server-7.1.1.Final.jar:7.1.1.Final]

                at org.jboss.as.server.deployment.module.descriptor.JBossDeploymentStructureParser11.parseModuleStructureSpec(JBossDeploymentStructureParser11.java:316) [jboss-as-server-7.1.1.Final.jar:7.1.1.Final]

                at org.jboss.as.server.deployment.module.descriptor.JBossDeploymentStructureParser11.parseDeployment(JBossDeploymentStructureParser11.java:234) [jboss-as-server-7.1.1.Final.jar:7.1.1.Final]

                at org.jboss.as.server.deployment.module.descriptor.JBossDeploymentStructureParser11.readElement(JBossDeploymentStructureParser11.java:211) [jboss-as-server-7.1.1.Final.jar:7.1.1.Final]

                at org.jboss.as.server.deployment.module.descriptor.JBossDeploymentStructureParser11.readElement(JBossDeploymentStructureParser11.java:42) [jboss-as-server-7.1.1.Final.jar:7.1.1.Final]

                at org.jboss.staxmapper.XMLMapperImpl.processNested(XMLMapperImpl.java:110)

                at org.jboss.staxmapper.XMLMapperImpl.parseDocument(XMLMapperImpl.java:69)

                at org.jboss.as.server.deployment.module.descriptor.DeploymentStructureDescriptorParser.parse(DeploymentStructureDescriptorParser.java:271) [jboss-as-server-7.1.1.Final.jar:7.1.1.Final]

                ... 8 more{noformat}

             

            I guess the description in the document ist wrong.

            I changed the jboss-deployment-structure.xml to the following and now the NullPointerException is gone:

             

             

            {code:xml}

            <jboss-deployment-structure>

                <deployment>

                    <!-- Exclusions allow you to prevent the server from automatically adding

                        some dependencies -->

                    <exclusions>

                        <module name="org.apache.log4j" />

                    </exclusions>

                </deployment>

            </jboss-deployment-structure>{code}

            • 3. Re: NullPointerException with log4j since JBoss 7.1.x
              jamezp

              Thanks for catching the invalid XML. I've updated the documentation to reflect the correct sample.