[seam-commits] Seam SVN: r7438 - trunk/ui/src/main/java/org/jboss/seam/ui/facelet.

seam-commits at lists.jboss.org seam-commits at lists.jboss.org
Tue Feb 19 08:07:02 EST 2008


Author: pete.muir at jboss.org
Date: 2008-02-19 08:07:02 -0500 (Tue, 19 Feb 2008)
New Revision: 7438

Added:
   trunk/ui/src/main/java/org/jboss/seam/ui/facelet/FaceletsJBossLogging.java
   trunk/ui/src/main/java/org/jboss/seam/ui/facelet/Log4JConversionFilter.java
Log:
Facelets jdk logging -> log4j bridge for jboss

Added: trunk/ui/src/main/java/org/jboss/seam/ui/facelet/FaceletsJBossLogging.java
===================================================================
--- trunk/ui/src/main/java/org/jboss/seam/ui/facelet/FaceletsJBossLogging.java	                        (rev 0)
+++ trunk/ui/src/main/java/org/jboss/seam/ui/facelet/FaceletsJBossLogging.java	2008-02-19 13:07:02 UTC (rev 7438)
@@ -0,0 +1,129 @@
+package org.jboss.seam.ui.facelet;
+
+import static org.jboss.seam.ScopeType.APPLICATION;
+import static org.jboss.seam.annotations.Install.BUILT_IN;
+
+import java.lang.reflect.Field;
+import java.util.logging.Filter;
+import java.util.logging.Logger;
+
+import org.apache.log4j.Level;
+import org.jboss.seam.annotations.Create;
+import org.jboss.seam.annotations.Install;
+import org.jboss.seam.annotations.Name;
+import org.jboss.seam.annotations.Scope;
+import org.jboss.seam.annotations.Startup;
+import org.jboss.seam.annotations.intercept.BypassInterceptors;
+import org.jboss.seam.log.LogProvider;
+import org.jboss.seam.log.Logging;
+import org.jboss.seam.util.Reflections;
+
+import com.sun.facelets.FaceletViewHandler;
+import com.sun.facelets.TemplateClient;
+import com.sun.facelets.compiler.TagLibraryConfig;
+import com.sun.facelets.el.LegacyELContext;
+import com.sun.facelets.impl.DefaultFaceletFactory;
+import com.sun.facelets.tag.jsf.ComponentHandler;
+import com.sun.facelets.tag.jsf.core.ViewHandler;
+import com.sun.facelets.tag.ui.CompositionHandler;
+import com.sun.facelets.tag.ui.DecorateHandler;
+import com.sun.facelets.util.Classpath;
+import com.sun.facelets.util.DevTools;
+import com.sun.facelets.util.Resource;
+
+ at Name("org.jboss.seam.ui.facelet.faceletsJBossLogging")
+ at Scope(APPLICATION)
+ at Install(classDependencies={"com.sun.facelets.Facelet", "org.jboss.logging.Logger", "org.apache.log4j.Logger"}, precedence=BUILT_IN)
+ at Startup
+ at BypassInterceptors
+public class FaceletsJBossLogging
+{
+   
+   private LogProvider log = Logging.getLogProvider(FaceletsJBossLogging.class);
+
+   public static void setLevel(java.util.logging.Logger julLogger)
+   {
+       org.apache.log4j.Logger log4jLogger = 
+           org.apache.log4j.Logger.getLogger(julLogger.getName());
+
+       julLogger.setLevel(java.util.logging.Level.OFF);
+
+       if (log4jLogger.isEnabledFor(Level.FATAL))
+           julLogger.setLevel(java.util.logging.Level.SEVERE);
+
+       if (log4jLogger.isEnabledFor(Level.ERROR))
+           julLogger.setLevel(java.util.logging.Level.SEVERE);
+
+       if (log4jLogger.isEnabledFor(Level.WARN))
+           julLogger.setLevel(java.util.logging.Level.WARNING);
+
+       if (log4jLogger.isEnabledFor(Level.INFO))
+           julLogger.setLevel(java.util.logging.Level.INFO);
+
+       if (log4jLogger.isEnabledFor(Level.DEBUG)) 
+           julLogger.setLevel(java.util.logging.Level.FINE);
+
+       if (log4jLogger.isEnabledFor(Level.DEBUG))
+           julLogger.setLevel(java.util.logging.Level.FINEST);
+
+       if (log4jLogger.isEnabledFor(Level.ALL))
+           julLogger.setLevel(java.util.logging.Level.ALL);
+   }
+   
+   @SuppressWarnings("deprecation")
+   @Create
+   public void create()
+   {
+      try
+      {
+         Filter conversionFilter = new org.jboss.seam.ui.facelet.Log4JConversionFilter();
+   
+         java.util.logging.Logger julLogger;
+   
+         // Gah have to do this by reflection as the loggers are protected
+         
+         // And some aren't static, so this really is best effort
+         
+         julLogger = getPrivateStaticLogger(TagLibraryConfig.class, "log");
+         setLevel(julLogger);
+         julLogger.setFilter(conversionFilter);
+         
+         julLogger = getPrivateStaticLogger(com.sun.facelets.compiler.Compiler.class, "log");
+         setLevel(julLogger);
+         julLogger.setFilter(conversionFilter);
+         
+         julLogger = getPrivateStaticLogger(DefaultFaceletFactory.class, "log");
+         setLevel(julLogger);
+         julLogger.setFilter(conversionFilter);
+         
+         julLogger = getPrivateStaticLogger(TagLibraryConfig.class, "log");
+         setLevel(julLogger);
+         julLogger.setFilter(conversionFilter);
+         
+         julLogger = getPrivateStaticLogger(ComponentHandler.class, "log");
+         setLevel(julLogger);
+         julLogger.setFilter(conversionFilter);
+         
+         julLogger = getPrivateStaticLogger(Resource.class, "log");
+         setLevel(julLogger);
+         julLogger.setFilter(conversionFilter);
+         
+         julLogger = getPrivateStaticLogger(FaceletViewHandler.class, "log");
+         setLevel(julLogger);
+         julLogger.setFilter(conversionFilter);
+         
+      }
+      catch (Exception e)
+      {
+         log.warn("Unable to wrap Facelets JDK logging in Log4j logging", e);
+      }
+   }
+   
+   private Logger getPrivateStaticLogger(Class clazz, String fieldName)
+   {
+      Field field = Reflections.getField(clazz, fieldName);
+      field.setAccessible(true);
+      return (Logger) Reflections.getAndWrap(field, new Object());
+   }
+   
+}


Property changes on: trunk/ui/src/main/java/org/jboss/seam/ui/facelet/FaceletsJBossLogging.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain

Added: trunk/ui/src/main/java/org/jboss/seam/ui/facelet/Log4JConversionFilter.java
===================================================================
--- trunk/ui/src/main/java/org/jboss/seam/ui/facelet/Log4JConversionFilter.java	                        (rev 0)
+++ trunk/ui/src/main/java/org/jboss/seam/ui/facelet/Log4JConversionFilter.java	2008-02-19 13:07:02 UTC (rev 7438)
@@ -0,0 +1,167 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2007, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.seam.ui.facelet;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.logging.Filter;
+import java.util.logging.Formatter;
+import java.util.logging.Level;
+import java.util.logging.LogRecord;
+import java.util.logging.SimpleFormatter;
+import org.jboss.logging.Logger;
+
+/**
+ * This filter is used to convert java.util.logging messages to Log4J messages.
+ *
+ * @author Stan Silvert
+ * @author Pete Muir
+ */
+public class Log4JConversionFilter implements Filter 
+{
+    
+    // cache Logger instances.  Logger.getLogger() is known to be slow.
+    // See http://www.qos.ch/logging/thinkAgain.jsp
+    private Map<String, Logger> loggerCache = new HashMap<String, Logger>();
+    
+    private Formatter formatter = new SimpleFormatter();
+    
+    /**
+     * If the message should be logged, convert the JDK 1.4 
+     * LogRecord to a Log4J message.
+     *
+     * @return <code>false</code> because JDK 1.4 logging should not happen
+     *         for JSF if this filter is active.
+     */
+    public boolean isLoggable(LogRecord record) 
+    {   
+        Logger logger = getLogger(record);
+        
+        if (record.getThrown() != null) 
+        {
+            logWithThrowable(logger, record);
+        } 
+        else 
+        {
+            logWithoutThrowable(logger, record);
+        }
+        
+        return false;
+    }
+    
+    private void logWithThrowable(Logger logger, LogRecord record) 
+    {
+        int loggedLevel = record.getLevel().intValue();
+        Object message = formatter.formatMessage(record);
+        Throwable throwable = record.getThrown();
+        
+        if (loggedLevel == Level.SEVERE.intValue())
+        {
+           logger.error(message, throwable);
+           return;
+        }
+
+        if (loggedLevel == Level.WARNING.intValue())
+        {
+           logger.warn(message, throwable);
+           return;
+        }
+
+        if ((loggedLevel == Level.INFO.intValue()) ||
+            (loggedLevel == Level.CONFIG.intValue()))
+        {
+           logger.info(message, throwable);
+           return;
+        }
+        
+        if (loggedLevel == Level.FINE.intValue())
+        {
+           logger.debug(message, throwable);
+           return;
+        }
+
+        if ((loggedLevel == Level.FINER.intValue()) ||
+            (loggedLevel == Level.FINEST.intValue()))
+        {
+           logger.trace(message, throwable);
+           return;
+        }
+    
+        logger.info(message, throwable);
+    }
+    
+    private void logWithoutThrowable(Logger logger, LogRecord record) 
+    {
+        int loggedLevel = record.getLevel().intValue();
+        Object message = formatter.formatMessage(record);
+        
+        if (loggedLevel == Level.SEVERE.intValue())
+        {
+           logger.error(message);
+           return;
+        }
+
+        if (loggedLevel == Level.WARNING.intValue())
+        {
+           logger.warn(message);
+           return;
+        }
+
+        if ((loggedLevel == Level.INFO.intValue()) ||
+            (loggedLevel == Level.CONFIG.intValue()))
+        {
+           logger.info(message);
+           return;
+        }
+        
+        if (loggedLevel == Level.FINE.intValue())
+        {
+           logger.debug(message);
+           return;
+        }
+
+        if ((loggedLevel == Level.FINER.intValue()) ||
+            (loggedLevel == Level.FINEST.intValue()))
+        {
+           logger.trace(message);
+           return;
+        }
+
+        logger.info(message);
+    }
+    
+    // get the Log4J logger corresponding to the java.util.logger.LogRecord
+    private Logger getLogger(LogRecord record) 
+    {
+        String loggerName = record.getLoggerName();
+        Logger logger = loggerCache.get(loggerName);
+        if (logger == null) 
+        {
+            logger = Logger.getLogger(loggerName);
+            loggerCache.put(loggerName, logger);
+        }
+        
+        return logger;
+    }
+    
+}


Property changes on: trunk/ui/src/main/java/org/jboss/seam/ui/facelet/Log4JConversionFilter.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain




More information about the seam-commits mailing list