Uploaded image for project: 'Struts 2'
  1. Struts 2
  2. WW-3988

Commons Logging may not work in XWork from issue WW-3959

    XMLWordPrintableJSON

Details

    Description

      Support for SLF4J in XWork added in WW-3959 can break Commons Logging since it looks for SLF4J in the classpath first. If found, it assumes Struts/XWork is configured to use SLF4J and creates an SLF4J log factory.

      The problem with this approach is that other libraries in a web application may have a dependency on SLF4J, hence it is found in the classpath, even when it is not configured as the logging factory for Struts.

      LoggerFactory.java
      Index: LoggerFactory.java
      ===================================================================
      --- LoggerFactory.java	(revision 911280)
      +++ LoggerFactory.java	(revision 1431482)
      @@ -16,6 +16,7 @@
       package com.opensymphony.xwork2.util.logging;
       
       import com.opensymphony.xwork2.util.logging.jdk.JdkLoggerFactory;
      +import com.opensymphony.xwork2.util.logging.slf4j.Slf4jLoggerFactory;
       
       import java.util.concurrent.locks.ReadWriteLock;
       import java.util.concurrent.locks.ReentrantReadWriteLock;
      @@ -59,11 +60,17 @@
               try {
                   if (factory == null) {
                       try {
      -                    Class.forName("org.apache.commons.logging.LogFactory");
      -                    factory = new com.opensymphony.xwork2.util.logging.commons.CommonsLoggerFactory();
      +                    Class.forName("org.slf4j.LoggerFactory");
      +                    factory = new Slf4jLoggerFactory();
                       } catch (ClassNotFoundException ex) {
      -                    // commons logging not found, falling back to jdk logging
      -                    factory = new JdkLoggerFactory();
      +                    //slf4j not found try commons LogFactory
      +                    try {
      +                        Class.forName("org.apache.commons.logging.LogFactory");
      +                        factory = new com.opensymphony.xwork2.util.logging.commons.CommonsLoggerFactory();
      +                    } catch (ClassNotFoundException cnfex) {
      +                        // commons logging not found, falling back to jdk logging
      +                        factory = new JdkLoggerFactory();
      +                    }
                       }
                   }
                   return factory;
      

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              eb Erik Berg
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: