1 2 3 Previous Next 37 Replies Latest reply on Dec 6, 2012 5:39 PM by jamezp

    How to setup log4j in JBoss AS7

    davidj

      Hi,

       

      Can someone step me thru how to setup log4j on JBoss AS7?

      I want the easiest way possible (not some fancy solution which requires me to modify all sorts of JBoss configuration and module files/directories).

      I've tried the following.

       

      I have an EJB Jar file which has a "log4j.properties" file. This file has the usual stuff in it.... nothing fancy, just the same old stuff you always put in this file. In-fact, I'll past it below:

       

      log4j.rootLogger=TRACE, stdout, R

       

      # Define stdout appender

      # =============================================

      log4j.appender.stdout=org.apache.log4j.ConsoleAppender

      log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

      log4j.appender.stdout.layout.ConversionPattern=[%d{MM-dd-yyyy hh:mm:ss}] [%c] [%-5p] %m%n

       

      #Define R appender

      # =============================================

      log4j.appender.R=org.apache.log4j.RollingFileAppender

      log4j.appender.R.File=myLogger.log

      log4j.appender.R.MaxFileSize=9216KB

      # Keep one backup file

      log4j.appender.R.MaxBackupIndex=1

      log4j.appender.R.layout=org.apache.log4j.PatternLayout

      log4j.appender.R.layout.ConversionPattern=[%d{MMM-dd hh:mm:ss}] [%c{1}] %m%n

       

      This file is located in my "src/main/resources" folder (just like it should be since this is a Maven project)

      This same JAR file will log correctly on other AppServers (such as Glassfish).

      Will not work on JBoss AS7 (it will not log anything; it won't even create a "myLogger.log" file).

       

      I read else where that I must do the following in my "standalone.xml" file:

      <size-rotating-file-handler name="MY_LOGGER">

          <level name="TRACE"/>

          <formatter>

              <pattern-formatter pattern="%d{MMM-dd hh:mm:ss} [%c{1}] %m%n"/>

          </formatter>

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

          <rotate-size value="9216K"/>

          <max-backup-index value="3"/>

      </size-rotating-file-handler>


      <logger category="com.mycompany">

          <level name="TRACE"/>

          <handlers>

              <handler name="MY_LOGGER"/>

          </handlers>

      </logger>

       

      I tried that and it still doesn't work

       

      Any help would be appreciated. Thanks.

       

      As a side note, the JAR is actually placed inside of an EAR file, and the EAR file is what actually gets deployed to JBoss.  I'm not sure if this make a difference.

        • 1. Re: How to setup log4j in JBoss AS7
          jamezp

          In JBoss 7.1.x.Final you need to exclude the log4j server dependency, https://docs.jboss.org/author/display/AS71/How+To#HowTo-HowdoIuselog4j.propertiesorlog4j.xmlinsteadofusingtheloggingsubsystemconfiguration%3F. In future versions and EAP you will have to do nothing. Your configuration file will be found and logging for your deployment will be configured using your configuration file.

           

          --

          James R. Perkins

          • 2. Re: How to setup log4j in JBoss AS7
            davidj

            Hi,

             

            I tried both ways and neither one worked.

             

            Below is the appropriate contents of my standalone.xml file:

            <size-rotating-file-handler name="wms-logger">

                <level name="INFO"/>

                <formatter>

                    <pattern-formatter pattern="%d{MMM.dd hh:mm:ss} [%c{1}] %m%n"/>

                </formatter>

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

                <rotate-size value="9216K"/>

                <max-backup-index value="3"/>

            </size-rotating-file-handler>

            <logger category="com.mycompany" use-parent-handlers="true">

                <handlers>

                    <handler name="wms-logger"/>

                </handlers>

            </logger>

             

             

            Nothing special, I'm just created a rolling file called "wms-ear.log".

             

            I also tried the other method (for using log4j).  I followed the directions exactly as stated (log4j.properties in "lib" and that jboss file in the META-INF).

             

            Is there any other way? Even if it's ugly.

            • 3. Re: How to setup log4j in JBoss AS7
              jamezp

              That configuration, your standalone.xml, should work. You should see any logged through com.mycompany.* sent to your wms-ear.log file was well as the console and server log handler assuming you didn't remove them of course.

               

              Also make sure you are not including a log4j library in your deployment if you want to use the logging subsystem configuration.

               

              --

              James R. Perkins

              • 4. Re: How to setup log4j in JBoss AS7
                davidj

                Are you saying to put the log4j dependency in pom.xml to scope "runtime" to avoid the log4j.jar file from being included in the JAR/EAR file?

                • 5. Re: How to setup log4j in JBoss AS7
                  jamezp

                  Not "runtime", but "provided". Maven makes that confusing IMO :-)

                   

                  --

                  James R. Perkins

                  • 6. Re: How to setup log4j in JBoss AS7
                    davidj

                    As it turns-out, I already had that dependency set to "provided".

                     

                    For fun, I modified the "root-logger" of standalone.xml section to be this:

                    <root-logger>

                        <level name="INFO"/>

                        <handlers>

                            <handler name="CONSOLE"/>

                            <handler name="FILE"/>

                           <handler name="wms-logger"/>

                        </handlers>

                    </root-logger>

                     

                    All I did was add "wms-logger" to the list.

                    And now everything which JBoss had been logging to "server.log" is also going to "wms-ear.log".  This is useful because it proves jboss is seeing my "size-rotating-file-handler" entry.  But the bad news is that still none of my project's logging statements are there.

                     

                    Does this spark any ideas?

                    • 7. Re: How to setup log4j in JBoss AS7
                      jamezp

                      Not really, it seems it should just be working. The only thing I can think of either no INFO or higher messages are being logged or the category has a typo. Those are both just a guess though.

                       

                      What version of JBoss AS are you using? I've done testing using log4j and logging into a separate file with no problems before.

                       

                      --

                      James R. Perkins

                      • 8. Re: How to setup log4j in JBoss AS7
                        davidj

                        I'm using JBoss AS 7.1.1.Final.

                         

                        To make sure I don't have any typos, I'll attempt to create another logger/category with words like "foo" (because then even bad typers like me can't mess-up).

                        • 9. Re: How to setup log4j in JBoss AS7
                          jamezp

                          One more thing to try too is setting the auto-flush attribute to true, <size-rotating-file-handler name="wms-logger" auto-flush="true">.

                           

                          --

                          James R. Perkins

                          • 10. Re: How to setup log4j in JBoss AS7
                            ybxiang.china

                            I think JBoss logging is good enough, and I will NOT use log4j in jboss 7+ any more.

                            • 11. Re: How to setup log4j in JBoss AS7
                              davidj

                              Hi xiang yingbing,

                               

                              Right now I'll take anything which will work.

                               

                              Can you explain to me  how you got jBoss logging to work?

                               

                              And if you don't use log4j, what do you use instead?

                               

                              For example, in your Java code you do this:

                                   log.info("this is a log message");

                               

                              What do you import in-order to get "log.info()" to compile?     e.g.   import org.apache.log4j.Logger;

                               

                              Thanks.

                              • 12. Re: How to setup log4j in JBoss AS7
                                jamezp

                                Using JBoss Logging is definitely an option. Using log4j should just work too, I'm not sure why it's not. If you really want to switch to JBoss Logging it's just a facade similar to slf4j. All you would need to do is change your org.apache.log4j.Logger imports with org.jboss.logging.Logger. The methods, IIRC, should be the same plus you get varargs as an add bonus. So you could do something like:

                                 

                                org.jboss.logging.Logger.getLogger("com.mycompany").info("Hello %s", name);
                                

                                 

                                Since JBoss Logging is just a facade too you can use it with log4j as the log manager, JUL as the log manager, JBoss Log Manager or logback as the log manager.

                                 

                                --

                                James R. Perkins

                                • 13. Re: How to setup log4j in JBoss AS7
                                  ybxiang.china

                                  Hi Jensen,

                                   

                                            It is VERY VERY easy!

                                   

                                   

                                  1. standalone.xml

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

                                   

                                  <server xmlns="urn:jboss:domain:1.4">

                                   

                                      <extensions>

                                          ...

                                          <extension module="org.jboss.as.logging"/>

                                          ...

                                      </extensions>

                                     

                                      <profile>

                                          <subsystem xmlns="urn:jboss:domain:logging:1.1">

                                              <console-handler name="CONSOLE">

                                                  <level name="INFO"/>

                                                  <formatter>

                                                      <pattern-formatter pattern="%K{level}%d{HH:mm:ss,SSS} %-5p [%c] (%t) %s%E%n"/>

                                                  </formatter>

                                              </console-handler>

                                              <periodic-rotating-file-handler name="FILE">

                                                  <formatter>

                                                      <pattern-formatter pattern="%d{HH:mm:ss,SSS} %-5p [%c] (%t) %s%E%n"/>

                                                  </formatter>

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

                                                  <suffix value=".yyyy-MM-dd"/>

                                                  <append value="true"/>

                                              </periodic-rotating-file-handler>

                                              <periodic-rotating-file-handler name="SOCKETTRACING">

                                                  <formatter>

                                                      <pattern-formatter pattern="%d{HH:mm:ss,SSS} %s%E%n"/>

                                                  </formatter>

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

                                                  <suffix value=".yyyy-MM-dd"/>

                                                  <append value="true"/>

                                              </periodic-rotating-file-handler>

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

                                                  <level name="DEBUG"/>

                                                  <handlers>

                                                      <handler name="SOCKETTRACING"/>

                                                  </handlers>

                                              </logger>

                                              <logger category="com.arjuna">

                                                  <level name="WARN"/>

                                              </logger>

                                              <logger category="org.apache.tomcat.util.modeler">

                                                  <level name="WARN"/>

                                              </logger>

                                              <logger category="sun.rmi">

                                                  <level name="WARN"/>

                                              </logger>

                                              <logger category="jacorb">

                                                  <level name="WARN"/>

                                              </logger>

                                              <logger category="jacorb.config">

                                                  <level name="ERROR"/>

                                              </logger>

                                              <root-logger>

                                                  <level name="INFO"/>

                                                  <handlers>

                                                      <handler name="CONSOLE"/>

                                                      <handler name="FILE"/>

                                                  </handlers>

                                              </root-logger>

                                          </subsystem>

                                          ...

                                      </profile>

                                      ...

                                  </server>

                                   

                                   

                                   

                                  2. Your Java code:

                                   

                                  import org.jboss.logging.Logger;

                                   

                                  @Local(IGprsServerSocketService.class)

                                  @Singleton

                                  @Startup

                                  public class GprsServerSocketService implements IGprsServerSocketService{

                                      private static final Logger log = Logger.getLogger(GprsServerSocketService.class);

                                      private static final Logger socket_log = Logger.getLogger("SOCKET_TRACING");

                                     

                                      public void start() throws Exception {

                                          log.info("==>starting...");

                                      }

                                      public void printSocketLog() throws Exception {

                                          socket_log.info("==>starting...");

                                      }

                                  }

                                   

                                   

                                   

                                  Nothing else!

                                  • 14. Re: How to setup log4j in JBoss AS7
                                    ybxiang.china

                                    Your log is here:

                                     

                                    jboss-as-7.2.0.Alpha1\standalone\log\server.log

                                     

                                    or

                                     

                                    jboss-as-7.2.0.Alpha1\standalone\log\sockettracing.log

                                     

                                     

                                     

                                     

                                     

                                    Please tell me your result.

                                    1 2 3 Previous Next