--- a/openide.util/apichanges.xml Wed Sep 01 15:32:15 2010 +0200 +++ a/openide.util/apichanges.xml Wed Sep 01 17:13:43 2010 +0200 @@ -51,6 +51,23 @@ Actions API + + + Supress logging of not important exceptions + + + + + +

+ New method Exceptions.attachSeverity allows + everyone to thrown an exception, which later will not be + logged. +

+
+ + +
Simplified constructor for RequestProcessor --- a/openide.util/manifest.mf Wed Sep 01 15:32:15 2010 +0200 +++ a/openide.util/manifest.mf Wed Sep 01 17:13:43 2010 +0200 @@ -1,5 +1,5 @@ Manifest-Version: 1.0 OpenIDE-Module: org.openide.util OpenIDE-Module-Localizing-Bundle: org/openide/util/Bundle.properties -OpenIDE-Module-Specification-Version: 8.7 +OpenIDE-Module-Specification-Version: 8.8 --- a/openide.util/src/org/openide/util/Exceptions.java Wed Sep 01 15:32:15 2010 +0200 +++ a/openide.util/src/org/openide/util/Exceptions.java Wed Sep 01 17:13:43 2010 +0200 @@ -115,6 +115,23 @@ ae.addRecord(rec); return e; } + + /** + * Attaches a given severity to the exception. When the exception is + * later passed to {@link #printStackTrace} method, the level is + * then used as a level for reported {@link LogRecord}. This allows + * those who report exceptions to annotate them as unimportant, + * expected. + * + * @param e exception to assign severity to + * @param severity the severity + * @since 8.8 + */ + public static E attachSeverity(E e, Level severity) { + AnnException ae = AnnException.findOrCreate(e, true); + ae.addRecord(new LogRecord(severity, null)); + return e; + } /** Extracts previously attached localized message for a given throwable. * Complements {@link #attachLocalizedMessage}. @@ -171,12 +188,25 @@ * @param t the exception to notify */ public static void printStackTrace(Throwable t) { + AnnException ae = AnnException.findOrCreate(t, false); + Level level = null; + if (ae != null) { + for (LogRecord r : ae.records) { + if (r.getLevel() != Level.ALL) { + level = r.getLevel(); + break; + } + } + } + if (level == null) { + level = OwnLevel.UNKNOWN; + } AnnException extra = AnnException.extras.get(t); if (extra != null) { assert t == extra.getCause(); t = extra; } - LOG.log(OwnLevel.UNKNOWN, null, t); + LOG.log(level, null, t); } /** An exception that has a log record associated with itself, so @@ -213,6 +243,9 @@ private static Map extras = new WeakHashMap(); static AnnException findOrCreate(Throwable t, boolean create) { + if (t == null) { + return null; + } if (t instanceof AnnException) { return (AnnException)t; } --- a/openide.util/test/unit/src/org/openide/util/ExceptionsTest.java Wed Sep 01 15:32:15 2010 +0200 +++ a/openide.util/test/unit/src/org/openide/util/ExceptionsTest.java Wed Sep 01 17:13:43 2010 +0200 @@ -44,9 +44,13 @@ package org.openide.util; +import java.io.IOException; import java.io.PrintWriter; import java.io.StringWriter; +import java.util.logging.Handler; import java.util.logging.Level; +import java.util.logging.LogRecord; +import java.util.logging.Logger; import junit.framework.TestCase; import org.netbeans.junit.Log; @@ -108,6 +112,42 @@ assertCleanStackTrace(e); } + + public void testLogLevel() { + Exception e = new IOException("Help"); + + Exception result = Exceptions.attachSeverity(e, Level.FINE); + assertSame(e, result); + + class H extends Handler { + int cnt; + + @Override + public void publish(LogRecord record) { + assertEquals("Fine is the level", Level.FINE, record.getLevel()); + cnt++; + } + + @Override + public void flush() { + } + + @Override + public void close() throws SecurityException { + } + + } + + H h = new H(); + h.setLevel(Level.ALL); + Logger L = Logger.getLogger(""); + L.setLevel(Level.ALL); + L.addHandler(h); + + Exceptions.printStackTrace(e); + L.removeHandler(h); + assertEquals("Called once", 1, h.cnt); + } public void testAttachLocalizedMessage() { Exception e = new Exception("Help");