sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1493748 - in /sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/logging: QuietLogRecord.java WarningListeners.java
Date Mon, 17 Jun 2013 13:00:39 GMT
Author: desruisseaux
Date: Mon Jun 17 13:00:39 2013
New Revision: 1493748

URL: http://svn.apache.org/r1493748
Log:
Declare the throwable in the LogRecord, to be omitted if the log is sent to the logger.

Added:
    sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/logging/QuietLogRecord.java
  (with props)
Modified:
    sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/logging/WarningListeners.java

Added: sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/logging/QuietLogRecord.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/logging/QuietLogRecord.java?rev=1493748&view=auto
==============================================================================
--- sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/logging/QuietLogRecord.java
(added)
+++ sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/logging/QuietLogRecord.java
[UTF-8] Mon Jun 17 13:00:39 2013
@@ -0,0 +1,70 @@
+/*
+ * 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.sis.util.logging;
+
+import java.util.logging.Level;
+import java.util.logging.LogRecord;
+
+
+/**
+ * A log record to be logged without stack trace, unless the user specified it explicitely.
+ *
+ * @author  Martin Desruisseaux (Geomatys)
+ * @since   0.3
+ * @version 0.3
+ * @module
+ */
+final class QuietLogRecord extends LogRecord {
+    /**
+     * For cross-version compatibility.
+     */
+    private static final long serialVersionUID = -8225936118310305206L;
+
+    /**
+     * {@code true} if the user invoked {@link #setThrown(Throwable)}.
+     * In such case, {@link #clearThrown()} will not reset the throwable to null.
+     */
+    private boolean explicitThrown;
+
+    /**
+     * Creates a new log record for the given message and exception.
+     */
+    QuietLogRecord(final String message, final Exception exception) {
+        super(Level.WARNING, message);
+        super.setThrown(exception);
+    }
+
+    /**
+     * Sets the throwable to the given value. The given throwable will not be cleared
+     * when the record will be logged.
+     */
+    @Override
+    public void setThrown(final Throwable thrown) {
+        explicitThrown = true;
+        super.setThrown(thrown);
+    }
+
+    /**
+     * Clears the throwable if it has not been explicit set by the user.
+     * Otherwise do nothing.
+     */
+    void clearThrown() {
+        if (!explicitThrown) {
+            super.setThrown(null);
+        }
+    }
+}

Propchange: sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/logging/QuietLogRecord.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/logging/QuietLogRecord.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain;charset=UTF-8

Modified: sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/logging/WarningListeners.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/logging/WarningListeners.java?rev=1493748&r1=1493747&r2=1493748&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/logging/WarningListeners.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/logging/WarningListeners.java
[UTF-8] Mon Jun 17 13:00:39 2013
@@ -125,6 +125,9 @@ public class WarningListeners<S> impleme
         } else {
             final Logger logger = getLogger();
             record.setLoggerName(logger.getName());
+            if (record instanceof QuietLogRecord) {
+                ((QuietLogRecord) record).clearThrown();
+            }
             logger.log(record);
         }
     }
@@ -133,10 +136,23 @@ public class WarningListeners<S> impleme
      * Reports a warning represented by the given message and exception.
      * At least one of {@code message} and {@code exception} shall be non-null.
      *
+     * {@section Stack trace omission}
+     * If there is no registered listener, then the {@link #warning(LogRecord)} method will
send the record to the
+     * {@linkplain #getLogger() logger}, but <em>without</em> the stack trace.
This is done that way because stack
+     * traces consume lot of space in the logging files, while being considered implementation
details in the context
+     * of {@code WarningListeners} (on the assumption that the logging message provides sufficient
information).
+     * If the stack trace is desired, then users can either:
+     * <ul>
+     *   <li>invoke {@code warning(LogRecord)} directly, or</li>
+     *   <li>override {@code warning(LogRecord)} and invoke {@link LogRecord#setThrown(Throwable)}
explicitely, or</li>
+     *   <li>register a listener which will log the record itself.</li>
+     * </ul>
+     *
      * @param message    The message to log, or {@code null} if none.
      * @param exception  The exception to log, or {@code null} if none.
      */
     public void warning(String message, final Exception exception) {
+        final LogRecord record;
         final StackTraceElement[] trace;
         if (exception != null) {
             trace = exception.getStackTrace();
@@ -144,11 +160,12 @@ public class WarningListeners<S> impleme
             if (message == null) {
                 message = exception.toString();
             }
+            record = new QuietLogRecord(message, exception);
         } else {
+            ArgumentChecks.ensureNonEmpty("message", message);
             trace = Thread.currentThread().getStackTrace();
+            record = new LogRecord(Level.WARNING, message);
         }
-        ArgumentChecks.ensureNonEmpty("message", message);
-        final LogRecord record = new LogRecord(Level.WARNING, message);
         for (int i=0; i<trace.length; i++) {
             StackTraceElement e = trace[i];
             if (isPublic(e)) {



Mime
View raw message