2 Replies Latest reply on Mar 24, 2015 6:44 AM by John Surveyor

    How to do logging in Wildfly 8.2.0

    John Surveyor Newbie

      I am attempting to teach myself basic J2EE web application development using wildlfy (and struts2 and eclipse). This is proving slightly more ambitious than I had hoped!  My problem is to get my web application to log a message.

       

      I don't mind which logging framework is used, but right now my only option seems to be to roll my own logging class.

       

      Here is a simple action class, which is working apart from the logging in wildfly.

      package com.tutorialspoint.struts2;

       

      import org.apache.log4j.Logger;  // But it doesnt matter what framework

       

      import com.opensymphony.xwork2.ActionSupport;

       

      public class HelloWorldAction extends ActionSupport{

          private static Logger logger = org.apache.log4j.Logger.getLogger("application");

       

          public static void main ( String[] args ) {

              HelloWorldAction h = new HelloWorldAction();

              try {

                  System.out.println( h.execute() );

              } catch (Exception e) {

              }

          }

       

          public String execute() throws Exception {

              logger.info("Inside HelloWorldAction....");       // <------ This the bit that doesnt work

              return SUCCESS;

          }

      }

       

      In my application I have created a WEB-INF/classes/log4j.xml

      <?xml version="1.0" encoding="UTF-8"?>

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

          <appender name="fileAppender" class="org.apache.log4j.RollingFileAppender">

              <param name="append" value="false" />

              <param name="file" value="log4j.log" />

              <layout class="org.apache.log4j.PatternLayout">

                  <param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c{1}] %m%n" />

              </layout>

          </appender>

          <root>

              <level value="DEBUG" />

              <appender-ref ref="fileAppender" />

          </root>

      </log4j:configuration>

       

      When I run the class as a standalone application it produces a log file with the log message. When I deploy the WAR file into wildfly the webapp runs (no problems importing log4j, then) but there is no log file produced.

       

      Next attempt was to add the following to the wildfly-8.2.0-Final/standalone/configuration/standalone.xml

                  <size-rotating-file-handler name="SIZEFILE" autoflush="true">

                      <level name="INFO"/>

                      <file relative-to="jboss.server.log.dir" path="application.log"/>

                      <append value="true"/>

                  </size-rotating-file-handler>

                  <logger category="application" use-parent-handlers="false">

                      <level name="INFO"/>

                      <handlers>

                          <handler name="CONSOLE"/>

                          <handler name="SIZEFILE"/>

                      </handlers>

                  </logger>

      When I restart wildfly I get an empty wildfly-8.2.0-Final/standalone/application.log file created.

       

      - so how can I get log4j to work OR

      - how can I change my java code to call whatever logging class wildfly uses

      - and for that matter how can I just get System.out.println( "Some message" ); to produce some output to the terminal window where I am running stanalone.sh ?

        • 1. Re: How to do logging in Wildfly 8.2.0
          James Perkins Master

          The simplest way to see the log messages on the console would be to just remove your log4j.xml file. Also make sure you're not including a log4j library in your WEB-INF/lib directory. WildFly will automatically add a log4j dependency for you. This should also solve your issue with not seeing System.out.printxx on the console.

           

          --

          James R. Perkins

          • 2. Re: How to do logging in Wildfly 8.2.0
            John Surveyor Newbie

            Thank you. At last I have some log output - I was feeling desperate there for a moment.

             

            I forgot to mention that I also had a looging.properties containing

            org.apache.catalina.core.ContainerBase.[Catalina].level = INFO

            org.apache.catalina.core.ContainerBase.[Catalina].handlers = java.util.logging.ConsoleHandler

             

            I removed that file as well as the log4j.xml.

             

            To summarise the solution to my problem.

            Currently, I am using just the standalone.xml configuration, quoted above and in my java code

            import org.apache.log4j.Logger;

            ...

                  private static final Logger logger= Logger.getLogger("application");

                  ....

                        logger.info( "My log message" );

             

            Possibly another alternative would be to use import org.jboss.logging.Logger;

             

            A final comment: logging in wildfly seems to be really easy but also really easy to do incorrectly. There seems to be a lot of people struggling with it. I am a bit new to this but I don't seem to be alone. Perhaps some tutorial examples are needed (or maybe I just haven't found them).