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/07/29 16:30:25 UTC
jena git commit: Use existing java logging classes. Support all
configuration properties
Repository: jena
Updated Branches:
refs/heads/master e385a0dec -> a2cc66c2e
Use existing java logging classes. Support all configuration properties
Project: http://git-wip-us.apache.org/repos/asf/jena/repo
Commit: http://git-wip-us.apache.org/repos/asf/jena/commit/a2cc66c2
Tree: http://git-wip-us.apache.org/repos/asf/jena/tree/a2cc66c2
Diff: http://git-wip-us.apache.org/repos/asf/jena/diff/a2cc66c2
Branch: refs/heads/master
Commit: a2cc66c2e464598e89e41de3bee698a0e66e84e9
Parents: e385a0d
Author: Andy Seaborne <an...@apache.org>
Authored: Fri Jul 29 17:28:09 2016 +0100
Committer: Andy Seaborne <an...@apache.org>
Committed: Fri Jul 29 17:30:09 2016 +0100
----------------------------------------------------------------------
.../logging/java/ConsoleHandlerStdout.java | 59 ++-----------------
.../logging/java/ConsoleHandlerStream.java | 62 ++++++++++++++++++++
.../jena/atlas/logging/java/TextFormatter.java | 44 ++++++++++++--
3 files changed, 106 insertions(+), 59 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/jena/blob/a2cc66c2/jena-base/src/main/java/org/apache/jena/atlas/logging/java/ConsoleHandlerStdout.java
----------------------------------------------------------------------
diff --git a/jena-base/src/main/java/org/apache/jena/atlas/logging/java/ConsoleHandlerStdout.java b/jena-base/src/main/java/org/apache/jena/atlas/logging/java/ConsoleHandlerStdout.java
index 7bfab7e..8af617b 100644
--- a/jena-base/src/main/java/org/apache/jena/atlas/logging/java/ConsoleHandlerStdout.java
+++ b/jena-base/src/main/java/org/apache/jena/atlas/logging/java/ConsoleHandlerStdout.java
@@ -18,58 +18,7 @@
package org.apache.jena.atlas.logging.java;
-import java.util.logging.Formatter ;
-import java.util.logging.Handler ;
-import java.util.logging.LogManager ;
-import java.util.logging.LogRecord ;
-
-
-public class ConsoleHandlerStdout extends Handler
-{
-
- private void configure()
- {
- LogManager manager = LogManager.getLogManager();
- String cname = getClass().getName();
-
- String cls = manager.getProperty(cname+".formatter") ;
- Formatter fmt = null ;
-
- try {
- if (cls != null) {
- Class<?> clz = ClassLoader.getSystemClassLoader().loadClass(cls);
- fmt = (Formatter) clz.newInstance();
- }
- } catch (Exception ex) {
- // We got one of a variety of exceptions in creating the
- // class or creating an instance.
- // Drop through.
- }
- if ( fmt == null )
- fmt = new TextFormatter() ;
- setFormatter(fmt) ;
- }
-
- public ConsoleHandlerStdout()
- {
- configure() ;
- }
-
- @Override
- public void close() throws SecurityException
- {}
-
- @Override
- public void flush()
- { System.out.flush(); }
-
- @Override
- public void publish(LogRecord record)
- {
- if ( ! super.isLoggable(record) )
- return ;
- String s = getFormatter().format(record) ;
- System.out.print(s) ;
- }
-
-}
+/** Backwards compatibility only */
+@Deprecated
+public class ConsoleHandlerStdout extends ConsoleHandlerStream
+{}
http://git-wip-us.apache.org/repos/asf/jena/blob/a2cc66c2/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
new file mode 100644
index 0000000..1dd5521
--- /dev/null
+++ b/jena-base/src/main/java/org/apache/jena/atlas/logging/java/ConsoleHandlerStream.java
@@ -0,0 +1,62 @@
+/*
+ * 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.io.OutputStream ;
+import java.nio.charset.StandardCharsets ;
+import java.util.logging.ConsoleHandler ;
+import java.util.logging.LogManager ;
+
+/** Console handler that modifies {@link java.util.logging.ConsoleHandler}.
+ * Supports the configuration parameters of {@link ConsoleHandler} -- {@code .level},
+ * {@code .filter}, {@code .formatter} and {@code .encoding}.
+ * <p>
+ * Defaults:
+ * <ul>
+ * <li>Stdout, rather than stderr</li>
+ * <li>{@link TextFormatter} rather than {@link java.util.logging.SimpleFormatter}</li>
+ * <li>UTF-8, rather than platform charset</li>
+ * </ul>
+ */
+public class ConsoleHandlerStream extends ConsoleHandler {
+
+ public ConsoleHandlerStream() {
+ this(System.out) ;
+ }
+
+ public ConsoleHandlerStream(OutputStream outputStream) {
+ super() ;
+ LogManager manager = LogManager.getLogManager();
+ 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.
+ }
+ }
+ setOutputStream(outputStream);
+ }
+}
http://git-wip-us.apache.org/repos/asf/jena/blob/a2cc66c2/jena-base/src/main/java/org/apache/jena/atlas/logging/java/TextFormatter.java
----------------------------------------------------------------------
diff --git a/jena-base/src/main/java/org/apache/jena/atlas/logging/java/TextFormatter.java b/jena-base/src/main/java/org/apache/jena/atlas/logging/java/TextFormatter.java
index 3e97942..e9287aa 100644
--- a/jena-base/src/main/java/org/apache/jena/atlas/logging/java/TextFormatter.java
+++ b/jena-base/src/main/java/org/apache/jena/atlas/logging/java/TextFormatter.java
@@ -21,11 +21,49 @@ package org.apache.jena.atlas.logging.java;
import java.text.MessageFormat ;
import java.util.Date ;
import java.util.logging.Formatter ;
+import java.util.logging.LogManager ;
import java.util.logging.LogRecord ;
-/** A pattern-like log formatter */
+/** A pattern-driven log formatter.
+ * inspired by Log4j's PatternLayout
+ * Set a different output pattern with {@code .format}.
+ * <p>
+ * The default format is {@code "%5$tT %3$-5s %2$-20s :: %6$s\n"}.
+ * <ul>
+ * <li>"%5$tT.%5$tL %3$-5s %2$-20s :: %6$s\n" for milliseconds.
+ * <li>"%tF %5$tT.%5$tL %3$-5s %2$-20s :: %6$s\n" for date
+ * </ul>
+ * <p>
+ * The log message formatting call is:
+ * <pre>
+ * String.format(format,
+ * loggerName, // 1
+ * loggerNameShort, // 2
+ * record.getLevel(), // 3
+ * Thread.currentThread().getName(), // 4
+ * new Date(record.getMillis()), // 5
+ * formatted$) ; // 6
+ * </pre>
+ * where {@code formatted$} is the {@link LogRecord} message string after parameters have been processed.
+ */
public class TextFormatter extends Formatter
{
+ // %tT (%5$tT) is %5$tH:%5$tM:%5$tS
+ // %tF is 2008-11-22 "%tY-%tm-%td"
+ private String format = "%5$tT %3$-5s %2$-20s :: %6$s\n" ;
+
+ public TextFormatter() {
+ LogManager manager = LogManager.getLogManager() ;
+ String cname = getClass().getName();
+
+ String fmt = manager.getProperty(cname+".format") ;
+ if ( fmt != null ) {
+ if ( ! fmt.endsWith("\n") )
+ fmt = fmt + "\n" ;
+ format = fmt ;
+ }
+ }
+
@Override
public String format(LogRecord record) {
String loggerName = record.getLoggerName();
@@ -40,9 +78,7 @@ public class TextFormatter extends Formatter
if ( record.getParameters() != null )
formatted$ = MessageFormat.format(formatted$, record.getParameters()) ;
- // %tT (%5$tT) is %5$tH:%5$tM:%5$tS
- // %tF is 2008-11-22 "%tY-%tm-%td"
- return String.format("%5$tT %3$-5s %2$-25s :: %6$s\n",
+ return String.format(format,
loggerName, // 1
loggerNameShort, // 2
record.getLevel(), // 3