You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jena.apache.org by an...@apache.org on 2020/11/10 14:02:14 UTC

[jena] branch master updated: JENA-1898: Log output for webapp

This is an automated email from the ASF dual-hosted git repository.

andy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/jena.git


The following commit(s) were added to refs/heads/master by this push:
     new da2e8dd  JENA-1898: Log output for webapp
     new fd94c6b  Merge pull request #823 from afs/jena1989-fuseki
da2e8dd is described below

commit da2e8ddc3c833a94687d2b2ec2cbe1909151f36f
Author: Andy Seaborne <an...@apache.org>
AuthorDate: Sat Nov 7 16:23:49 2020 +0000

    JENA-1898: Log output for webapp
---
 .../apache/jena/atlas/logging/LogCtlLog4j2.java    | 15 +++---
 .../apache/jena/fuseki/system/FusekiLogging.java   | 41 +++++++++------
 jena-fuseki2/jena-fuseki-webapp/pom.xml            |  3 +-
 .../fuseki/webapp/FusekiServerEnvironmentInit.java | 19 ++++---
 .../src/main/webapp/WEB-INF/web.xml                |  5 ++
 .../src/main/webapp/log4j2.properties              | 61 ++++++++++++++++++++++
 6 files changed, 111 insertions(+), 33 deletions(-)

diff --git a/jena-base/src/main/java/org/apache/jena/atlas/logging/LogCtlLog4j2.java b/jena-base/src/main/java/org/apache/jena/atlas/logging/LogCtlLog4j2.java
index a22c2ea..017cef6 100644
--- a/jena-base/src/main/java/org/apache/jena/atlas/logging/LogCtlLog4j2.java
+++ b/jena-base/src/main/java/org/apache/jena/atlas/logging/LogCtlLog4j2.java
@@ -24,10 +24,11 @@ import java.io.InputStream;
 
 import org.apache.jena.atlas.io.IO;
 import org.apache.jena.atlas.lib.StrUtils;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.core.LoggerContext;
 import org.apache.logging.log4j.core.config.Configuration;
 import org.apache.logging.log4j.core.config.ConfigurationFactory;
 import org.apache.logging.log4j.core.config.ConfigurationSource;
-import org.apache.logging.log4j.core.config.Configurator;
 import org.apache.logging.log4j.core.config.properties.PropertiesConfigurationFactory;
 
 /**
@@ -56,22 +57,18 @@ public class LogCtlLog4j2 {
             ? new PropertiesConfigurationFactory()
             : ConfigurationFactory.getInstance();
         Configuration configuration = factory.getConfiguration(null, source);
-        Configurator.initialize(configuration);
+        LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
+        // This changes exising loggers.
+        ctx.setConfiguration(configuration);
     }
 
-//    public static void setCmdLogging() {
-//        LogCtl.setLog4j2();
-//        if ( ! LogCtl.isSetLog4j2property() )
-//            resetLogging(log4j2setupCmd);
-//    }
-
     // basic setup.
     // @formatter:off
     /** A basic logging setup. */
     public static String log4j2setup = StrUtils.strjoinNL
         ( "## Command default log4j2 setup : log4j2 properties syntax."
         , "status = error"
-        , "name = PropertiesConfig"
+        , "name = JenaLoggingDft"
 //        , "filters = threshold"
 //        , ""
 //        , "filter.threshold.type = ThresholdFilter"
diff --git a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/system/FusekiLogging.java b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/system/FusekiLogging.java
index 1c65afa..b457802 100644
--- a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/system/FusekiLogging.java
+++ b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/system/FusekiLogging.java
@@ -35,6 +35,16 @@ import org.apache.logging.log4j.core.config.ConfigurationSource;
 import org.apache.logging.log4j.core.config.Configurator;
 import org.apache.logging.log4j.core.config.properties.PropertiesConfigurationFactory;
 
+/**
+ * FusekiLogging.
+ * <p>
+ * This applies to Fuseki run from the command line and embedded.
+ * <p>
+ * This does not apply to Fuseki running in Tomcat where it uses the
+ * servlet 3.0 mechanism described in
+ * <a href="https://logging.apache.org/log4j/2.x/manual/webapp.html">Log4j2 manual
+ * (webapp)</a>. See {@code FusekiServerEnvironmentInit}.
+ */
 public class FusekiLogging
 {
     // This class must not have static constants, or otherwise not "Fuseki.*"
@@ -46,13 +56,15 @@ public class FusekiLogging
     // 2/ Use file:log4j2.properties if exists
     // 3/ Use log4j2.properties on the classpath.
     // 4/ Use org/apache/jena/fuseki/log4j2.properties on the classpath.
-    // 5/ Use Built in string
+    // 5/ Use built in string
+    //
+    // If a webapp running as a .war file in webapp container (e.g. Tomcat)
+    // logging is initialized in FusekiServerEnvironmentInit using a <contaxt-param>.
 
     /**
      * Places for the log4j properties file at (3).
      * This is not the standard, fixed classpath names used by log4j.
-     *             //   log4j2.properties, log4j2.yaml, log4j2.json, log4j2.xml
-
+     * log4j2.properties, log4j2.yaml, log4j2.json, log4j2.xml
      */
     private static final String[] resourcesForLog4jProperties = {
         // NOT the standard, fixed classpath names used by log4j2
@@ -60,20 +72,12 @@ public class FusekiLogging
         "log4j2-fuseki.properties"
     };
 
-    private static final boolean LogLogging     = System.getProperty("fuseki.loglogging") != null;
+    private static final boolean LogLogging     = System.getenv("FUSEKI_LOGLOGGING") != null || System.getProperty("fuseki.loglogging") != null;
+    
     private static boolean loggingInitialized   = false;
     private static boolean allowLoggingReset    = true;
 
     /**
-     * Switch off logging setting.
-     * Used by the embedded server so that the application's
-     * logging setup is not overwritten.
-     */
-    public static synchronized void allowLoggingReset(boolean value) {
-        allowLoggingReset = value;
-    }
-
-    /**
      * Mark whether logging is considered "initialized".
      * Some external factor (e.g. log4j2 webapp context param "log4jConfiguration")
      * may mean logging wil be initialized some other way.
@@ -91,6 +95,10 @@ public class FusekiLogging
     public static final String log4j2_configurationFile = "log4j.configurationFile";
     public static final String log4j2_web_configuration = "log4jConfiguration";
 
+    public static synchronized boolean hasInitialized() {
+        return loggingInitialized;
+    }
+    
     /** Set up logging. Allow an extra location (string directory name without trailing "/"). This may be null
      *
      * @param extraDir
@@ -204,13 +212,13 @@ public class FusekiLogging
     }
 
     private static String log4j2setupFallback() {
-        // This should be the same as resource.
-        // It protects against downstream repacking not including all resources.
+        // The logging file for Fuseki in Tomcat webapp is in "log4j2.properties" in the webapp root directory.
+        // This is used by command line Fuseki (full and main)
         // @formatter:off
         return StrUtils.strjoinNL
             ("## Plain output to stdout"
             , "status = error"
-            , "name = PropertiesConfig"
+            , "name = FusekiLogging"
 //            , ""
 //            , "filters = threshold"
 //            , "filter.threshold.type = ThresholdFilter"
@@ -220,7 +228,6 @@ public class FusekiLogging
             , "appender.console.name = OUT"
             , "appender.console.target = SYSTEM_OUT"
             , "appender.console.layout.type = PatternLayout"
-            //, "appender.console.layout.pattern = [%d{yyyy-MM-dd HH:mm:ss}] %-10c{1} %-5p :DFT: %m%n"
             , "appender.console.layout.pattern = [%d{yyyy-MM-dd HH:mm:ss}] %-10c{1} %-5p %m%n"
             , ""
             , "rootLogger.level                  = INFO"
diff --git a/jena-fuseki2/jena-fuseki-webapp/pom.xml b/jena-fuseki2/jena-fuseki-webapp/pom.xml
index 97f0ae5..8744ccf 100644
--- a/jena-fuseki2/jena-fuseki-webapp/pom.xml
+++ b/jena-fuseki2/jena-fuseki-webapp/pom.xml
@@ -72,12 +72,13 @@
       <artifactId>shiro-web</artifactId>
     </dependency>
 
-    <!-- Logging :  Needed because the Fuseki command line manages logging -->  
+    <!-- Logging  -->  
     <dependency>
       <groupId>org.apache.logging.log4j</groupId>
       <artifactId>log4j-slf4j-impl</artifactId>
     </dependency>
 
+    <!-- Within-webapp use -->
     <dependency>
       <groupId>org.apache.logging.log4j</groupId>
       <artifactId>log4j-web</artifactId>
diff --git a/jena-fuseki2/jena-fuseki-webapp/src/main/java/org/apache/jena/fuseki/webapp/FusekiServerEnvironmentInit.java b/jena-fuseki2/jena-fuseki-webapp/src/main/java/org/apache/jena/fuseki/webapp/FusekiServerEnvironmentInit.java
index 0b218cb..117f063 100644
--- a/jena-fuseki2/jena-fuseki-webapp/src/main/java/org/apache/jena/fuseki/webapp/FusekiServerEnvironmentInit.java
+++ b/jena-fuseki2/jena-fuseki-webapp/src/main/java/org/apache/jena/fuseki/webapp/FusekiServerEnvironmentInit.java
@@ -36,12 +36,19 @@ public class FusekiServerEnvironmentInit implements ServletContextListener {
     public void contextInitialized(ServletContextEvent sce) {
         // These do not touch Jena.
         FusekiEnv.setEnvironment();
-        // Log4j2 name.
-        // If this is set, the webapp is controlling log4j setup via log4j-web and its own initialization.
-        String x = sce.getServletContext().getInitParameter(FusekiLogging.log4j2_web_configuration);
-        if ( x != null )
-            FusekiLogging.markInitialized(true);
-        FusekiLogging.setLogging(FusekiEnv.FUSEKI_BASE);
+        // The command line initializes Fuseki-full with FusekiLogging.setLogging()
+        if ( FusekiLogging.hasInitialized() ) {
+            // If this is set, the webapp is controlling log4j setup via log4j-web and its own initialization.
+            // The logging file in in log4j2.properties in the webapp root directory.
+            String x = sce.getServletContext().getInitParameter(FusekiLogging.log4j2_web_configuration);
+            if ( x != null ) {
+                // https://logging.apache.org/log4j/2.x/manual/webapp.html
+                System.err.println("log4jConfiguration = "+x);
+                FusekiLogging.markInitialized(true);
+            } else {
+                FusekiLogging.setLogging(FusekiEnv.FUSEKI_BASE);
+            }
+        }
         JenaSystem.init();
     }
 
diff --git a/jena-fuseki2/jena-fuseki-webapp/src/main/webapp/WEB-INF/web.xml b/jena-fuseki2/jena-fuseki-webapp/src/main/webapp/WEB-INF/web.xml
index 5eda4f8..7af1640 100644
--- a/jena-fuseki2/jena-fuseki-webapp/src/main/webapp/WEB-INF/web.xml
+++ b/jena-fuseki2/jena-fuseki-webapp/src/main/webapp/WEB-INF/web.xml
@@ -93,6 +93,11 @@
     -->
     <param-value>shiro.ini</param-value>
   </context-param>
+
+  <context-param>
+    <param-name>log4jConfiguration</param-name>
+    <param-value>log4j2.properties</param-value>
+  </context-param>
   
   <!-- Fuseki datatset serviced calls -->
   <filter>
diff --git a/jena-fuseki2/jena-fuseki-webapp/src/main/webapp/log4j2.properties b/jena-fuseki2/jena-fuseki-webapp/src/main/webapp/log4j2.properties
new file mode 100644
index 0000000..2521a18
--- /dev/null
+++ b/jena-fuseki2/jena-fuseki-webapp/src/main/webapp/log4j2.properties
@@ -0,0 +1,61 @@
+## Licensed under the terms of http://www.apache.org/licenses/LICENSE-2.0
+
+## Plain output to stdout
+status = error
+name = FusekiLoggingWebapp
+
+appender.console.type = Console
+appender.console.name = OUT
+appender.console.target = SYSTEM_OUT
+appender.console.layout.type = PatternLayout
+appender.console.layout.pattern = [%d{yyyy-MM-dd HH:mm:ss}] %-10c{1} %-5p %m%n
+
+rootLogger.level                  = INFO
+rootLogger.appenderRef.stdout.ref = OUT
+
+logger.jena.name  = org.apache.jena
+logger.jena.level = INFO
+
+logger.arq-exec.name  = org.apache.jena.arq.exec
+logger.arq-exec.level = INFO
+
+logger.riot.name  = org.apache.jena.riot
+logger.riot.level = INFO
+
+logger.fuseki.name  = org.apache.jena.fuseki
+logger.fuseki.level = INFO
+
+logger.fuseki-fuseki.name  = org.apache.jena.fuseki.Fuseki
+logger.fuseki-fuseki.level = INFO
+
+logger.fuseki-server.name  = org.apache.jena.fuseki.Server
+logger.fuseki-server.level = INFO
+
+logger.fuseki-config.name  = org.apache.jena.fuseki.Config
+logger.fuseki-config.level = INFO
+
+logger.fuseki-admin.name  = org.apache.jena.fuseki.Admin
+logger.fuseki-admin.level = INFO
+
+logger.jetty.name  = org.eclipse.jetty
+logger.jetty.level = WARN
+
+logger.apache-http.name   = org.apache.http
+logger.apache-http.level  = WARN
+logger.shiro.name = org.apache.shiro
+logger.shiro.level = WARN
+
+# Hide bug in Shiro 1.5.0
+logger.shiro-realm.name = org.apache.shiro.realm.text.IniRealm
+logger.shiro-realm.level = ERROR
+
+# This goes out in NCSA format
+appender.plain.type = Console
+appender.plain.name = PLAIN
+appender.plain.layout.type = PatternLayout
+appender.plain.layout.pattern = %m%n
+
+logger.fuseki-request.name                   = org.apache.jena.fuseki.Request
+logger.fuseki-request.additivity             = false
+logger.fuseki-request.level                  = OFF
+logger.fuseki-request.appenderRef.plain.ref  = PLAIN