cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dk...@apache.org
Subject svn commit: r1302936 - in /cxf/branches/2.5.x-fixes/common/common/src/main/java/org/apache/cxf/common/logging: AbstractDelegatingLogger.java LogUtils.java Slf4jLogger.java
Date Tue, 20 Mar 2012 15:24:35 GMT
Author: dkulp
Date: Tue Mar 20 15:24:35 2012
New Revision: 1302936

URL: http://svn.apache.org/viewvc?rev=1302936&view=rev
Log:
Merged revisions 1302574 via  git cherry-pick from
https://svn.apache.org/repos/asf/cxf/trunk

........
  r1302574 | dkulp | 2012-03-19 13:56:40 -0400 (Mon, 19 Mar 2012) | 3 lines

  [CXF-4180] Some work on the SLF4J loggers to help the performance a
  little bit and allow it to be used in more cases.

........

Modified:
    cxf/branches/2.5.x-fixes/common/common/src/main/java/org/apache/cxf/common/logging/AbstractDelegatingLogger.java
    cxf/branches/2.5.x-fixes/common/common/src/main/java/org/apache/cxf/common/logging/LogUtils.java
    cxf/branches/2.5.x-fixes/common/common/src/main/java/org/apache/cxf/common/logging/Slf4jLogger.java

Modified: cxf/branches/2.5.x-fixes/common/common/src/main/java/org/apache/cxf/common/logging/AbstractDelegatingLogger.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.5.x-fixes/common/common/src/main/java/org/apache/cxf/common/logging/AbstractDelegatingLogger.java?rev=1302936&r1=1302935&r2=1302936&view=diff
==============================================================================
--- cxf/branches/2.5.x-fixes/common/common/src/main/java/org/apache/cxf/common/logging/AbstractDelegatingLogger.java
(original)
+++ cxf/branches/2.5.x-fixes/common/common/src/main/java/org/apache/cxf/common/logging/AbstractDelegatingLogger.java
Tue Mar 20 15:24:35 2012
@@ -277,23 +277,45 @@ public abstract class AbstractDelegating
         return level.intValue() >= l.intValue() && l != Level.OFF;
     }
 
+    protected boolean supportsHandlers() {
+        return false;
+    }
+    
     public synchronized void addHandler(Handler handler) throws SecurityException {
+        if (supportsHandlers()) {
+            super.addHandler(handler);
+            return;
+        }
         throw new UnsupportedOperationException();
     }
 
     public synchronized void removeHandler(Handler handler) throws SecurityException {
+        if (supportsHandlers()) {
+            super.removeHandler(handler);
+            return;
+        }
         throw new UnsupportedOperationException();
     }
 
     public synchronized Handler[] getHandlers() {
+        if (supportsHandlers()) {
+            return super.getHandlers();
+        }
         throw new UnsupportedOperationException();
     }
 
     public synchronized void setUseParentHandlers(boolean useParentHandlers) {
+        if (supportsHandlers()) {
+            super.setUseParentHandlers(useParentHandlers);
+            return;
+        }
         throw new UnsupportedOperationException();
     }
 
     public synchronized boolean getUseParentHandlers() {
+        if (supportsHandlers()) {
+            return super.getUseParentHandlers();
+        }
         throw new UnsupportedOperationException();
     }
 

Modified: cxf/branches/2.5.x-fixes/common/common/src/main/java/org/apache/cxf/common/logging/LogUtils.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.5.x-fixes/common/common/src/main/java/org/apache/cxf/common/logging/LogUtils.java?rev=1302936&r1=1302935&r2=1302936&view=diff
==============================================================================
--- cxf/branches/2.5.x-fixes/common/common/src/main/java/org/apache/cxf/common/logging/LogUtils.java
(original)
+++ cxf/branches/2.5.x-fixes/common/common/src/main/java/org/apache/cxf/common/logging/LogUtils.java
Tue Mar 20 15:24:35 2012
@@ -96,17 +96,34 @@ public final class LogUtils {
                 }
             }
             if (StringUtils.isEmpty(cname)) {
-                Class.forName("org.slf4j.impl.StaticLoggerBinder");
-                Class<?> cls = Class.forName("org.slf4j.LoggerFactory");
-                Class<?> fcls = cls.getMethod("getILoggerFactory").invoke(null).getClass();
-                if (fcls.getName().contains("Log4j")) {
-                    cname = "org.apache.cxf.common.logging.Log4jLogger";
-                } else if (fcls.getName().contains("JCL")) {
-                    cls = Class.forName("org.apache.commons.logging.LogFactory");
-                    fcls = cls.getMethod("getFactory").invoke(null).getClass();
-                    if (fcls.getName().contains("Log4j")) {
+                try {
+                    // This Class.forName likely will barf in OSGi, but it's OK
+                    // as we'll just use j.u.l and pax-logging will pick it up fine
+                    // If we don't call this and there isn't a slf4j impl avail,
+                    // you get warnings printed to stderr about NOPLoggers and such
+                    Class.forName("org.slf4j.impl.StaticLoggerBinder");  
+                    Class<?> cls = Class.forName("org.slf4j.LoggerFactory");
+                    Class<?> fcls = cls.getMethod("getILoggerFactory").invoke(null).getClass();
+                    String clsName = fcls.getName();
+                    if (clsName.contains("NOPLogger")) {
+                        //no real slf4j implementation, use j.u.l 
+                        cname = null;
+                    } else if (clsName.contains("Log4j")) {
                         cname = "org.apache.cxf.common.logging.Log4jLogger";
+                    } else if (clsName.contains("JCL")) {
+                        cls = Class.forName("org.apache.commons.logging.LogFactory");
+                        fcls = cls.getMethod("getFactory").invoke(null).getClass();
+                        if (fcls.getName().contains("Log4j")) {
+                            cname = "org.apache.cxf.common.logging.Log4jLogger";
+                        }
+                    } else if (clsName.contains("JDK14")
+                        || clsName.contains("pax.logging")) {
+                        //both of these we can use the appropriate j.u.l API's 
+                        //directly and have it work properly
+                        cname = null;
                     }
+                } catch (Throwable t) {
+                    //ignore - Slf4j not available
                 }
             }
             if (!StringUtils.isEmpty(cname)) {

Modified: cxf/branches/2.5.x-fixes/common/common/src/main/java/org/apache/cxf/common/logging/Slf4jLogger.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.5.x-fixes/common/common/src/main/java/org/apache/cxf/common/logging/Slf4jLogger.java?rev=1302936&r1=1302935&r2=1302936&view=diff
==============================================================================
--- cxf/branches/2.5.x-fixes/common/common/src/main/java/org/apache/cxf/common/logging/Slf4jLogger.java
(original)
+++ cxf/branches/2.5.x-fixes/common/common/src/main/java/org/apache/cxf/common/logging/Slf4jLogger.java
Tue Mar 20 15:24:35 2012
@@ -18,6 +18,7 @@
  */
 package org.apache.cxf.common.logging;
 
+import java.util.logging.Handler;
 import java.util.logging.Level;
 import java.util.logging.LogRecord;
 
@@ -52,6 +53,7 @@ public class Slf4jLogger extends Abstrac
 
     private final org.slf4j.Logger logger;
     private LocationAwareLogger locationAwareLogger;
+    
 
     public Slf4jLogger(String name, String resourceBundleName) {
         super(name, resourceBundleName);
@@ -60,6 +62,10 @@ public class Slf4jLogger extends Abstrac
             locationAwareLogger = (LocationAwareLogger) logger;
         }
     }
+    @Override
+    protected boolean supportsHandlers() {
+        return true;
+    }
 
     @Override
     public Level getLevel() {
@@ -83,11 +89,39 @@ public class Slf4jLogger extends Abstrac
     }
 
     @Override
+    public boolean isLoggable(Level level) {
+        final int i = level.intValue();
+        if (i == Level.OFF.intValue()) {
+            return false;
+        } else if (i >= Level.SEVERE.intValue()) {
+            return logger.isErrorEnabled();
+        } else if (i >= Level.WARNING.intValue()) {
+            return logger.isWarnEnabled();
+        } else if (i >= Level.INFO.intValue()) {
+            return logger.isInfoEnabled();
+        } else if (i >= Level.FINER.intValue()) {
+            return logger.isDebugEnabled();
+        }
+        return logger.isTraceEnabled();
+    }
+
+
+    @Override
     protected void internalLogFormatted(String msg, LogRecord record) {
 
         Level level = record.getLevel();
         Throwable t = record.getThrown();
-
+        
+        Handler targets[] = getHandlers();
+        if (targets != null) {
+            for (Handler h : targets) {
+                h.publish(record);
+            }
+        }
+        if (!getUseParentHandlers()) {
+            return;
+        }
+        
         /*
          * As we can not use a "switch ... case" block but only a "if ... else if ..." block,
the order of the
          * comparisons is important. We first try log level FINE then INFO, WARN, FINER,
etc



Mime
View raw message