1 19 20 package org.netbeans.modules.exceptions; 21 import java.text.MessageFormat ; 22 import java.util.Date ; 23 import java.util.HashSet ; 24 import java.util.Set ; 25 import java.util.logging.Level ; 26 import java.util.logging.LogRecord ; 27 import org.openide.util.NbBundle; 28 29 33 public class ExceptionLogger { 34 35 36 private static final String NAME = NbBundle.getMessage(ExceptionLogger.class, "ExceptionLoggerName"); 39 private static Collector coll = null; 40 private static final MessageFormat EXC_HEADER = new MessageFormat (NbBundle.getMessage(ExceptionLogger.class, 41 "ExceptionMessage")); 42 private static Set <Throwable > causes = new HashSet <Throwable >(); 43 44 public ExceptionLogger() { 45 46 } 47 48 public static void logError(LogRecord rec){ 49 logError(rec.getLevel(), rec.getThrown()); 50 } 51 52 56 public static void logError(Level severity, Throwable throwable){ 57 LogRecord log = new LogRecord (convertSeverity(severity), EXC_HEADER.format(new Object [] { severity.toString(), new Date () })); 60 log.setThrown(convert(throwable)); 61 log.setLoggerName(NAME); 62 getColl().write(log); } 64 65 69 private static Level convertSeverity(Level severity){ 70 if ((severity.equals(Level.SEVERE))||(severity.equals(Level.WARNING))||(severity.equals(Level.INFO))|| 71 (severity.equals(Level.FINE))||(severity.equals(Level.FINER))||(severity.equals(Level.FINEST))|| 72 (severity.equals(Level.CONFIG))) return severity; 73 else return Level.WARNING; 74 } 75 76 77 private static Throwable convertRecursivly(Throwable throwable){ 78 if (throwable==null){ 79 return throwable; 80 } 81 causes.add(throwable); 82 Throwable tNew = new Throwable (throwable.toString()); 83 tNew.setStackTrace(throwable.getStackTrace()); 84 Throwable cause = throwable.getCause(); 85 if ((cause != null)&&(!causes.contains(cause))){ 86 tNew.initCause(convertRecursivly(cause)); 87 } 88 return tNew; 89 } 90 91 94 public static synchronized Throwable convert(Throwable throwable){ 95 causes.clear(); 96 return convertRecursivly(throwable); 97 } 98 99 public static void log(LogRecord rec){ 100 rec.setLoggerName(NAME); 101 getColl().write(rec); 102 } 103 104 public static void log(Level severity, String str){ 105 LogRecord log = new LogRecord (severity, str); 107 log.setLoggerName(NAME); 108 getColl().write(log); } 110 111 116 123 private static synchronized Collector getColl(){ 124 if (coll == null){ 125 coll = Collector.getDefault(); 126 } 127 return coll; 128 } 129 130 } 131 | Popular Tags |