You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by lo...@apache.org on 2015/02/02 17:13:19 UTC

svn commit: r1656507 - in /myfaces/tobago/trunk/tobago-example/tobago-example-demo/src/main: java/org/apache/myfaces/tobago/example/demo/ webapp/ webapp/WEB-INF/ webapp/WEB-INF/tags/layout/

Author: lofwyr
Date: Mon Feb  2 16:13:19 2015
New Revision: 1656507

URL: http://svn.apache.org/r1656507
Log:
show logging information dynamically for several logging frameworks and test the logging

Added:
    myfaces/tobago/trunk/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/UniversalLoggingInfo.java
    myfaces/tobago/trunk/tobago-example/tobago-example-demo/src/main/webapp/logging-info.xhtml
Modified:
    myfaces/tobago/trunk/tobago-example/tobago-example-demo/src/main/webapp/WEB-INF/faces-config.xml
    myfaces/tobago/trunk/tobago-example/tobago-example-demo/src/main/webapp/WEB-INF/tags/layout/overview.tag
    myfaces/tobago/trunk/tobago-example/tobago-example-demo/src/main/webapp/menu.xhtml

Added: myfaces/tobago/trunk/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/UniversalLoggingInfo.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/UniversalLoggingInfo.java?rev=1656507&view=auto
==============================================================================
--- myfaces/tobago/trunk/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/UniversalLoggingInfo.java (added)
+++ myfaces/tobago/trunk/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/UniversalLoggingInfo.java Mon Feb  2 16:13:19 2015
@@ -0,0 +1,251 @@
+package org.apache.myfaces.tobago.example.demo;
+
+import javax.enterprise.context.ApplicationScoped;
+import javax.inject.Named;
+import java.io.PrintStream;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
+/**
+ * Provides the possibility to get information about various logging APIs available in the current setup.
+ */
+@Named
+@ApplicationScoped
+public class UniversalLoggingInfo {
+
+  private static final PrintStream LOG = System.err;
+
+  public static final String JUL = "JUL";
+  public static final String SLF4J = "SLF4J";
+  public static final String LOG4J = "LOG4J";
+  public static final String LOG4J2 = "LOG4J2";
+  public static final String JCL = "JCL";
+
+  /**
+   * should always be available, because it's part of Java since 1.4
+   */
+  private LoggingInfo jul;
+
+  /**
+   * should always be true, because it's used by Tobago
+   */
+  private LoggingInfo slf4j;
+
+  private LoggingInfo log4j;
+
+  private LoggingInfo log4j2;
+
+  private LoggingInfo commonsLogging;
+
+  private String testCategory = UniversalLoggingInfo.class.getName();
+
+  public UniversalLoggingInfo() {
+    jul = new LoggingInfo(JUL, "java.util.logging.Logger", "getLogger", testCategory, true,
+        "finest",
+        "finer",
+        "fine",
+        "config",
+        "info",
+        "warning",
+        "severe");
+    slf4j = new LoggingInfo(SLF4J, "org.slf4j.LoggerFactory", "getLogger", testCategory, true,
+        "trace",
+        "debug",
+        "info",
+        "warn",
+        "error");
+    log4j = new LoggingInfo(LOG4J, "org.apache.log4j.Logger", "getLogger", testCategory, false,
+        "trace",
+        "debug",
+        "info",
+        "warn",
+        "error",
+        "fatal");
+    log4j2 = new LoggingInfo(LOG4J2, "org.apache.logging.log4j.LogManager", "getLogger", testCategory, false,
+        "trace",
+        "debug",
+        "info",
+        "warn",
+        "error",
+        "fatal");
+    commonsLogging = new LoggingInfo(JCL, "org.apache.commons.logging.LogFactory", "getLog", testCategory, false,
+        "trace",
+        "debug",
+        "info",
+        "warn",
+        "error",
+        "fatal");
+    update();
+  }
+
+  public String update() {
+    jul.reset(testCategory);
+    slf4j.reset(testCategory);
+    log4j.reset(testCategory);
+    log4j2.reset(testCategory);
+    commonsLogging.reset(testCategory);
+    return null;
+  }
+
+  public LoggingInfo getJul() {
+    return jul;
+  }
+
+  public LoggingInfo getSlf4j() {
+    return slf4j;
+  }
+
+  public LoggingInfo getLog4j() {
+    return log4j;
+  }
+
+  public LoggingInfo getLog4j2() {
+    return log4j2;
+  }
+
+  public LoggingInfo getCommonsLogging() {
+    return commonsLogging;
+  }
+
+  public String getTestCategory() {
+    return testCategory;
+  }
+
+  public void setTestCategory(String testCategory) {
+    if (testCategory != null) {
+      this.testCategory = testCategory;
+    } else {
+      this.testCategory = "";
+    }
+  }
+
+  public static class LoggingInfo {
+
+    private String id;
+    private Object logger;
+    private String[] calls;
+    private String activeLevels = "n/a";
+    private String factoryClassName;
+    private String factoryMethod;
+    private boolean usesString; // is false it uses "Object" for logging
+
+    public LoggingInfo(String id, String factoryClassName, String factoryMethod,
+                       String category, boolean usesString, String... calls) {
+      this.id = id;
+      this.calls = calls;
+      this.factoryClassName = factoryClassName;
+      this.factoryMethod = factoryMethod;
+      this.usesString = usesString;
+      reset(category);
+    }
+
+    public void logDemo() {
+      for (String call : calls) {
+        try {
+          invoke(id, logger, call);
+        } catch (Exception e) {
+          LOG.println("Ignoring: " + e);
+        }
+      }
+    }
+
+    private void invoke(String id, Object logger, String name) throws Exception {
+      final Class clazz = usesString ? String.class : Object.class;
+      final Method method = logger.getClass().getMethod(name, clazz);
+      method.invoke(logger, "Hello " + id + ", this is the level: " + name);
+    }
+
+    public boolean isAvailable() {
+      return logger != null;
+    }
+
+    public String getActiveLevels() {
+      return activeLevels;
+    }
+
+    protected void reset(String category) {
+
+      logger = null;
+
+      try {
+        final Method method = Class.forName(factoryClassName).getMethod(factoryMethod, String.class);
+        logger = method.invoke(null, category);
+      } catch (Exception e) {
+        LOG.println("Ignoring: " + e);
+      }
+
+      if (logger != null) {
+        activeLevels = "";
+        for (String call : calls) {
+          try {
+            if (checkLevel(category, call)) {
+              activeLevels += call + ":+ ";
+            } else {
+              activeLevels += call + ":- ";
+            }
+          } catch (Exception e) {
+            LOG.println(e.getMessage());
+            e.printStackTrace();
+            activeLevels += call + ":? ";
+          }
+        }
+      } else {
+        activeLevels = "n/a";
+      }
+    }
+
+    private boolean checkLevel(String category, String level)
+        throws NoSuchMethodException, ClassNotFoundException, InvocationTargetException, IllegalAccessException {
+
+      final Method method = Class.forName(factoryClassName).getMethod(factoryMethod, String.class);
+      final Object c = method.invoke(null, category);
+
+      if (JUL.equals(id)) {
+//        c.isLoggable(Level.parse(level.toUpperCase()))
+        final Class<?> levelClass = Class.forName("java.util.logging.Level");
+        final Method isLoggable = c.getClass().getMethod("isLoggable", levelClass);
+        final Method parse = levelClass.getMethod("parse", String.class);
+        final Object levelObject = parse.invoke(null, level.toUpperCase());
+        final Object hasLevel = isLoggable.invoke(c, levelObject);
+        return (Boolean) hasLevel;
+      }
+
+      if (SLF4J.equals(id)) {
+        String methodName = "is" + level.substring(0, 1).toUpperCase() + level.substring(1) + "Enabled";
+        final Object hasLevel = c.getClass().getMethod(methodName).invoke(c);
+        return (Boolean) hasLevel;
+      }
+
+      if (LOG4J.equals(id)) {
+//        org.apache.log4j.Logger.getLogger("").isEnabledFor(Priority.toPriority("debug"));
+
+        final Class<?> levelClass = Class.forName("org.apache.log4j.Priority");
+        final Method isLoggable = c.getClass().getMethod("isEnabledFor", levelClass);
+        final Method parse = levelClass.getMethod("toPriority", String.class);
+        final Object levelObject = parse.invoke(null, level.toUpperCase());
+        final Object hasLevel = isLoggable.invoke(c, levelObject);
+        return (Boolean) hasLevel;
+      }
+
+      if (LOG4J2.equals(id)) {
+        // org.apache.logging.log4j.core.Logger l = null; l.isEnabled(Level.parse("DEBUG"))
+
+        final Class<?> levelClass = Class.forName("org.apache.logging.log4j.Level");
+        final Method isLoggable = c.getClass().getMethod("isEnabled", levelClass);
+        final Method parse = levelClass.getMethod("getLevel", String.class);
+        final Object levelObject = parse.invoke(null, level.toUpperCase());
+        final Object hasLevel = isLoggable.invoke(c, levelObject);
+        return (Boolean) hasLevel;
+      }
+
+      if (JCL.equals(id)) {
+        //org.apache.commons.logging.Log l = null; l.isDebugEnabled();
+        String methodName = "is" + level.substring(0, 1).toUpperCase() + level.substring(1) + "Enabled";
+        final Object hasLevel = c.getClass().getMethod(methodName).invoke(c);
+        return (Boolean) hasLevel;
+      }
+
+      throw new IllegalStateException();
+    }
+  }
+}

Modified: myfaces/tobago/trunk/tobago-example/tobago-example-demo/src/main/webapp/WEB-INF/faces-config.xml
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/tobago-example/tobago-example-demo/src/main/webapp/WEB-INF/faces-config.xml?rev=1656507&r1=1656506&r2=1656507&view=diff
==============================================================================
--- myfaces/tobago/trunk/tobago-example/tobago-example-demo/src/main/webapp/WEB-INF/faces-config.xml (original)
+++ myfaces/tobago/trunk/tobago-example/tobago-example-demo/src/main/webapp/WEB-INF/faces-config.xml Mon Feb  2 16:13:19 2015
@@ -77,13 +77,6 @@
     <managed-bean-scope>session</managed-bean-scope>
   </managed-bean>
 
-  <navigation-rule>
-    <navigation-case>
-      <from-outcome>server-info</from-outcome>
-      <to-view-id>/server-info.xhtml</to-view-id>
-    </navigation-case>
-  </navigation-rule>
-
   <!--  overview  -->
 
   <lifecycle>

Modified: myfaces/tobago/trunk/tobago-example/tobago-example-demo/src/main/webapp/WEB-INF/tags/layout/overview.tag
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/tobago-example/tobago-example-demo/src/main/webapp/WEB-INF/tags/layout/overview.tag?rev=1656507&r1=1656506&r2=1656507&view=diff
==============================================================================
--- myfaces/tobago/trunk/tobago-example/tobago-example-demo/src/main/webapp/WEB-INF/tags/layout/overview.tag (original)
+++ myfaces/tobago/trunk/tobago-example/tobago-example-demo/src/main/webapp/WEB-INF/tags/layout/overview.tag Mon Feb  2 16:13:19 2015
@@ -50,8 +50,10 @@
                 onclick="alert('#{overviewBundle.pageTitle}' + String.fromCharCode(10) + '#{info.version}' + String.fromCharCode(10) + '#{overviewBundle.tobago_url}' + String.fromCharCode(10))"
                 label="#{overviewBundle.menu_about}"/>
             <tc:menuCommand link="http://myfaces.apache.org/tobago" label="Tobago in the Web"/>
-            <tc:menuCommand action="server-info" immediate="true"
+            <tc:menuCommand action="/server-info.xhtml" immediate="true"
                             label="Server Info" disabled="#{! info.enabled}"/>
+            <tc:menuCommand action="/logging-info.xhtml" immediate="true"
+                            label="Logging Info" disabled="#{! info.enabled}"/>
           </tc:menu>
         </tc:form>
       </tc:menuBar>

Added: myfaces/tobago/trunk/tobago-example/tobago-example-demo/src/main/webapp/logging-info.xhtml
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/tobago-example/tobago-example-demo/src/main/webapp/logging-info.xhtml?rev=1656507&view=auto
==============================================================================
--- myfaces/tobago/trunk/tobago-example/tobago-example-demo/src/main/webapp/logging-info.xhtml (added)
+++ myfaces/tobago/trunk/tobago-example/tobago-example-demo/src/main/webapp/logging-info.xhtml Mon Feb  2 16:13:19 2015
@@ -0,0 +1,100 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+-->
+
+<ui:composition template="/WEB-INF/tags/layout/overview.xhtml"
+                xmlns:tc="http://myfaces.apache.org/tobago/component"
+                xmlns:tx="http://myfaces.apache.org/tobago/extension"
+                xmlns:ui="http://java.sun.com/jsf/facelets"
+                xmlns:f="http://java.sun.com/jsf/core">
+
+  <ui:param name="title" value="Logging Info"/>
+
+  <tc:panel>
+    <f:facet name="layout">
+      <tc:gridLayout rows="auto;auto;auto;*"/>
+    </f:facet>
+
+    <tc:panel>
+      <f:facet name="layout">
+        <tc:gridLayout columns="*;auto"/>
+      </f:facet>
+
+      <tx:in label="Test Category" value="#{universalLoggingInfo.testCategory}"/>
+      <tc:button label="Update" action="#{universalLoggingInfo.update}"/>
+    </tc:panel>
+
+    <tc:separator/>
+
+    <tc:panel>
+      <f:facet name="layout">
+        <tc:gridLayout columns="auto;120px;auto;*"/>
+      </f:facet>
+
+      <tc:label value="JUL" tip="Java Util Logging"/>
+      <tc:selectBooleanCheckbox value="#{universalLoggingInfo.jul.available}" readonly="true"
+                                label="available"/>
+      <tc:button label="Log some stuff in every level" action="#{universalLoggingInfo.jul.logDemo}"
+                 disabled="#{not universalLoggingInfo.jul.available}"/>
+      <tc:out value="#{universalLoggingInfo.jul.activeLevels}"/>
+
+      <tc:label value="SLF4J" tip="Simple Logging Facade"/>
+      <tc:selectBooleanCheckbox value="#{universalLoggingInfo.slf4j.available}" readonly="true"
+                                label="available"/>
+      <tc:button label="Log some stuff in every level" action="#{universalLoggingInfo.slf4j.logDemo}"
+                 disabled="#{not universalLoggingInfo.slf4j.available}"/>
+      <tc:out value="#{universalLoggingInfo.slf4j.activeLevels}"/>
+
+      <tc:label value="LOG4J"/>
+      <tc:selectBooleanCheckbox value="#{universalLoggingInfo.log4j.available}" readonly="true"
+                                label="available"/>
+      <tc:button label="Log some stuff in every level" action="#{universalLoggingInfo.log4j.logDemo}"
+                 disabled="#{not universalLoggingInfo.log4j.available}"/>
+      <tc:out value="#{universalLoggingInfo.log4j.activeLevels}"/>
+
+      <tc:label value="LOG4J2"/>
+      <tc:selectBooleanCheckbox value="#{universalLoggingInfo.log4j2.available}" readonly="true"
+                                label="available"/>
+      <tc:button label="Log some stuff in every level" action="#{universalLoggingInfo.log4j2.logDemo}"
+                 disabled="#{not universalLoggingInfo.log4j2.available}"/>
+      <tc:out value="#{universalLoggingInfo.log4j2.activeLevels}"/>
+
+      <tc:label value="JCL" tip="Commons Logging"/>
+      <tc:selectBooleanCheckbox value="#{universalLoggingInfo.commonsLogging.available}" readonly="true"
+                                label="available"/>
+      <tc:button label="Log some stuff in every level" action="#{universalLoggingInfo.commonsLogging.logDemo}"
+                 disabled="#{not universalLoggingInfo.commonsLogging.available}"/>
+      <tc:out value="#{universalLoggingInfo.commonsLogging.activeLevels}"/>
+    </tc:panel>
+
+    <tc:panel>
+      <f:facet name="layout">
+        <tc:flowLayout/>
+      </f:facet>
+
+      <h3>This is a test page to check if all logging libraries are working well in the used container.</h3>
+
+      <ul>
+        <li>Change the category to check different categories and press "update" to see if the category is enabled.</li>
+        <li>Press "Log some stuff in every level" to log in that category.</li>
+        <li>Consult your log file (or anywhere you are logging) to check the result</li>
+      </ul>
+
+    </tc:panel>
+  </tc:panel>
+</ui:composition>

Modified: myfaces/tobago/trunk/tobago-example/tobago-example-demo/src/main/webapp/menu.xhtml
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/tobago-example/tobago-example-demo/src/main/webapp/menu.xhtml?rev=1656507&r1=1656506&r2=1656507&view=diff
==============================================================================
--- myfaces/tobago/trunk/tobago-example/tobago-example-demo/src/main/webapp/menu.xhtml (original)
+++ myfaces/tobago/trunk/tobago-example/tobago-example-demo/src/main/webapp/menu.xhtml Mon Feb  2 16:13:19 2015
@@ -42,9 +42,8 @@
                           value="#{overviewBundle.pageTitle} - #{info.version} - #{overviewBundle.tobago_url}"/>
       </tc:menuCommand>
       <tc:menuCommand link="http://myfaces.apache.org/tobago" label="Tobago in the Web"/>
-      <tc:menuCommand action="server-info" immediate="true"
-                      label="Server Info"
-                      disabled="#{! info.enabled}"/>
+      <tc:menuCommand action="/server-info.xhtml" immediate="true" label="Server Info" disabled="#{! info.enabled}"/>
+      <tc:menuCommand action="/logging-info.xhtml" immediate="true" label="Logging Info" disabled="#{! info.enabled}"/>
     </tc:menu>
   </tc:form>
 </tc:menuBar>