jena-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a...@apache.org
Subject [3/3] jena git commit: set -> setLevel. Protect against no log4j dependency.
Date Sat, 06 Aug 2016 12:06:52 GMT
set -> setLevel. Protect against no log4j dependency.


Project: http://git-wip-us.apache.org/repos/asf/jena/repo
Commit: http://git-wip-us.apache.org/repos/asf/jena/commit/67354f9f
Tree: http://git-wip-us.apache.org/repos/asf/jena/tree/67354f9f
Diff: http://git-wip-us.apache.org/repos/asf/jena/diff/67354f9f

Branch: refs/heads/master
Commit: 67354f9f220dd72b2c33a6280759ad5157f07240
Parents: d71ba0d
Author: Andy Seaborne <andy@apache.org>
Authored: Sat Aug 6 12:58:09 2016 +0100
Committer: Andy Seaborne <andy@apache.org>
Committed: Sat Aug 6 12:58:09 2016 +0100

----------------------------------------------------------------------
 .../org/apache/jena/atlas/logging/LogCtl.java   | 124 +++++++++++--------
 jena-cmds/src/main/java/arq/qparse.java         |   2 +-
 .../java/org/apache/jena/fuseki/TS_Fuseki.java  |  21 ++--
 .../java/org/apache/jena/fuseki/TestAuth.java   |   6 +-
 4 files changed, 86 insertions(+), 67 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/jena/blob/67354f9f/jena-base/src/main/java/org/apache/jena/atlas/logging/LogCtl.java
----------------------------------------------------------------------
diff --git a/jena-base/src/main/java/org/apache/jena/atlas/logging/LogCtl.java b/jena-base/src/main/java/org/apache/jena/atlas/logging/LogCtl.java
index cde9aba..ac4b4ac 100644
--- a/jena-base/src/main/java/org/apache/jena/atlas/logging/LogCtl.java
+++ b/jena-base/src/main/java/org/apache/jena/atlas/logging/LogCtl.java
@@ -33,52 +33,69 @@ import org.slf4j.Logger ;
 /** Setup and control of logging - needs access to log4j binaries */ 
 public class LogCtl {
 
-    /**
-     * Turn on a logger (all levels). Works for Log4j and Java logging as the
-     * logging provider to Apache common logging or slf4j.
-     */
-    
-    static public void enable(Logger logger) {
-        enable(logger.getName()) ;
-    }
-
-    static public void enable(String logger) {
-        set(logger, "all") ;
-    }
-
     static public void set(Logger logger, String level) {
-        set(logger.getName(), level) ;
+        setLevel(logger.getName(), level) ;
     }
 
-    /** Turn on a logger (specific level levels) */
+    /** @deprecated Use {@link #setLevel(String, String)} */
+    @Deprecated
     static public void set(Class<? > logger, String level) {
-        set(logger.getName(), level) ;
+        setLevel(logger.getName(), level) ;
     }
 
+    /** @deprecated Use {@link #setLevel(String, String)} */
+    @Deprecated
     static public void set(String logger, String level) {
-        org.apache.log4j.Level level1 = org.apache.log4j.Level.ALL ;
-        java.util.logging.Level level2 = java.util.logging.Level.ALL ;
-        if ( level.equalsIgnoreCase("info") ) {
-            level1 = org.apache.log4j.Level.INFO ;
-            level2 = java.util.logging.Level.INFO ;
-        }
-    
-        if ( level.equalsIgnoreCase("debug") ) {
-            level1 = org.apache.log4j.Level.DEBUG ;
-            level2 = java.util.logging.Level.FINE ;
-        }
+        setLevel(logger,level) ;
+    }
+
+    static public void setLevel(Class<? > logger, String level) {
+        setLevel(logger.getName(), level) ;
+    }
     
-        if ( level.equalsIgnoreCase("warn") ) {
-            level1 = org.apache.log4j.Level.WARN ;
-            level2 = java.util.logging.Level.WARNING ;
-        }
-        if ( level.equalsIgnoreCase("error") ) {
-            level1 = org.apache.log4j.Level.ERROR ;
-            level2 = java.util.logging.Level.SEVERE ;
+    static public void setLevel(String logger, String level) {
+        setLevelLog4j(logger,level) ;
+        setLevelJUL(logger,level) ;
+    }
+
+    private static void setLevelJUL(String logger, String levelName) {
+        java.util.logging.Level level = java.util.logging.Level.ALL ;
+        if ( levelName.equalsIgnoreCase("info") )
+            level = java.util.logging.Level.INFO ;
+        else if ( levelName.equalsIgnoreCase("debug") )
+            level = java.util.logging.Level.FINE ;
+        else if ( levelName.equalsIgnoreCase("warn") || levelName.equalsIgnoreCase("warning")
) 
+            level = java.util.logging.Level.WARNING ;
+        else if ( levelName.equalsIgnoreCase("error") )
+            level = java.util.logging.Level.SEVERE ;
+        else if ( levelName.equalsIgnoreCase("OFF") )
+            level = java.util.logging.Level.OFF ;
+        if ( level != null )
+            java.util.logging.Logger.getLogger(logger).setLevel(level) ;
+    }
+
+    private static void setLevelLog4j(String logger, String levelName) {
+        try {
+            org.apache.log4j.Level level = org.apache.log4j.Level.ALL ;
+            if ( levelName.equalsIgnoreCase("info") )
+                level = org.apache.log4j.Level.INFO ;
+            else if ( levelName.equalsIgnoreCase("debug") )
+                level = org.apache.log4j.Level.DEBUG ;
+            else if ( levelName.equalsIgnoreCase("warn") || levelName.equalsIgnoreCase("warning")
)
+                level = org.apache.log4j.Level.WARN ;
+            else if ( levelName.equalsIgnoreCase("error") )
+                level = org.apache.log4j.Level.ERROR ;
+            else if ( levelName.equalsIgnoreCase("OFF") )
+                level = org.apache.log4j.Level.OFF ;
+            if ( level != null )   
+                org.apache.log4j.LogManager.getLogger(logger).setLevel(level) ;         
  
+        } catch (NoClassDefFoundError ex) {
+            // For when it is not on the class path 
         }
-        logLevel(logger, level1, level2) ;
     }
 
+    /** @deprecated Do not use - to be removed - use {@link #setLevel(String, String)} */
+    @Deprecated
     static public void logLevel(String logger, org.apache.log4j.Level level1, java.util.logging.Level
level2) {
         if ( level1 != null )
             org.apache.log4j.LogManager.getLogger(logger).setLevel(level1) ;
@@ -90,6 +107,19 @@ public class LogCtl {
      * Turn on a logger (all levels). Works for Log4j and Java logging as the
      * logging provider to Apache common logging or slf4j.
      */
+    
+    static public void enable(Logger logger) {
+        enable(logger.getName()) ;
+    }
+
+    static public void enable(String logger) {
+        setLevel(logger, "all") ;
+    }
+
+    /**
+     * Turn on a logger (all levels). Works for Log4j and Java logging as the
+     * logging provider to Apache common logging or slf4j.
+     */
     static public void enable(Class<? > logger) {
         org.apache.log4j.LogManager.getLogger(logger).setLevel(org.apache.log4j.Level.ALL)
;
         java.util.logging.Logger.getLogger(logger.getName()).setLevel(java.util.logging.Level.ALL)
;
@@ -100,8 +130,7 @@ public class LogCtl {
      * logging provider to Apache common logging or slf4j.
      */
     static public void disable(String logger) {
-        org.apache.log4j.LogManager.getLogger(logger).setLevel(org.apache.log4j.Level.OFF)
;
-        java.util.logging.Logger.getLogger(logger).setLevel(java.util.logging.Level.OFF)
;
+        setLevel(logger, "OFF") ;
     }
 
     /**
@@ -109,8 +138,7 @@ public class LogCtl {
      * logging provider to Apache common logging or slf4j.
      */
     static public void disable(Class<? > logger) {
-        org.apache.log4j.LogManager.getLogger(logger).setLevel(org.apache.log4j.Level.OFF)
;
-        java.util.logging.Logger.getLogger(logger.getName()).setLevel(java.util.logging.Level.OFF)
;
+        setLevel(logger.getName(), "OFF") ;
     }
 
     /**
@@ -118,8 +146,7 @@ public class LogCtl {
      * provider to Apache common logging or slf4j.
      */
     static public void setInfo(String logger) {
-        org.apache.log4j.LogManager.getLogger(logger).setLevel(org.apache.log4j.Level.INFO)
;
-        java.util.logging.Logger.getLogger(logger).setLevel(java.util.logging.Level.INFO)
;
+        setLevel(logger, "info") ;
     }
 
     /**
@@ -127,8 +154,7 @@ public class LogCtl {
      * provider to Apache common logging or slf4j.
      */
     static public void setInfo(Class<? > logger) {
-        org.apache.log4j.LogManager.getLogger(logger).setLevel(org.apache.log4j.Level.INFO)
;
-        java.util.logging.Logger.getLogger(logger.getName()).setLevel(java.util.logging.Level.INFO)
;
+        setLevel(logger.getName(), "info") ;
     }
 
     /**
@@ -136,8 +162,7 @@ public class LogCtl {
      * provider to Apache common logging or slf4j.
      */
     static public void setWarn(String logger) {
-        org.apache.log4j.LogManager.getLogger(logger).setLevel(org.apache.log4j.Level.WARN)
;
-        java.util.logging.Logger.getLogger(logger).setLevel(java.util.logging.Level.WARNING)
;
+        setLevel(logger, "warn") ;
     }
 
     /**
@@ -145,8 +170,7 @@ public class LogCtl {
      * provider to Apache common logging or slf4j.
      */
     static public void setWarn(Class<? > logger) {
-        org.apache.log4j.LogManager.getLogger(logger).setLevel(org.apache.log4j.Level.WARN)
;
-        java.util.logging.Logger.getLogger(logger.getName()).setLevel(java.util.logging.Level.WARNING)
;
+        setLevel(logger.getName(), "warn") ;
     }
 
     /**
@@ -154,8 +178,7 @@ public class LogCtl {
      * provider to Apache common logging or slf4j.
      */
     static public void setError(String logger) {
-        org.apache.log4j.LogManager.getLogger(logger).setLevel(org.apache.log4j.Level.ERROR)
;
-        java.util.logging.Logger.getLogger(logger).setLevel(java.util.logging.Level.SEVERE)
;
+        setLevel(logger, "error") ;
     }
 
     /**
@@ -163,8 +186,7 @@ public class LogCtl {
      * provider to Apache common logging or slf4j.
      */
     static public void setError(Class<? > logger) {
-        org.apache.log4j.LogManager.getLogger(logger).setLevel(org.apache.log4j.Level.ERROR)
;
-        java.util.logging.Logger.getLogger(logger.getName()).setLevel(java.util.logging.Level.SEVERE)
;
+        setLevel(logger.getName(), "error") ;
     }
 
     private static String log4Jsetup = StrUtils.strjoinNL

http://git-wip-us.apache.org/repos/asf/jena/blob/67354f9f/jena-cmds/src/main/java/arq/qparse.java
----------------------------------------------------------------------
diff --git a/jena-cmds/src/main/java/arq/qparse.java b/jena-cmds/src/main/java/arq/qparse.java
index 6b2eda5..041ebeb 100644
--- a/jena-cmds/src/main/java/arq/qparse.java
+++ b/jena-cmds/src/main/java/arq/qparse.java
@@ -180,7 +180,7 @@ public class qparse extends CmdARQ
                 if ( ex.getCause() != null )
                     ex.getCause().printStackTrace(System.err) ;
             }
-            finally { LogCtl.set(ParserBase.ParserLoggerName, "INFO") ; }
+            finally { LogCtl.setLevel(ParserBase.ParserLoggerName, "INFO") ; }
 
             
             // Print the query out in some syntax

http://git-wip-us.apache.org/repos/asf/jena/blob/67354f9f/jena-fuseki2/jena-fuseki-core/src/test/java/org/apache/jena/fuseki/TS_Fuseki.java
----------------------------------------------------------------------
diff --git a/jena-fuseki2/jena-fuseki-core/src/test/java/org/apache/jena/fuseki/TS_Fuseki.java
b/jena-fuseki2/jena-fuseki-core/src/test/java/org/apache/jena/fuseki/TS_Fuseki.java
index 26de2a1..e7a09ee 100644
--- a/jena-fuseki2/jena-fuseki-core/src/test/java/org/apache/jena/fuseki/TS_Fuseki.java
+++ b/jena-fuseki2/jena-fuseki-core/src/test/java/org/apache/jena/fuseki/TS_Fuseki.java
@@ -59,19 +59,16 @@ public class TS_Fuseki extends ServerTest
         FusekiLogging.setLogging();
         FusekiEnv.setEnvironment() ;
         
-        org.apache.log4j.Level WARN1 = org.apache.log4j.Level.WARN ; 
-        java.util.logging.Level WARN2 = java.util.logging.Level.WARNING ;
-
         // Occasionally log4j.properties gets out of step.
-        LogCtl.logLevel("org.apache.shiro",     WARN1, WARN2);
-        LogCtl.logLevel("org.eclipse.jetty",    WARN1, WARN2);
+        LogCtl.setLevel("org.apache.shiro",          "WARN") ;
+        LogCtl.setLevel("org.eclipse.jetty",         "WARN");
         
-        LogCtl.logLevel(Fuseki.serverLogName,   WARN1, WARN2);
-        LogCtl.logLevel(Fuseki.configLogName,   WARN1, WARN2);
-        LogCtl.logLevel(Fuseki.adminLogName,    WARN1, WARN2);
-        LogCtl.logLevel(Fuseki.builderLogName,  WARN1, WARN2);
-        LogCtl.logLevel(Fuseki.actionLogName,   WARN1, WARN2);
-        LogCtl.logLevel(Fuseki.requestLogName,  WARN1, WARN2);
-        LogCtl.logLevel(Fuseki.servletRequestLogName,   WARN1, WARN2);
+        LogCtl.setLevel(Fuseki.serverLogName,        "WARN");
+        LogCtl.setLevel(Fuseki.configLogName,        "WARN");
+        LogCtl.setLevel(Fuseki.adminLogName,         "WARN");
+        LogCtl.setLevel(Fuseki.builderLogName,       "WARN");
+        LogCtl.setLevel(Fuseki.actionLogName,        "WARN");
+        LogCtl.setLevel(Fuseki.requestLogName,       "WARN");
+        LogCtl.setLevel(Fuseki.servletRequestLogName,"WARN");
     }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/jena/blob/67354f9f/jena-fuseki2/jena-fuseki-core/src/test/java/org/apache/jena/fuseki/TestAuth.java
----------------------------------------------------------------------
diff --git a/jena-fuseki2/jena-fuseki-core/src/test/java/org/apache/jena/fuseki/TestAuth.java
b/jena-fuseki2/jena-fuseki-core/src/test/java/org/apache/jena/fuseki/TestAuth.java
index a5158d2..00fe231 100644
--- a/jena-fuseki2/jena-fuseki-core/src/test/java/org/apache/jena/fuseki/TestAuth.java
+++ b/jena-fuseki2/jena-fuseki-core/src/test/java/org/apache/jena/fuseki/TestAuth.java
@@ -78,9 +78,9 @@ public class TestAuth {
             writer.write("forbidden: password, other");
         }
 
-        LogCtl.logLevel(Fuseki.serverLog.getName(), org.apache.log4j.Level.WARN, java.util.logging.Level.WARNING);
-        LogCtl.logLevel(Fuseki.actionLog.getName(), org.apache.log4j.Level.WARN, java.util.logging.Level.WARNING);
-        LogCtl.logLevel("org.eclipse.jetty", org.apache.log4j.Level.WARN, java.util.logging.Level.WARNING);
+        LogCtl.setLevel(Fuseki.serverLogName, "warn");
+        LogCtl.setLevel(Fuseki.actionLogName, "warn") ;
+        LogCtl.setLevel("org.eclipse.jetty",  "warn") ;
 
         ServerTest.setupServer(authPort, realmFile.getAbsolutePath(), authDatasetPath, true);
     }


Mime
View raw message