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 2016/08/12 14:54:54 UTC

[1/2] jena git commit: Reimplement to avoid problems with inheriting from ConsoleHandler.

Repository: jena
Updated Branches:
  refs/heads/master ce3c8f9aa -> 793ffa1f7


Reimplement to avoid problems with inheriting from ConsoleHandler.

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

Branch: refs/heads/master
Commit: 1ef96bb9d170587e76e5b2636caba6f746f95fef
Parents: ce3c8f9
Author: Andy Seaborne <an...@apache.org>
Authored: Fri Aug 12 15:50:02 2016 +0100
Committer: Andy Seaborne <an...@apache.org>
Committed: Fri Aug 12 15:50:02 2016 +0100

----------------------------------------------------------------------
 .../logging/java/ConsoleHandlerStream.java      | 92 ++++++++++++++++----
 1 file changed, 73 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/jena/blob/1ef96bb9/jena-base/src/main/java/org/apache/jena/atlas/logging/java/ConsoleHandlerStream.java
----------------------------------------------------------------------
diff --git a/jena-base/src/main/java/org/apache/jena/atlas/logging/java/ConsoleHandlerStream.java b/jena-base/src/main/java/org/apache/jena/atlas/logging/java/ConsoleHandlerStream.java
index fd4dbac..d900b1e 100644
--- a/jena-base/src/main/java/org/apache/jena/atlas/logging/java/ConsoleHandlerStream.java
+++ b/jena-base/src/main/java/org/apache/jena/atlas/logging/java/ConsoleHandlerStream.java
@@ -20,8 +20,9 @@ package org.apache.jena.atlas.logging.java;
 
 import java.io.OutputStream ;
 import java.nio.charset.StandardCharsets ;
-import java.util.logging.ConsoleHandler ;
-import java.util.logging.LogManager ;
+import java.util.logging.* ;
+
+import org.apache.jena.atlas.logging.java.TextFormatter ;
 
 /** Console handler that modifies {@link java.util.logging.ConsoleHandler}.
  * Supports the configuration parameters of {@link ConsoleHandler} -- {@code .level},
@@ -29,35 +30,88 @@ import java.util.logging.LogManager ;
  * <p>
  * Defaults:
  * <ul>
- * <li>Stdout, rather than stderr</li>
+ * <li>Stdout, rather than stderr, by default.</li>
  * <li>{@link TextFormatter} rather than {@link java.util.logging.SimpleFormatter}</li>
  * <li>UTF-8, rather than platform charset</li>
  * </ul>
+ * Example:
+ * <pre>
+ * handlers=org.apache.jena.atlas.logging.java.ConsoleHandlerStream</pre>
+ * or to configure the formatter as well:
+ * <pre>
+ * handlers=org.apache.jena.atlas.logging.java.ConsoleHandlerStream
+ * org.apache.jena.atlas.logging.java.TextFormatter.format = %5$tT %3$-5s %2$-20s -- %6$s</pre>
  */
-public class ConsoleHandlerStream extends ConsoleHandler {
-
+public class ConsoleHandlerStream extends StreamHandler {
+    // We can't use ConsoleHandler.  
+    // The setOutputStream() operation closes the previous stream but when the only
+    // constructor ConsoleHandler() runs, it sets output to System.err.
+    // So System.err is then closed in the app!
+    // We need to chose the output in the constructor and ConsoleHandler does not allow that,
+    // hence going straight to StreamHandler and having to provide the functionality here. 
+    
     public ConsoleHandlerStream() {
         this(System.out) ;
     }
     
     public ConsoleHandlerStream(OutputStream outputStream) {
-        super() ;
+        super(outputStream, new TextFormatter()) ;
+        
         LogManager manager = LogManager.getLogManager();
+        ClassLoader classLoader = ClassLoader.getSystemClassLoader() ;
         String cname = getClass().getName();
-        // Change the formatter default from SimpleFormatter to TextFormatter.
-        String pNameFormatter = cname +".formatter" ;
-        if ( manager.getProperty(pNameFormatter) == null )
-            setFormatter(new TextFormatter()) ;
-        String pNameEncoding = cname +".encoding" ;
-        if ( manager.getProperty(pNameEncoding) == null ) {
-            try { setEncoding(StandardCharsets.UTF_8.name()) ; }
-            catch (Exception e) { 
-                // That should work as it is a required charset. 
-                System.err.print("Failed to set encoding: "+e.getMessage()) ;
-                // Ignore and try to carry on.
+        
+        // -- Level
+        Level level = Level.INFO ;
+        String pLevel = getProperty(manager, cname, "level") ;
+        if ( pLevel != null )
+            level = Level.parse(pLevel) ;
+        setLevel(level);
+        
+        // -- Formatter
+        // The default is TextFormatter above
+        // (we had to pass a Formatter of some kind to super(,)).
+        String pFormatter = getProperty(manager, cname, "formatter") ;
+        if ( pFormatter != null ) {
+            try {
+                Class<?> cls = classLoader.loadClass(pFormatter);
+                setFormatter((Formatter) cls.newInstance());
+            } catch (Exception ex) {
+                System.err.println("Problems setting the logging formatter") ;
+                ex.printStackTrace(System.err);
+            }
+        }
+        
+        // -- Filter
+        String pFilter = getProperty(manager, cname, "filter") ;
+        if ( pFilter != null ) {
+            try {
+                Class<?> cls = classLoader.loadClass(pFilter);
+                setFilter((Filter) cls.newInstance());
+            } catch (Exception ex) {
+                System.err.println("Problems setting the logging filter") ;
+                ex.printStackTrace(System.err);
             }
         }
-        // Temporary fix : setOutputStream closes the old setting which is backed by System.err.  
-        //setOutputStream(outputStream);
+        
+        // -- Encoding : Default UTF-8
+        String pEncoding = getProperty(manager, cname, "encoding") ;
+        if ( pEncoding == null )
+            pEncoding = StandardCharsets.UTF_8.name() ;
+        try { setEncoding(pEncoding) ; }
+        catch (Exception e) { 
+            // That should work for UTF-8 as it is a required charset. 
+            System.err.print("Failed to set encoding: "+e.getMessage()) ;
+        }
+    }
+    
+    private String getProperty(LogManager manager, String cname, String pname) {
+        return manager.getProperty(cname+"."+pname);
+    }
+    
+    @Override
+    public void publish(LogRecord record) {
+        super.publish(record);
+        flush();
     }
 }


[2/2] jena git commit: Simple logging handler/formatter for use with pre-formatted messages.

Posted by an...@apache.org.
Simple logging handler/formatter for use with pre-formatted messages.

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

Branch: refs/heads/master
Commit: 793ffa1f715fbf3536e554cd5abbce02393eb7b7
Parents: 1ef96bb
Author: Andy Seaborne <an...@apache.org>
Authored: Fri Aug 12 15:50:56 2016 +0100
Committer: Andy Seaborne <an...@apache.org>
Committed: Fri Aug 12 15:50:56 2016 +0100

----------------------------------------------------------------------
 .../jena/atlas/logging/java/FlatFormatter.java  | 45 ++++++++++++++++++++
 .../jena/atlas/logging/java/FlatHandler.java    | 45 ++++++++++++++++++++
 2 files changed, 90 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/jena/blob/793ffa1f/jena-base/src/main/java/org/apache/jena/atlas/logging/java/FlatFormatter.java
----------------------------------------------------------------------
diff --git a/jena-base/src/main/java/org/apache/jena/atlas/logging/java/FlatFormatter.java b/jena-base/src/main/java/org/apache/jena/atlas/logging/java/FlatFormatter.java
new file mode 100644
index 0000000..75563e4
--- /dev/null
+++ b/jena-base/src/main/java/org/apache/jena/atlas/logging/java/FlatFormatter.java
@@ -0,0 +1,45 @@
+/*
+ * 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.
+ */
+
+package org.apache.jena.atlas.logging.java;
+
+import java.text.MessageFormat ;
+import java.util.logging.Formatter ;
+import java.util.logging.LogRecord ;
+
+/** Very simple formatter - just the log message */ 
+public class FlatFormatter extends Formatter {
+
+    private final boolean ensureNL ;
+
+    public FlatFormatter() { this(true) ; }
+    
+    public FlatFormatter(boolean ensureNewline) {
+        this.ensureNL = ensureNewline ;
+    }
+
+    @Override
+    public String format(LogRecord record) {
+        String message = record.getMessage() ;
+        if ( record.getParameters() != null )
+            message = MessageFormat.format(message, record.getParameters()) ;
+        if ( ensureNL && ! message.endsWith("\n") )
+            message = message + "\n" ;
+        return message ;                 
+    }
+}

http://git-wip-us.apache.org/repos/asf/jena/blob/793ffa1f/jena-base/src/main/java/org/apache/jena/atlas/logging/java/FlatHandler.java
----------------------------------------------------------------------
diff --git a/jena-base/src/main/java/org/apache/jena/atlas/logging/java/FlatHandler.java b/jena-base/src/main/java/org/apache/jena/atlas/logging/java/FlatHandler.java
new file mode 100644
index 0000000..3216135
--- /dev/null
+++ b/jena-base/src/main/java/org/apache/jena/atlas/logging/java/FlatHandler.java
@@ -0,0 +1,45 @@
+/*
+ * 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.
+ */
+
+package org.apache.jena.atlas.logging.java;
+
+import java.util.logging.LogRecord ;
+import java.util.logging.StreamHandler ;
+
+/** A handler and formatter for unadorned output to stdout.
+ * Example: NCSA Format logging in Fuseki alerady formats the whole line 
+ * 
+ * <pre>
+ * org.apache.jena.fuseki.Request.level=INFO
+ * org.apache.jena.fuseki.Request.useParentHandlers=false
+ * org.apache.jena.fuseki.Request.handlers=logging.FlatHandler
+ * </pre>
+ * 
+ */
+public class FlatHandler extends StreamHandler {
+    
+    public FlatHandler() { 
+        super(System.out, new FlatFormatter(true)) ;
+    }
+    
+    @Override
+    public void publish(LogRecord record) {
+        super.publish(record);
+        flush();
+    }
+}